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.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
29 import java.net.MalformedURLException;
31 import java.util.Map.Entry;
32 import java.util.Properties;
34 import java.util.Vector;
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";
48 private static Gson gson = new GsonBuilder() //
53 private Vector<RicConfig> ricConfigs;
55 private Properties dmaapPublisherConfig = new Properties();
57 private Properties dmaapConsumerConfig = new Properties();
59 public void parse(JsonObject root) throws ServiceException {
60 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
61 ricConfigs = parseRics(agentConfigJson);
63 JsonObject json = agentConfigJson.getAsJsonObject("streams_publishes");
65 this.dmaapPublisherConfig = parseDmaapConfig(json);
68 json = agentConfigJson.getAsJsonObject("streams_subscribes");
70 this.dmaapConsumerConfig = parseDmaapConfig(json);
75 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
76 Vector<RicConfig> result = new Vector<RicConfig>();
77 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
78 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
83 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
84 JsonElement elem = obj.get(memberName);
86 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
91 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
92 return get(obj, memberName).getAsJsonArray();
95 private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
96 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
97 if (streamConfigEntries.size() != 1) {
98 throw new ServiceException(
99 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
101 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
102 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
103 String topicUrl = getAsString(dmaapInfo, "topic_url");
106 Properties dmaapProps = new Properties();
107 URL url = new URL(topicUrl);
109 String userName = "";
110 if (url.getUserInfo() != null) {
111 String[] userInfo = url.getUserInfo().split(":");
112 userName = userInfo[0];
113 passwd = userInfo[1];
115 String urlPath = url.getPath();
116 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
118 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
119 dmaapProps.put("topic", path.dmaapTopicName);
120 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
121 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
122 dmaapProps.put("userName", userName);
123 dmaapProps.put("password", passwd);
124 dmaapProps.put("group", path.consumerGroup);
125 dmaapProps.put("id", path.consumerId);
126 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
127 dmaapProps.put("timeout", 15000);
128 dmaapProps.put("limit", 100);
129 dmaapProps.put("maxBatchSize", "10");
130 dmaapProps.put("maxAgeMs", "10000");
131 dmaapProps.put("compress", true);
132 dmaapProps.put("MessageSentThreadOccurance", "2");
134 } catch (MalformedURLException e) {
135 throw new ServiceException("Could not parse the URL", e);
140 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
141 return get(obj, memberName).getAsString();
144 private class DmaapUrlPath {
145 final String dmaapTopicName;
146 final String consumerGroup;
147 final String consumerId;
149 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
150 this.dmaapTopicName = dmaapTopicName;
151 this.consumerGroup = consumerGroup;
152 this.consumerId = consumerId;
156 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
157 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
158 if (!(tokens.length == 3 ^ tokens.length == 5)) {
159 throw new ServiceException("The path has incorrect syntax: " + urlPath);
162 final String dmaapTopicName = tokens[2]; // /events/A1-P
163 String consumerGroup = ""; // users
164 String consumerId = ""; // sdnc1
165 if (tokens.length == 5) {
166 consumerGroup = tokens[3];
167 consumerId = tokens[4];
169 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);