9df901fae88646427f10b50af269952d2d7ae54d
[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.common.io.CharStreams;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27
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;
38 import java.util.Map;
39 import java.util.Set;
40
41 import lombok.Builder;
42 import lombok.Getter;
43
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;
49
50 /**
51  * Parser for the Json representing of the component configuration.
52  */
53 public class ApplicationConfigParser {
54     private static final Logger logger = LoggerFactory.getLogger(ApplicationConfigParser.class);
55
56     private static final String CONFIG = "config";
57     private static final String CONTROLLER = "controller";
58     private final ApplicationConfig applicationConfig;
59
60     public ApplicationConfigParser(ApplicationConfig applicationConfig) {
61         this.applicationConfig = applicationConfig;
62     }
63
64     @Builder
65     @Getter
66     public static class ConfigParserResult {
67         private List<RicConfig> ricConfigs;
68
69         @Builder.Default
70         private Map<String, ControllerConfig> controllerConfigs = new HashMap<>();
71
72     }
73
74     public ConfigParserResult parse(JsonObject root) throws ServiceException {
75
76         validateJsonObjectAgainstSchema(root);
77
78         JsonObject pmsConfigJson = root.getAsJsonObject(CONFIG);
79
80         if (pmsConfigJson == null) {
81             throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
82         }
83
84         List<RicConfig> ricConfigs = parseRics(pmsConfigJson);
85         Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(pmsConfigJson);
86         checkConfigurationConsistency(ricConfigs, controllerConfigs);
87
88         return ConfigParserResult.builder() //
89                 .ricConfigs(ricConfigs) //
90                 .controllerConfigs(controllerConfigs) //
91                 .build();
92     }
93
94     private void validateJsonObjectAgainstSchema(Object object) throws ServiceException {
95         if (applicationConfig.getConfigurationFileSchemaPath() == null
96                 || applicationConfig.getConfigurationFileSchemaPath().isEmpty()) {
97             return;
98         }
99
100         try {
101             String schemaAsString = readSchemaFile();
102
103             JSONObject schemaJSON = new JSONObject(schemaAsString);
104             var schema = org.everit.json.schema.loader.SchemaLoader.load(schemaJSON);
105
106             String objectAsString = object.toString();
107             JSONObject json = new JSONObject(objectAsString);
108             schema.validate(json);
109         } catch (Exception e) {
110             throw new ServiceException("Json schema validation failure: " + e.toString());
111         }
112     }
113
114     private String readSchemaFile() throws IOException, ServiceException {
115         String filePath = applicationConfig.getConfigurationFileSchemaPath();
116         InputStream in = getClass().getResourceAsStream(filePath);
117         logger.debug("Reading application schema file from: {} with: {}", filePath, in);
118         if (in == null) {
119             throw new ServiceException("Could not read application configuration schema file: " + filePath,
120                     HttpStatus.INTERNAL_SERVER_ERROR);
121         }
122         return CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
123     }
124
125     private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
126             Map<String, ControllerConfig> controllerConfigs) throws ServiceException {
127         Set<String> ricUrls = new HashSet<>();
128         Set<String> ricNames = new HashSet<>();
129         for (RicConfig ric : ricConfigs) {
130             if (!ricUrls.add(ric.getBaseUrl())) {
131                 throw new ServiceException("Configuration error, more than one RIC URL: " + ric.getBaseUrl());
132             }
133             if (!ricNames.add(ric.getRicId())) {
134                 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.getRicId());
135             }
136             if (!ric.getControllerName().isEmpty() && controllerConfigs.get(ric.getControllerName()) == null) {
137                 throw new ServiceException(
138                         "Configuration error, controller configuration not found: " + ric.getControllerName());
139             }
140         }
141     }
142
143     private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
144         List<RicConfig> result = new ArrayList<>();
145         for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
146             JsonObject ricJsonObj = ricElem.getAsJsonObject();
147             RicConfig ricConfig = RicConfig.builder() //
148                     .ricId(get(ricJsonObj, "name", "id", "ricId").getAsString()) //
149                     .baseUrl(get(ricJsonObj, "baseUrl").getAsString()) //
150                     .managedElementIds(parseManagedElementIds(get(ricJsonObj, "managedElementIds").getAsJsonArray())) //
151                     .controllerName(getString(ricJsonObj, CONTROLLER, ""))
152                     .customAdapterClass(getString(ricJsonObj, "customAdapterClass", "")) //
153                     .build();
154             if (!ricConfig.getBaseUrl().isEmpty()) {
155                 result.add(ricConfig);
156             } else {
157                 logger.error("RIC configuration error {}, baseUrl is empty", ricConfig.getRicId());
158             }
159         }
160         return result;
161     }
162
163     String getString(JsonObject obj, String name, String defaultValue) {
164         JsonElement elem = obj.get(name);
165         if (elem != null) {
166             return elem.getAsString();
167         }
168         return defaultValue;
169     }
170
171     Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
172         if (config.get(CONTROLLER) == null) {
173             return new HashMap<>();
174         }
175         Map<String, ControllerConfig> result = new HashMap<>();
176         for (JsonElement element : getAsJsonArray(config, CONTROLLER)) {
177             JsonObject controllerAsJson = element.getAsJsonObject();
178             ControllerConfig controllerConfig = ControllerConfig.builder() //
179                     .name(get(controllerAsJson, "name").getAsString()) //
180                     .baseUrl(get(controllerAsJson, "baseUrl").getAsString()) //
181                     .password(get(controllerAsJson, "password").getAsString()) //
182                     .userName(get(controllerAsJson, "userName").getAsString()) // )
183                     .build();
184
185             if (result.put(controllerConfig.getName(), controllerConfig) != null) {
186                 throw new ServiceException(
187                         "Configuration error, more than one controller with name: " + controllerConfig.getName());
188             }
189         }
190         return result;
191     }
192
193     private List<String> parseManagedElementIds(JsonArray asJsonObject) {
194         Iterator<JsonElement> iterator = asJsonObject.iterator();
195         List<String> managedElementIds = new ArrayList<>();
196         while (iterator.hasNext()) {
197             managedElementIds.add(iterator.next().getAsString());
198
199         }
200         return managedElementIds;
201     }
202
203     private static JsonElement get(JsonObject obj, String... alternativeMemberNames) throws ServiceException {
204         for (String memberName : alternativeMemberNames) {
205             JsonElement elem = obj.get(memberName);
206             if (elem != null) {
207                 return elem;
208             }
209         }
210         throw new ServiceException("Could not find member: " + Arrays.toString(alternativeMemberNames) + " in: " + obj);
211     }
212
213     private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
214         return get(obj, memberName).getAsJsonArray();
215     }
216
217     private static String getAsString(JsonObject obj, String memberName) throws ServiceException {
218         return get(obj, memberName).getAsString();
219     }
220 }