2 * ========================LICENSE_START=================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.configuration;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
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;
34 import java.util.Map.Entry;
37 import javax.validation.constraints.NotNull;
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;
46 * Parser for the Json representing of the component configuration.
48 public class ApplicationConfigParser {
49 private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigParser.class);
51 private static final String CONFIG = "config";
52 private static final String CONTROLLER = "controller";
56 public interface ConfigParserResult {
57 List<RicConfig> ricConfigs();
59 Map<String, ControllerConfig> controllerConfigs();
61 String dmaapConsumerTopicUrl();
63 String dmaapProducerTopicUrl();
67 public ConfigParserResult parse(JsonObject root) throws ServiceException {
69 String dmaapProducerTopicUrl = "";
70 String dmaapConsumerTopicUrl = "";
72 JsonObject pmsConfigJson = root.getAsJsonObject(CONFIG);
74 if (pmsConfigJson == null) {
75 throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
78 JsonObject json = pmsConfigJson.getAsJsonObject("streams_publishes");
80 dmaapProducerTopicUrl = parseDmaapConfig(json);
83 json = pmsConfigJson.getAsJsonObject("streams_subscribes");
85 dmaapConsumerTopicUrl = parseDmaapConfig(json);
88 List<RicConfig> ricConfigs = parseRics(pmsConfigJson);
89 Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson);
90 checkConfigurationConsistency(ricConfigs, controllerConfigs);
92 return ImmutableConfigParserResult.builder() //
93 .dmaapConsumerTopicUrl(dmaapConsumerTopicUrl) //
94 .dmaapProducerTopicUrl(dmaapProducerTopicUrl) //
95 .ricConfigs(ricConfigs) //
96 .controllerConfigs(controllerConfigs) //
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());
108 if (!ricNames.add(ric.ricId())) {
109 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.ricId());
111 if (!ric.controllerName().isEmpty() && controllerConfigs.get(ric.controllerName()) == null) {
112 throw new ServiceException(
113 "Configuration error, controller configuration not found: " + ric.controllerName());
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() : "") //
129 if (!ricConfig.baseUrl().isEmpty()) {
130 result.add(ricConfig);
132 logger.error("RIC configuration error {}, baseUrl is empty", ricConfig.ricId());
138 Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
139 if (config.get(CONTROLLER) == null) {
140 return new HashMap<>();
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()) // )
152 if (result.put(controllerConfig.name(), controllerConfig) != null) {
153 throw new ServiceException(
154 "Configuration error, more than one controller with name: " + controllerConfig.name());
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());
167 return managedElementIds;
170 private static JsonElement get(JsonObject obj, String... alternativeMemberNames) throws ServiceException {
171 for (String memberName : alternativeMemberNames) {
172 JsonElement elem = obj.get(memberName);
177 throw new ServiceException("Could not find member: " + Arrays.toString(alternativeMemberNames) + " in: " + obj);
180 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
181 return get(obj, memberName).getAsJsonArray();
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);
190 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
191 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
192 return getAsString(dmaapInfo, "topic_url");
195 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
196 return get(obj, memberName).getAsString();