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;
57 private Properties dmaapConsumerConfig;
59 public void parse(JsonObject root) throws ServiceException {
60 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
61 ricConfigs = parseRics(agentConfigJson);
62 JsonObject dmaapPublisherConfigJson = agentConfigJson.getAsJsonObject("streams_publishes");
63 if (dmaapPublisherConfigJson == null) {
64 dmaapPublisherConfig = new Properties();
66 dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
68 JsonObject dmaapConsumerConfigJson = agentConfigJson.getAsJsonObject("streams_subscribes");
69 if (dmaapConsumerConfigJson == null) {
70 dmaapConsumerConfig = new Properties();
72 dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
76 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
77 Vector<RicConfig> result = new Vector<RicConfig>();
78 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
79 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
84 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
85 JsonElement elem = obj.get(memberName);
87 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
92 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
93 return get(obj, memberName).getAsJsonArray();
96 private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
97 Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
98 if (streamConfigEntries.size() != 1) {
99 throw new ServiceException(
100 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
102 JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
103 JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
104 String topicUrl = getAsString(dmaapInfo, "topic_url");
106 Properties dmaapProps = new Properties();
108 URL url = new URL(topicUrl);
110 String userName = "";
111 if (url.getUserInfo() != null) {
112 String[] userInfo = url.getUserInfo().split(":");
113 userName = userInfo[0];
114 passwd = userInfo[1];
116 String urlPath = url.getPath();
117 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
119 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
120 dmaapProps.put("topic", path.dmaapTopicName);
121 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
122 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
123 dmaapProps.put("userName", userName);
124 dmaapProps.put("password", passwd);
125 dmaapProps.put("group", path.consumerGroup);
126 dmaapProps.put("id", path.consumerId);
127 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
128 dmaapProps.put("timeout", 15000);
129 dmaapProps.put("limit", 1000);
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);