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.common.io.CharStreams;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.Map.Entry;
42 import lombok.Builder;
45 import org.json.JSONObject;
46 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.http.HttpStatus;
52 * Parser for the Json representing of the component configuration.
54 public class ApplicationConfigParser {
55 private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigParser.class);
57 private static final String CONFIG = "config";
58 private static final String CONTROLLER = "controller";
59 private final ApplicationConfig applicationConfig;
61 public ApplicationConfigParser(ApplicationConfig applicationConfig) {
62 this.applicationConfig = applicationConfig;
67 public static class ConfigParserResult {
68 private List<RicConfig> ricConfigs;
71 private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
74 private String dmaapConsumerTopicUrl = "";
77 private String dmaapProducerTopicUrl = "";
81 public ConfigParserResult parse(JsonObject root) throws ServiceException {
83 validateJsonObjectAgainstSchema(root);
85 String dmaapProducerTopicUrl = "";
86 String dmaapConsumerTopicUrl = "";
88 JsonObject pmsConfigJson = root.getAsJsonObject(CONFIG);
90 if (pmsConfigJson == null) {
91 throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
94 JsonObject json = pmsConfigJson.getAsJsonObject("streams_publishes");
96 dmaapProducerTopicUrl = parseDmaapConfig(json);
99 json = pmsConfigJson.getAsJsonObject("streams_subscribes");
101 dmaapConsumerTopicUrl = parseDmaapConfig(json);
104 List<RicConfig> ricConfigs = parseRics(pmsConfigJson);
105 Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson);
106 checkConfigurationConsistency(ricConfigs, controllerConfigs);
108 return ConfigParserResult.builder() //
109 .dmaapConsumerTopicUrl(dmaapConsumerTopicUrl) //
110 .dmaapProducerTopicUrl(dmaapProducerTopicUrl) //
111 .ricConfigs(ricConfigs) //
112 .controllerConfigs(controllerConfigs) //
116 private void validateJsonObjectAgainstSchema(Object object) throws ServiceException {
117 if (applicationConfig.getConfigurationFileSchemaPath() == null
118 || applicationConfig.getConfigurationFileSchemaPath().isEmpty()) {
123 String schemaAsString = readSchemaFile();
125 JSONObject schemaJSON = new JSONObject(schemaAsString);
126 var schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
128 String objectAsString = object.toString();
129 JSONObject json = new JSONObject(objectAsString);
130 schema.validate(json);
131 } catch (Exception e) {
132 throw new ServiceException("Json schema validation failure: " + e.toString());
136 private String readSchemaFile() throws IOException, ServiceException {
137 String filePath = applicationConfig.getConfigurationFileSchemaPath();
138 InputStream in = getClass().getResourceAsStream(filePath);
139 logger.debug("Reading application schema file from: {} with: {}", filePath, in);
141 throw new ServiceException("Could not read application configuration schema file: " + filePath,
142 HttpStatus.INTERNAL_SERVER_ERROR);
144 return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
147 private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
148 Map<String, ControllerConfig> controllerConfigs) throws ServiceException {
149 Set<String> ricUrls = new HashSet<>();
150 Set<String> ricNames = new HashSet<>();
151 for (RicConfig ric : ricConfigs) {
152 if (!ricUrls.add(ric.getBaseUrl())) {
153 throw new ServiceException("Configuration error, more than one RIC URL: " + ric.getBaseUrl());
155 if (!ricNames.add(ric.getRicId())) {
156 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.getRicId());
158 if (!ric.getControllerName().isEmpty() && controllerConfigs.get(ric.getControllerName()) == null) {
159 throw new ServiceException(
160 "Configuration error, controller configuration not found: " + ric.getControllerName());
165 private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
166 List<RicConfig> result = new ArrayList<>();
167 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
168 JsonObject ricJsonObj = ricElem.getAsJsonObject();
169 RicConfig ricConfig = RicConfig.builder() //
170 .ricId(get(ricJsonObj, "name", "id", "ricId").getAsString()) //
171 .baseUrl(get(ricJsonObj, "baseUrl").getAsString()) //
172 .managedElementIds(parseManagedElementIds(get(ricJsonObj, "managedElementIds").getAsJsonArray())) //
173 .controllerName(getString(ricJsonObj, CONTROLLER, ""))
174 .customAdapterClass(getString(ricJsonObj, "customAdapterClass", "")) //
176 if (!ricConfig.getBaseUrl().isEmpty()) {
177 result.add(ricConfig);
179 logger.error("RIC configuration error {}, baseUrl is empty", ricConfig.getRicId());
185 String getString(JsonObject obj, String name, String defaultValue) {
186 JsonElement elem = obj.get(name);
188 return elem.getAsString();
193 Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
194 if (config.get(CONTROLLER) == null) {
195 return new HashMap<>();
197 Map<String, ControllerConfig> result = new HashMap<>();
198 for (JsonElement element : getAsJsonArray(config, CONTROLLER)) {
199 JsonObject controllerAsJson = element.getAsJsonObject();
200 ControllerConfig controllerConfig = ControllerConfig.builder() //
201 .name(get(controllerAsJson, "name").getAsString()) //
202 .baseUrl(get(controllerAsJson, "baseUrl").getAsString()) //
203 .password(get(controllerAsJson, "password").getAsString()) //
204 .userName(get(controllerAsJson, "userName").getAsString()) // )
207 if (result.put(controllerConfig.getName(), controllerConfig) != null) {
208 throw new ServiceException(
209 "Configuration error, more than one controller with name: " + controllerConfig.getName());
215 private List<String> parseManagedElementIds(JsonArray asJsonObject) {
216 Iterator<JsonElement> iterator = asJsonObject.iterator();
217 List<String> managedElementIds = new ArrayList<>();
218 while (iterator.hasNext()) {
219 managedElementIds.add(iterator.next().getAsString());
222 return managedElementIds;
225 private static JsonElement get(JsonObject obj, String... alternativeMemberNames) throws ServiceException {
226 for (String memberName : alternativeMemberNames) {
227 JsonElement elem = obj.get(memberName);
232 throw new ServiceException("Could not find member: " + Arrays.toString(alternativeMemberNames) + " in: " + obj);
235 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
236 return get(obj, memberName).getAsJsonArray();
239 private String parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
240 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
241 if (streamConfigEntries.size() != 1) {
242 throw new ServiceException(
243 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
245 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
246 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
247 return getAsString(dmaapInfo, "topic_url");
250 private static String getAsString(JsonObject obj, String memberName) throws ServiceException {
251 return get(obj, memberName).getAsString();