2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent.configuration;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
27 import java.net.MalformedURLException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.List;
35 import java.util.Map.Entry;
36 import java.util.Properties;
39 import javax.validation.constraints.NotNull;
41 import org.immutables.gson.Gson;
42 import org.immutables.value.Value;
43 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
44 import org.oransc.policyagent.exceptions.ServiceException;
45 import org.springframework.http.MediaType;
48 * Parser for the Json representing of the component configuration.
50 public class ApplicationConfigParser {
52 private static final String CONFIG = "config";
53 private static final String CONTROLLER = "controller";
57 public interface ConfigParserResult {
58 List<RicConfig> ricConfigs();
60 Properties dmaapPublisherConfig();
62 Properties dmaapConsumerConfig();
64 Map<String, ControllerConfig> controllerConfigs();
67 public ConfigParserResult parse(JsonObject root) throws ServiceException {
69 Properties dmaapPublisherConfig = new Properties();
70 Properties dmaapConsumerConfig = new Properties();
72 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
73 List<RicConfig> ricConfigs = parseRics(agentConfigJson);
74 Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(agentConfigJson);
76 JsonObject json = agentConfigJson.getAsJsonObject("streams_publishes");
78 dmaapPublisherConfig = parseDmaapConfig(json);
81 json = agentConfigJson.getAsJsonObject("streams_subscribes");
83 dmaapConsumerConfig = parseDmaapConfig(json);
86 checkConfigurationConsistency(ricConfigs, controllerConfigs);
88 return ImmutableConfigParserResult.builder() //
89 .dmaapConsumerConfig(dmaapConsumerConfig) //
90 .dmaapPublisherConfig(dmaapPublisherConfig) //
91 .ricConfigs(ricConfigs) //
92 .controllerConfigs(controllerConfigs) //
96 private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
97 Map<String, ControllerConfig> controllerConfigs) throws ServiceException {
98 Set<String> ricUrls = new HashSet<>();
99 Set<String> ricNames = new HashSet<>();
100 for (RicConfig ric : ricConfigs) {
101 if (!ricUrls.add(ric.baseUrl())) {
102 throw new ServiceException("Configuration error, more than one RIC URL: " + ric.baseUrl());
104 if (!ricNames.add(ric.name())) {
105 throw new ServiceException("Configuration error, more than one RIC with name: " + ric.name());
107 if (!ric.controllerName().isEmpty() && controllerConfigs.get(ric.controllerName()) == null) {
108 throw new ServiceException(
109 "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 ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
122 .name(get(ricAsJson, "name").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 memberName) throws ServiceException {
165 JsonElement elem = obj.get(memberName);
167 throw new ServiceException("Could not find member: '" + memberName + "' in: " + obj);
172 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
173 return get(obj, memberName).getAsJsonArray();
176 private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
177 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
178 if (streamConfigEntries.size() != 1) {
179 throw new ServiceException(
180 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
182 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
183 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
184 String topicUrl = getAsString(dmaapInfo, "topic_url");
187 Properties dmaapProps = new Properties();
188 URL url = new URL(topicUrl);
190 String userName = "";
191 if (url.getUserInfo() != null) {
192 String[] userInfo = url.getUserInfo().split(":");
193 userName = userInfo[0];
194 passwd = userInfo[1];
196 String urlPath = url.getPath();
197 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
199 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
200 dmaapProps.put("topic", path.dmaapTopicName);
201 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
202 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
203 dmaapProps.put("userName", userName);
204 dmaapProps.put("password", passwd);
205 dmaapProps.put("group", path.consumerGroup);
206 dmaapProps.put("id", path.consumerId);
207 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
208 dmaapProps.put("timeout", 15000);
209 dmaapProps.put("limit", 100);
210 dmaapProps.put("maxBatchSize", "10");
211 dmaapProps.put("maxAgeMs", "10000");
212 dmaapProps.put("compress", true);
213 dmaapProps.put("MessageSentThreadOccurance", "2");
215 } catch (MalformedURLException e) {
216 throw new ServiceException("Could not parse the URL", e);
220 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
221 return get(obj, memberName).getAsString();
224 private class DmaapUrlPath {
225 final String dmaapTopicName;
226 final String consumerGroup;
227 final String consumerId;
229 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
230 this.dmaapTopicName = dmaapTopicName;
231 this.consumerGroup = consumerGroup;
232 this.consumerId = consumerId;
236 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
237 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
238 if (!(tokens.length == 3 ^ tokens.length == 5)) {
239 throw new ServiceException("The path has incorrect syntax: " + urlPath);
242 final String dmaapTopicName = tokens[2]; // /events/A1-P
243 String consumerGroup = ""; // users
244 String consumerId = ""; // sdnc1
245 if (tokens.length == 5) {
246 consumerGroup = tokens[3];
247 consumerId = tokens[4];
249 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);