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;
28 import java.net.MalformedURLException;
30 import java.util.Map.Entry;
31 import java.util.Properties;
33 import java.util.Vector;
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";
44 private static Gson gson = new GsonBuilder() //
49 private Vector<RicConfig> ricConfigs;
51 private Properties dmaapPublisherConfig;
53 private Properties dmaapConsumerConfig;
55 public void parse(JsonObject root) throws ServiceException {
56 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
57 ricConfigs = parseRics(agentConfigJson);
58 JsonObject dmaapPublisherConfigJson = agentConfigJson.getAsJsonObject("streams_publishes");
59 if (dmaapPublisherConfigJson == null) {
60 dmaapPublisherConfig = new Properties();
62 dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
64 JsonObject dmaapConsumerConfigJson = agentConfigJson.getAsJsonObject("streams_subscribes");
65 if (dmaapConsumerConfigJson == null) {
66 dmaapConsumerConfig = new Properties();
68 dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
72 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
73 Vector<RicConfig> result = new Vector<RicConfig>();
74 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
75 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
80 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
81 JsonElement elem = obj.get(memberName);
83 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
88 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
89 return get(obj, memberName).getAsJsonArray();
92 private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
93 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
94 if (streamConfigEntries.size() != 1) {
95 throw new ServiceException(
96 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
98 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
99 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
100 String topicUrl = getAsString(dmaapInfo, "topic_url");
102 Properties dmaapProps = new Properties();
104 URL url = new URL(topicUrl);
106 String userName = "";
107 if (url.getUserInfo() != null) {
108 String[] userInfo = url.getUserInfo().split(":");
109 userName = userInfo[0];
110 passwd = userInfo[1];
112 String urlPath = url.getPath();
113 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
115 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
116 dmaapProps.put("topic", path.dmaapTopicName);
117 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
118 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
119 dmaapProps.put("userName", userName);
120 dmaapProps.put("password", passwd);
121 dmaapProps.put("group", path.consumerGroup);
122 dmaapProps.put("id", path.consumerId);
123 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
124 dmaapProps.put("timeout", 15000);
125 dmaapProps.put("limit", 100);
126 dmaapProps.put("maxBatchSize", "10");
127 dmaapProps.put("maxAgeMs", "10000");
128 dmaapProps.put("compress", true);
129 dmaapProps.put("MessageSentThreadOccurance", "2");
130 } catch (MalformedURLException e) {
131 throw new ServiceException("Could not parse the URL", e);
137 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
138 return get(obj, memberName).getAsString();
141 private class DmaapUrlPath {
142 final String dmaapTopicName;
143 final String consumerGroup;
144 final String consumerId;
146 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
147 this.dmaapTopicName = dmaapTopicName;
148 this.consumerGroup = consumerGroup;
149 this.consumerId = consumerId;
153 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
154 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
155 if (!(tokens.length == 3 ^ tokens.length == 5)) {
156 throw new ServiceException("The path has incorrect syntax: " + urlPath);
159 final String dmaapTopicName = tokens[2]; // /events/A1-P
160 String consumerGroup = ""; // users
161 String consumerId = ""; // sdnc1
162 if (tokens.length == 5) {
163 consumerGroup = tokens[3];
164 consumerId = tokens[4];
166 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);