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;
26 import java.net.MalformedURLException;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.Properties;
34 import javax.validation.constraints.NotNull;
36 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
37 import org.oransc.policyagent.exceptions.ServiceException;
38 import org.springframework.http.MediaType;
40 public class ApplicationConfigParser {
42 private static final String CONFIG = "config";
45 private List<RicConfig> ricConfigs;
47 private Properties dmaapPublisherConfig = new Properties();
49 private Properties dmaapConsumerConfig = new Properties();
51 public void parse(JsonObject root) throws ServiceException {
52 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
53 ricConfigs = parseRics(agentConfigJson);
55 JsonObject json = agentConfigJson.getAsJsonObject("streams_publishes");
57 this.dmaapPublisherConfig = parseDmaapConfig(json);
60 json = agentConfigJson.getAsJsonObject("streams_subscribes");
62 this.dmaapConsumerConfig = parseDmaapConfig(json);
67 private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
68 List<RicConfig> result = new ArrayList<>();
69 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
70 JsonObject ricAsJson = ricElem.getAsJsonObject();
71 ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
72 .name(ricAsJson.get("name").getAsString()) //
73 .baseUrl(ricAsJson.get("baseUrl").getAsString()) //
74 .managedElementIds(parseManagedElementIds(ricAsJson.get("managedElementIds").getAsJsonArray())) //
76 result.add(ricConfig);
81 private List<String> parseManagedElementIds(JsonArray asJsonObject) {
82 Iterator<JsonElement> iterator = asJsonObject.iterator();
83 List<String> managedElementIds = new ArrayList<>();
84 while (iterator.hasNext()) {
85 managedElementIds.add(iterator.next().getAsString());
88 return managedElementIds;
91 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
92 JsonElement elem = obj.get(memberName);
94 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
99 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
100 return get(obj, memberName).getAsJsonArray();
103 private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
104 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
105 if (streamConfigEntries.size() != 1) {
106 throw new ServiceException(
107 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
109 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
110 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
111 String topicUrl = getAsString(dmaapInfo, "topic_url");
114 Properties dmaapProps = new Properties();
115 URL url = new URL(topicUrl);
117 String userName = "";
118 if (url.getUserInfo() != null) {
119 String[] userInfo = url.getUserInfo().split(":");
120 userName = userInfo[0];
121 passwd = userInfo[1];
123 String urlPath = url.getPath();
124 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
126 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
127 dmaapProps.put("topic", path.dmaapTopicName);
128 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
129 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
130 dmaapProps.put("userName", userName);
131 dmaapProps.put("password", passwd);
132 dmaapProps.put("group", path.consumerGroup);
133 dmaapProps.put("id", path.consumerId);
134 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
135 dmaapProps.put("timeout", 15000);
136 dmaapProps.put("limit", 100);
137 dmaapProps.put("maxBatchSize", "10");
138 dmaapProps.put("maxAgeMs", "10000");
139 dmaapProps.put("compress", true);
140 dmaapProps.put("MessageSentThreadOccurance", "2");
142 } catch (MalformedURLException e) {
143 throw new ServiceException("Could not parse the URL", e);
148 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
149 return get(obj, memberName).getAsString();
152 private class DmaapUrlPath {
153 final String dmaapTopicName;
154 final String consumerGroup;
155 final String consumerId;
157 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
158 this.dmaapTopicName = dmaapTopicName;
159 this.consumerGroup = consumerGroup;
160 this.consumerId = consumerId;
164 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
165 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
166 if (!(tokens.length == 3 ^ tokens.length == 5)) {
167 throw new ServiceException("The path has incorrect syntax: " + urlPath);
170 final String dmaapTopicName = tokens[2]; // /events/A1-P
171 String consumerGroup = ""; // users
172 String consumerId = ""; // sdnc1
173 if (tokens.length == 5) {
174 consumerGroup = tokens[3];
175 consumerId = tokens[4];
177 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);