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.Iterator;
31 import java.util.List;
32 import java.util.Map.Entry;
33 import java.util.Properties;
36 import javax.validation.constraints.NotNull;
38 import org.immutables.gson.Gson;
39 import org.immutables.value.Value;
40 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
41 import org.oransc.policyagent.exceptions.ServiceException;
42 import org.springframework.http.MediaType;
45 * Parser for the Json representing of the component configuration.
47 public class ApplicationConfigParser {
49 private static final String CONFIG = "config";
53 public interface ConfigParserResult {
54 List<RicConfig> ricConfigs();
56 Properties dmaapPublisherConfig();
58 Properties dmaapConsumerConfig();
61 public ConfigParserResult parse(JsonObject root) throws ServiceException {
63 Properties dmaapPublisherConfig = new Properties();
64 Properties dmaapConsumerConfig = new Properties();
66 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
67 List<RicConfig> ricConfigs = parseRics(agentConfigJson);
69 JsonObject json = agentConfigJson.getAsJsonObject("streams_publishes");
71 dmaapPublisherConfig = parseDmaapConfig(json);
74 json = agentConfigJson.getAsJsonObject("streams_subscribes");
76 dmaapConsumerConfig = parseDmaapConfig(json);
79 return ImmutableConfigParserResult.builder() //
80 .dmaapConsumerConfig(dmaapConsumerConfig) //
81 .dmaapPublisherConfig(dmaapPublisherConfig) //
82 .ricConfigs(ricConfigs) //
86 private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
87 List<RicConfig> result = new ArrayList<>();
88 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
89 JsonObject ricAsJson = ricElem.getAsJsonObject();
90 ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
91 .name(ricAsJson.get("name").getAsString()) //
92 .baseUrl(ricAsJson.get("baseUrl").getAsString()) //
93 .managedElementIds(parseManagedElementIds(ricAsJson.get("managedElementIds").getAsJsonArray())) //
95 result.add(ricConfig);
100 private List<String> parseManagedElementIds(JsonArray asJsonObject) {
101 Iterator<JsonElement> iterator = asJsonObject.iterator();
102 List<String> managedElementIds = new ArrayList<>();
103 while (iterator.hasNext()) {
104 managedElementIds.add(iterator.next().getAsString());
107 return managedElementIds;
110 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
111 JsonElement elem = obj.get(memberName);
113 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
118 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
119 return get(obj, memberName).getAsJsonArray();
122 private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
123 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
124 if (streamConfigEntries.size() != 1) {
125 throw new ServiceException(
126 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
128 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
129 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
130 String topicUrl = getAsString(dmaapInfo, "topic_url");
133 Properties dmaapProps = new Properties();
134 URL url = new URL(topicUrl);
136 String userName = "";
137 if (url.getUserInfo() != null) {
138 String[] userInfo = url.getUserInfo().split(":");
139 userName = userInfo[0];
140 passwd = userInfo[1];
142 String urlPath = url.getPath();
143 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
145 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
146 dmaapProps.put("topic", path.dmaapTopicName);
147 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
148 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
149 dmaapProps.put("userName", userName);
150 dmaapProps.put("password", passwd);
151 dmaapProps.put("group", path.consumerGroup);
152 dmaapProps.put("id", path.consumerId);
153 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
154 dmaapProps.put("timeout", 15000);
155 dmaapProps.put("limit", 100);
156 dmaapProps.put("maxBatchSize", "10");
157 dmaapProps.put("maxAgeMs", "10000");
158 dmaapProps.put("compress", true);
159 dmaapProps.put("MessageSentThreadOccurance", "2");
161 } catch (MalformedURLException e) {
162 throw new ServiceException("Could not parse the URL", e);
167 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
168 return get(obj, memberName).getAsString();
171 private class DmaapUrlPath {
172 final String dmaapTopicName;
173 final String consumerGroup;
174 final String consumerId;
176 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
177 this.dmaapTopicName = dmaapTopicName;
178 this.consumerGroup = consumerGroup;
179 this.consumerId = consumerId;
183 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
184 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
185 if (!(tokens.length == 3 ^ tokens.length == 5)) {
186 throw new ServiceException("The path has incorrect syntax: " + urlPath);
189 final String dmaapTopicName = tokens[2]; // /events/A1-P
190 String consumerGroup = ""; // users
191 String consumerId = ""; // sdnc1
192 if (tokens.length == 5) {
193 consumerGroup = tokens[3];
194 consumerId = tokens[4];
196 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);