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;
35 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
36 import org.oransc.policyagent.exceptions.ServiceException;
37 import org.springframework.http.MediaType;
39 public class ApplicationConfigParser {
41 private static final String CONFIG = "config";
43 private static Gson gson = new GsonBuilder() //
47 private Vector<RicConfig> ricConfig;
48 private Properties dmaapPublisherConfig;
49 private Properties dmaapConsumerConfig;
51 public ApplicationConfigParser() {
54 public void parse(JsonObject root) throws ServiceException {
55 JsonObject ricConfigJson = root.getAsJsonObject(CONFIG);
56 ricConfig = parseRics(ricConfigJson);
57 JsonObject dmaapPublisherConfigJson = root.getAsJsonObject("streams_publishes");
58 if (dmaapPublisherConfigJson == null) {
59 dmaapPublisherConfig = new Properties();
61 dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
63 JsonObject dmaapConsumerConfigJson = root.getAsJsonObject("streams_subscribes");
64 if (dmaapConsumerConfigJson == null) {
65 dmaapConsumerConfig = new Properties();
67 dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
71 public Vector<RicConfig> getRicConfigs() {
72 return this.ricConfig;
75 public Properties getDmaapPublisherConfig() {
76 return dmaapPublisherConfig;
79 public Properties getDmaapConsumerConfig() {
80 return dmaapConsumerConfig;
83 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
84 Vector<RicConfig> result = new Vector<RicConfig>();
85 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
86 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
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 consumerCfg) throws ServiceException {
104 Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
105 if (topics.size() != 1) {
106 throw new ServiceException("Invalid configuration, number of topic must be one, config: " + topics);
108 JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
109 JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
110 String topicUrl = getAsString(dmaapInfo, "topic_url");
112 Properties dmaapProps = new Properties();
114 URL url = new URL(topicUrl);
116 String userName = "";
117 if (url.getUserInfo() != null) {
118 String[] userInfo = url.getUserInfo().split(":");
119 userName = userInfo[0];
120 passwd = userInfo[1];
122 String urlPath = url.getPath();
123 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
125 dmaapProps.put("ServiceName", url.getHost()+":"+url.getPort()+"/events");
126 dmaapProps.put("topic", path.dmaapTopicName);
127 dmaapProps.put("host", url.getHost()+":"+url.getPort());
128 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
129 dmaapProps.put("userName", userName);
130 dmaapProps.put("password", passwd);
131 dmaapProps.put("group", path.consumerGroup);
132 dmaapProps.put("id", path.consumerId);
133 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
134 dmaapProps.put("timeout", 15000);
135 dmaapProps.put("limit", 1000);
136 } catch (MalformedURLException e) {
137 throw new ServiceException("Could not parse the URL", e);
143 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
144 return get(obj, memberName).getAsString();
147 private class DmaapUrlPath {
148 final String dmaapTopicName;
149 final String consumerGroup;
150 final String consumerId;
152 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
153 this.dmaapTopicName = dmaapTopicName;
154 this.consumerGroup = consumerGroup;
155 this.consumerId = consumerId;
159 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
160 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
161 if (!(tokens.length == 3 ^ tokens.length == 5)) {
162 throw new ServiceException("The path has incorrect syntax: " + urlPath);
165 final String dmaapTopicName = tokens[2]; // /events/A1-P
166 String consumerGroup = ""; // users
167 String consumerId = ""; // sdnc1
168 if (tokens.length == 5) {
169 consumerGroup = tokens[3];
170 consumerId = tokens[4];
172 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);