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;
41 import lombok.Builder;
44 import org.json.JSONObject;
45 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.springframework.http.HttpStatus;
51 * Parser for the Json representing of the component configuration.
53 public class ApplicationConfigParser {
54 private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigParser.class);
56 private static final String CONFIG = "config";
57 private static final String CONTROLLER = "controller";
58 private final ApplicationConfig applicationConfig;
60 public ApplicationConfigParser(ApplicationConfig applicationConfig) {
61 this.applicationConfig = applicationConfig;
66 public static class ConfigParserResult {
67 private List<RicConfig> ricConfigs;
70 private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
74 public ConfigParserResult parse(JsonObject root) throws ServiceException {
76 validateJsonObjectAgainstSchema(root);
78 JsonObject pmsConfigJson = root.getAsJsonObject(CONFIG);
80 if (pmsConfigJson == null) {
81 throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
84 Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson);
85 List<RicConfig> ricConfigs = parseRics(pmsConfigJson, controllerConfigs);
87 checkConfigurationConsistency(ricConfigs, controllerConfigs);
89 return ConfigParserResult.builder() //
90 .ricConfigs(ricConfigs) //
91 .controllerConfigs(controllerConfigs) //
95 private void validateJsonObjectAgainstSchema(Object object) throws ServiceException {
96 if (applicationConfig.getConfigurationFileSchemaPath() == null
97 || applicationConfig.getConfigurationFileSchemaPath().isEmpty()) {
102 String schemaAsString = readSchemaFile();
104 JSONObject schemaJSON = new JSONObject(schemaAsString);
105 var schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
107 String objectAsString = object.toString();
108 JSONObject json = new JSONObject(objectAsString);
109 schema.validate(json);
110 } catch (Exception e) {
111 throw new ServiceException("Json schema validation failure: " + e.toString());
115 private String readSchemaFile() throws IOException, ServiceException {
116 String filePath = applicationConfig.getConfigurationFileSchemaPath();
117 InputStream in = getClass().getResourceAsStream(filePath);
118 logger.debug("Reading application schema file from: {} with: {}", filePath, in);
120 throw new ServiceException("Could not read application configuration schema file: " + filePath,
121 HttpStatus.INTERNAL_SERVER_ERROR);
123 return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
126 private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
127 Map<String, ControllerConfig> controllerConfigs) throws ServiceException {
128 Set<String> ricUrls = new HashSet<>();
129 Set<String> ricNames = new HashSet<>();
130 for (RicConfig ric : ricConfigs) {
131 if (!ricUrls.add(ric.getBaseUrl())) {
132 throw new ServiceException("Configuration error, more than one RIC URL: " + ric.getBaseUrl());
134 if (!ricNames.add(ric.getRicId())) {
135 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.getRicId());
140 private List<RicConfig> parseRics(JsonObject config, Map<String, ControllerConfig> controllerConfigs)
141 throws ServiceException {
142 List<RicConfig> result = new ArrayList<>();
143 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
144 JsonObject ricJsonObj = ricElem.getAsJsonObject();
145 String controllerName = getString(ricJsonObj, CONTROLLER, "");
146 ControllerConfig controllerConfig = controllerConfigs.get(controllerName);
147 if (!controllerName.isEmpty() && controllerConfig == null) {
148 throw new ServiceException(
149 "Configuration error, controller configuration not found: " + controllerName);
152 RicConfig ricConfig = RicConfig.builder() //
153 .ricId(get(ricJsonObj, "name", "id", "ricId").getAsString()) //
154 .baseUrl(get(ricJsonObj, "baseUrl").getAsString()) //
155 .managedElementIds(parseManagedElementIds(get(ricJsonObj, "managedElementIds").getAsJsonArray())) //
156 .controllerConfig(controllerConfig)
157 .customAdapterClass(getString(ricJsonObj, "customAdapterClass", "")) //
159 if (!ricConfig.getBaseUrl().isEmpty()) {
160 result.add(ricConfig);
162 logger.error("RIC configuration error {}, baseUrl is empty", ricConfig.getRicId());
168 String getString(JsonObject obj, String name, String defaultValue) {
169 JsonElement elem = obj.get(name);
171 return elem.getAsString();
176 Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
177 if (config.get(CONTROLLER) == null) {
178 return new HashMap<>();
180 Map<String, ControllerConfig> result = new HashMap<>();
181 for (JsonElement element : getAsJsonArray(config, CONTROLLER)) {
182 JsonObject controllerAsJson = element.getAsJsonObject();
183 ControllerConfig controllerConfig = ControllerConfig.builder() //
184 .name(get(controllerAsJson, "name").getAsString()) //
185 .baseUrl(get(controllerAsJson, "baseUrl").getAsString()) //
186 .password(get(controllerAsJson, "password").getAsString()) //
187 .userName(get(controllerAsJson, "userName").getAsString()) // )
190 if (result.put(controllerConfig.getName(), controllerConfig) != null) {
191 throw new ServiceException(
192 "Configuration error, more than one controller with name: " + controllerConfig.getName());
198 private List<String> parseManagedElementIds(JsonArray asJsonObject) {
199 Iterator<JsonElement> iterator = asJsonObject.iterator();
200 List<String> managedElementIds = new ArrayList<>();
201 while (iterator.hasNext()) {
202 managedElementIds.add(iterator.next().getAsString());
205 return managedElementIds;
208 private static JsonElement get(JsonObject obj, String... alternativeMemberNames) throws ServiceException {
209 for (String memberName : alternativeMemberNames) {
210 JsonElement elem = obj.get(memberName);
215 throw new ServiceException("Could not find member: " + Arrays.toString(alternativeMemberNames) + " in: " + obj);
218 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
219 return get(obj, memberName).getAsJsonArray();
222 private static String getAsString(JsonObject obj, String memberName) throws ServiceException {
223 return get(obj, memberName).getAsString();