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;
44 * Parser for the Json representing of the component configuration.
46 public class ApplicationConfigParser {
48 private static final String CONFIG = "config";
49 private static final String CONTROLLER = "controller";
53 public interface ConfigParserResult {
54 List<RicConfig> ricConfigs();
56 Map<String, ControllerConfig> controllerConfigs();
58 String dmaapConsumerTopicUrl();
60 String dmaapProducerTopicUrl();
64 public ConfigParserResult parse(JsonObject root) throws ServiceException {
66 String dmaapProducerTopicUrl = "";
67 String dmaapConsumerTopicUrl = "";
69 JsonObject pmsConfigJson = root.getAsJsonObject(CONFIG);
71 if (pmsConfigJson == null) {
72 throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
75 JsonObject json = pmsConfigJson.getAsJsonObject("streams_publishes");
77 dmaapProducerTopicUrl = parseDmaapConfig(json);
80 json = pmsConfigJson.getAsJsonObject("streams_subscribes");
82 dmaapConsumerTopicUrl = parseDmaapConfig(json);
85 List<RicConfig> ricConfigs = parseRics(pmsConfigJson);
86 Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson);
87 checkConfigurationConsistency(ricConfigs, controllerConfigs);
89 return ImmutableConfigParserResult.builder() //
90 .dmaapConsumerTopicUrl(dmaapConsumerTopicUrl) //
91 .dmaapProducerTopicUrl(dmaapProducerTopicUrl) //
92 .ricConfigs(ricConfigs) //
93 .controllerConfigs(controllerConfigs) //
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());
105 if (!ricNames.add(ric.ricId())) {
106 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.ricId());
108 if (!ric.controllerName().isEmpty() && controllerConfigs.get(ric.controllerName()) == null) {
109 throw new ServiceException(
110 "Configuration error, controller configuration not found: " + ric.controllerName());
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() : "") //
127 result.add(ricConfig);
132 Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
133 if (config.get(CONTROLLER) == null) {
134 return new HashMap<>();
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()) // )
146 if (result.put(controllerConfig.name(), controllerConfig) != null) {
147 throw new ServiceException(
148 "Configuration error, more than one controller with name: " + controllerConfig.name());
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());
161 return managedElementIds;
164 private static JsonElement get(JsonObject obj, String... alternativeMemberNames) throws ServiceException {
165 for (String memberName : alternativeMemberNames) {
166 JsonElement elem = obj.get(memberName);
171 throw new ServiceException("Could not find member: " + Arrays.toString(alternativeMemberNames) + " in: " + obj);
174 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
175 return get(obj, memberName).getAsJsonArray();
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);
184 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
185 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
186 return getAsString(dmaapInfo, "topic_url");
189 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
190 return get(obj, memberName).getAsString();