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;
40 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
41 import org.oransc.policyagent.exceptions.ServiceException;
42 import org.springframework.http.MediaType;
44 public class ApplicationConfigParser {
46 private static final String CONFIG = "config";
49 private List<RicConfig> ricConfigs;
51 private Properties dmaapPublisherConfig = new Properties();
53 private Properties dmaapConsumerConfig = new Properties();
55 public void parse(JsonObject root) throws ServiceException {
56 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
57 ricConfigs = parseRics(agentConfigJson);
59 JsonObject json = agentConfigJson.getAsJsonObject("streams_publishes");
61 this.dmaapPublisherConfig = parseDmaapConfig(json);
64 json = agentConfigJson.getAsJsonObject("streams_subscribes");
66 this.dmaapConsumerConfig = parseDmaapConfig(json);
71 private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
72 List<RicConfig> result = new ArrayList<>();
73 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
74 JsonObject ricAsJson = ricElem.getAsJsonObject();
75 ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
76 .name(ricAsJson.get("name").getAsString()) //
77 .baseUrl(ricAsJson.get("baseUrl").getAsString()) //
78 .managedElementIds(parseManagedElementIds(ricAsJson.get("managedElementIds").getAsJsonArray())) //
80 result.add(ricConfig);
85 private List<String> parseManagedElementIds(JsonArray asJsonObject) {
86 Iterator<JsonElement> iterator = asJsonObject.iterator();
87 List<String> managedElementIds = new ArrayList<>();
88 while (iterator.hasNext()) {
89 managedElementIds.add(iterator.next().getAsString());
92 return managedElementIds;
95 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
96 JsonElement elem = obj.get(memberName);
98 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
103 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
104 return get(obj, memberName).getAsJsonArray();
107 private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
108 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
109 if (streamConfigEntries.size() != 1) {
110 throw new ServiceException(
111 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
113 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
114 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
115 String topicUrl = getAsString(dmaapInfo, "topic_url");
118 Properties dmaapProps = new Properties();
119 URL url = new URL(topicUrl);
121 String userName = "";
122 if (url.getUserInfo() != null) {
123 String[] userInfo = url.getUserInfo().split(":");
124 userName = userInfo[0];
125 passwd = userInfo[1];
127 String urlPath = url.getPath();
128 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
130 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
131 dmaapProps.put("topic", path.dmaapTopicName);
132 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
133 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
134 dmaapProps.put("userName", userName);
135 dmaapProps.put("password", passwd);
136 dmaapProps.put("group", path.consumerGroup);
137 dmaapProps.put("id", path.consumerId);
138 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
139 dmaapProps.put("timeout", 15000);
140 dmaapProps.put("limit", 100);
141 dmaapProps.put("maxBatchSize", "10");
142 dmaapProps.put("maxAgeMs", "10000");
143 dmaapProps.put("compress", true);
144 dmaapProps.put("MessageSentThreadOccurance", "2");
146 } catch (MalformedURLException e) {
147 throw new ServiceException("Could not parse the URL", e);
152 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
153 return get(obj, memberName).getAsString();
156 private class DmaapUrlPath {
157 final String dmaapTopicName;
158 final String consumerGroup;
159 final String consumerId;
161 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
162 this.dmaapTopicName = dmaapTopicName;
163 this.consumerGroup = consumerGroup;
164 this.consumerId = consumerId;
168 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
169 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
170 if (!(tokens.length == 3 ^ tokens.length == 5)) {
171 throw new ServiceException("The path has incorrect syntax: " + urlPath);
174 final String dmaapTopicName = tokens[2]; // /events/A1-P
175 String consumerGroup = ""; // users
176 String consumerId = ""; // sdnc1
177 if (tokens.length == 5) {
178 consumerGroup = tokens[3];
179 consumerId = tokens[4];
181 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);