07c04e1a10dfbe55f456b95d810cc7333c51c811
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
22
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36
37 import javax.validation.constraints.NotNull;
38
39 import org.immutables.gson.Gson;
40 import org.immutables.value.Value;
41 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
42
43 /**
44  * Parser for the Json representing of the component configuration.
45  */
46 public class ApplicationConfigParser {
47
48     private static final String CONFIG = "config";
49     private static final String CONTROLLER = "controller";
50
51     @Value.Immutable
52     @Gson.TypeAdapters
53     public interface ConfigParserResult {
54         List<RicConfig> ricConfigs();
55
56         Map<String, ControllerConfig> controllerConfigs();
57
58         String dmaapConsumerTopicUrl();
59
60         String dmaapProducerTopicUrl();
61
62     }
63
64     public ConfigParserResult parse(JsonObject root) throws ServiceException {
65
66         String dmaapProducerTopicUrl = "";
67         String dmaapConsumerTopicUrl = "";
68
69         JsonObject pmsConfigJson = root.getAsJsonObject(CONFIG);
70
71         if (pmsConfigJson == null) {
72             throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
73         }
74
75         JsonObject json = pmsConfigJson.getAsJsonObject("streams_publishes");
76         if (json != null) {
77             dmaapProducerTopicUrl = parseDmaapConfig(json);
78         }
79
80         json = pmsConfigJson.getAsJsonObject("streams_subscribes");
81         if (json != null) {
82             dmaapConsumerTopicUrl = parseDmaapConfig(json);
83         }
84
85         List<RicConfig> ricConfigs = parseRics(pmsConfigJson);
86         Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson);
87         checkConfigurationConsistency(ricConfigs, controllerConfigs);
88
89         return ImmutableConfigParserResult.builder() //
90             .dmaapConsumerTopicUrl(dmaapConsumerTopicUrl) //
91             .dmaapProducerTopicUrl(dmaapProducerTopicUrl) //
92             .ricConfigs(ricConfigs) //
93             .controllerConfigs(controllerConfigs) //
94             .build();
95     }
96
97     private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
98         Map<String, ControllerConfig> controllerConfigs) throws ServiceException {
99         Set<String> ricUrls = new HashSet<>();
100         Set<String> ricNames = new HashSet<>();
101         for (RicConfig ric : ricConfigs) {
102             if (!ricUrls.add(ric.baseUrl())) {
103                 throw new ServiceException("Configuration error, more than one RIC URL: " + ric.baseUrl());
104             }
105             if (!ricNames.add(ric.ricId())) {
106                 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.ricId());
107             }
108             if (!ric.controllerName().isEmpty() && controllerConfigs.get(ric.controllerName()) == null) {
109                 throw new ServiceException(
110                     "Configuration error, controller configuration not found: " + ric.controllerName());
111             }
112
113         }
114     }
115
116     private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
117         List<RicConfig> result = new ArrayList<>();
118         for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
119             JsonObject ricAsJson = ricElem.getAsJsonObject();
120             JsonElement controllerNameElement = ricAsJson.get(CONTROLLER);
121             RicConfig ricConfig = ImmutableRicConfig.builder() //
122                 .ricId(get(ricAsJson, "name", "id", "ricId").getAsString()) //
123                 .baseUrl(get(ricAsJson, "baseUrl").getAsString()) //
124                 .managedElementIds(parseManagedElementIds(get(ricAsJson, "managedElementIds").getAsJsonArray())) //
125                 .controllerName(controllerNameElement != null ? controllerNameElement.getAsString() : "") //
126                 .build();
127             result.add(ricConfig);
128         }
129         return result;
130     }
131
132     Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
133         if (config.get(CONTROLLER) == null) {
134             return new HashMap<>();
135         }
136         Map<String, ControllerConfig> result = new HashMap<>();
137         for (JsonElement element : getAsJsonArray(config, CONTROLLER)) {
138             JsonObject controllerAsJson = element.getAsJsonObject();
139             ImmutableControllerConfig controllerConfig = ImmutableControllerConfig.builder() //
140                 .name(get(controllerAsJson, "name").getAsString()) //
141                 .baseUrl(get(controllerAsJson, "baseUrl").getAsString()) //
142                 .password(get(controllerAsJson, "password").getAsString()) //
143                 .userName(get(controllerAsJson, "userName").getAsString()) // )
144                 .build();
145
146             if (result.put(controllerConfig.name(), controllerConfig) != null) {
147                 throw new ServiceException(
148                     "Configuration error, more than one controller with name: " + controllerConfig.name());
149             }
150         }
151         return result;
152     }
153
154     private List<String> parseManagedElementIds(JsonArray asJsonObject) {
155         Iterator<JsonElement> iterator = asJsonObject.iterator();
156         List<String> managedElementIds = new ArrayList<>();
157         while (iterator.hasNext()) {
158             managedElementIds.add(iterator.next().getAsString());
159
160         }
161         return managedElementIds;
162     }
163
164     private static JsonElement get(JsonObject obj, String... alternativeMemberNames) throws ServiceException {
165         for (String memberName : alternativeMemberNames) {
166             JsonElement elem = obj.get(memberName);
167             if (elem != null) {
168                 return elem;
169             }
170         }
171         throw new ServiceException("Could not find member: " + Arrays.toString(alternativeMemberNames) + " in: " + obj);
172     }
173
174     private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
175         return get(obj, memberName).getAsJsonArray();
176     }
177
178     private String parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
179         Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
180         if (streamConfigEntries.size() != 1) {
181             throw new ServiceException(
182                 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
183         }
184         JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
185         JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
186         return getAsString(dmaapInfo, "topic_url");
187     }
188
189     private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
190         return get(obj, memberName).getAsString();
191     }
192 }