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;
38 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
39 import org.oransc.policyagent.exceptions.ServiceException;
40 import org.springframework.http.MediaType;
42 public class ApplicationConfigParser {
44 private static final String CONFIG = "config";
46 private static Gson gson = new GsonBuilder() //
51 private Vector<RicConfig> ricConfigs;
53 private Properties dmaapPublisherConfig;
55 private Properties dmaapConsumerConfig;
57 public void parse(JsonObject root) throws ServiceException {
58 JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
59 ricConfigs = parseRics(agentConfigJson);
60 JsonObject dmaapPublisherConfigJson = agentConfigJson.getAsJsonObject("streams_publishes");
61 if (dmaapPublisherConfigJson == null) {
62 dmaapPublisherConfig = new Properties();
64 dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
66 JsonObject dmaapConsumerConfigJson = agentConfigJson.getAsJsonObject("streams_subscribes");
67 if (dmaapConsumerConfigJson == null) {
68 dmaapConsumerConfig = new Properties();
70 dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
74 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
75 Vector<RicConfig> result = new Vector<RicConfig>();
76 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
77 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
82 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
83 JsonElement elem = obj.get(memberName);
85 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
90 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
91 return get(obj, memberName).getAsJsonArray();
94 private Properties parseDmaapConfig(JsonObject consumerCfg) throws ServiceException {
95 Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
96 if (topics.size() != 1) {
97 throw new ServiceException("Invalid configuration, number of topic must be one, config: " + topics);
99 JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
100 JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
101 String topicUrl = getAsString(dmaapInfo, "topic_url");
103 Properties dmaapProps = new Properties();
105 URL url = new URL(topicUrl);
107 String userName = "";
108 if (url.getUserInfo() != null) {
109 String[] userInfo = url.getUserInfo().split(":");
110 userName = userInfo[0];
111 passwd = userInfo[1];
113 String urlPath = url.getPath();
114 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
116 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
117 dmaapProps.put("topic", path.dmaapTopicName);
118 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
119 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
120 dmaapProps.put("userName", userName);
121 dmaapProps.put("password", passwd);
122 dmaapProps.put("group", path.consumerGroup);
123 dmaapProps.put("id", path.consumerId);
124 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
125 dmaapProps.put("timeout", 15000);
126 dmaapProps.put("limit", 1000);
127 } catch (MalformedURLException e) {
128 throw new ServiceException("Could not parse the URL", e);
134 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
135 return get(obj, memberName).getAsString();
138 private class DmaapUrlPath {
139 final String dmaapTopicName;
140 final String consumerGroup;
141 final String consumerId;
143 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
144 this.dmaapTopicName = dmaapTopicName;
145 this.consumerGroup = consumerGroup;
146 this.consumerId = consumerId;
150 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
151 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
152 if (!(tokens.length == 3 ^ tokens.length == 5)) {
153 throw new ServiceException("The path has incorrect syntax: " + urlPath);
156 final String dmaapTopicName = tokens[2]; // /events/A1-P
157 String consumerGroup = ""; // users
158 String consumerId = ""; // sdnc1
159 if (tokens.length == 5) {
160 consumerGroup = tokens[3];
161 consumerId = tokens[4];
163 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);