a6af20235421d3a1dec018236ce021bfd7c5cde6
[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 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Parser for the Json representing of the component configuration.
47  */
48 public class ApplicationConfigParser {
49     private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigParser.class);
50
51     private static final String CONFIG = "config";
52     private static final String CONTROLLER = "controller";
53
54     @Value.Immutable
55     @Gson.TypeAdapters
56     public interface ConfigParserResult {
57         List<RicConfig> ricConfigs();
58
59         Map<String, ControllerConfig> controllerConfigs();
60
61         String dmaapConsumerTopicUrl();
62
63         String dmaapProducerTopicUrl();
64
65     }
66
67     public ConfigParserResult parse(JsonObject root) throws ServiceException {
68
69         String dmaapProducerTopicUrl = "";
70         String dmaapConsumerTopicUrl = "";
71
72         JsonObject pmsConfigJson = root.getAsJsonObject(CONFIG);
73
74         if (pmsConfigJson == null) {
75             throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
76         }
77
78         JsonObject json = pmsConfigJson.getAsJsonObject("streams_publishes");
79         if (json != null) {
80             dmaapProducerTopicUrl = parseDmaapConfig(json);
81         }
82
83         json = pmsConfigJson.getAsJsonObject("streams_subscribes");
84         if (json != null) {
85             dmaapConsumerTopicUrl = parseDmaapConfig(json);
86         }
87
88         List<RicConfig> ricConfigs = parseRics(pmsConfigJson);
89         Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson);
90         checkConfigurationConsistency(ricConfigs, controllerConfigs);
91
92         return ImmutableConfigParserResult.builder() //
93                 .dmaapConsumerTopicUrl(dmaapConsumerTopicUrl) //
94                 .dmaapProducerTopicUrl(dmaapProducerTopicUrl) //
95                 .ricConfigs(ricConfigs) //
96                 .controllerConfigs(controllerConfigs) //
97                 .build();
98     }
99
100     private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
101             Map<String, ControllerConfig> controllerConfigs) throws ServiceException {
102         Set<String> ricUrls = new HashSet<>();
103         Set<String> ricNames = new HashSet<>();
104         for (RicConfig ric : ricConfigs) {
105             if (!ricUrls.add(ric.baseUrl())) {
106                 throw new ServiceException("Configuration error, more than one RIC URL: " + ric.baseUrl());
107             }
108             if (!ricNames.add(ric.ricId())) {
109                 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.ricId());
110             }
111             if (!ric.controllerName().isEmpty() && controllerConfigs.get(ric.controllerName()) == null) {
112                 throw new ServiceException(
113                         "Configuration error, controller configuration not found: " + ric.controllerName());
114             }
115         }
116     }
117
118     private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
119         List<RicConfig> result = new ArrayList<>();
120         for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
121             JsonObject ricAsJson = ricElem.getAsJsonObject();
122             JsonElement controllerNameElement = ricAsJson.get(CONTROLLER);
123             RicConfig ricConfig = ImmutableRicConfig.builder() //
124                     .ricId(get(ricAsJson, "name", "id", "ricId").getAsString()) //
125                     .baseUrl(get(ricAsJson, "baseUrl").getAsString()) //
126                     .managedElementIds(parseManagedElementIds(get(ricAsJson, "managedElementIds").getAsJsonArray())) //
127                     .controllerName(controllerNameElement != null ? controllerNameElement.getAsString() : "") //
128                     .build();
129             if (!ricConfig.baseUrl().isEmpty()) {
130                 result.add(ricConfig);
131             } else {
132                 logger.error("RIC configuration error {}, baseUrl is empty", ricConfig.ricId());
133             }
134         }
135         return result;
136     }
137
138     Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
139         if (config.get(CONTROLLER) == null) {
140             return new HashMap<>();
141         }
142         Map<String, ControllerConfig> result = new HashMap<>();
143         for (JsonElement element : getAsJsonArray(config, CONTROLLER)) {
144             JsonObject controllerAsJson = element.getAsJsonObject();
145             ImmutableControllerConfig controllerConfig = ImmutableControllerConfig.builder() //
146                     .name(get(controllerAsJson, "name").getAsString()) //
147                     .baseUrl(get(controllerAsJson, "baseUrl").getAsString()) //
148                     .password(get(controllerAsJson, "password").getAsString()) //
149                     .userName(get(controllerAsJson, "userName").getAsString()) // )
150                     .build();
151
152             if (result.put(controllerConfig.name(), controllerConfig) != null) {
153                 throw new ServiceException(
154                         "Configuration error, more than one controller with name: " + controllerConfig.name());
155             }
156         }
157         return result;
158     }
159
160     private List<String> parseManagedElementIds(JsonArray asJsonObject) {
161         Iterator<JsonElement> iterator = asJsonObject.iterator();
162         List<String> managedElementIds = new ArrayList<>();
163         while (iterator.hasNext()) {
164             managedElementIds.add(iterator.next().getAsString());
165
166         }
167         return managedElementIds;
168     }
169
170     private static JsonElement get(JsonObject obj, String... alternativeMemberNames) throws ServiceException {
171         for (String memberName : alternativeMemberNames) {
172             JsonElement elem = obj.get(memberName);
173             if (elem != null) {
174                 return elem;
175             }
176         }
177         throw new ServiceException("Could not find member: " + Arrays.toString(alternativeMemberNames) + " in: " + obj);
178     }
179
180     private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
181         return get(obj, memberName).getAsJsonArray();
182     }
183
184     private String parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
185         Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
186         if (streamConfigEntries.size() != 1) {
187             throw new ServiceException(
188                     "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
189         }
190         JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
191         JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
192         return getAsString(dmaapInfo, "topic_url");
193     }
194
195     private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
196         return get(obj, memberName).getAsString();
197     }
198 }