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() //
50 private Vector<RicConfig> ricConfig;
51 private Properties dmaapPublisherConfig;
52 private Properties dmaapConsumerConfig;
54 public ApplicationConfigParser() {
57 public void parse(JsonObject root) throws ServiceException {
58 JsonObject ricConfigJson = root.getAsJsonObject(CONFIG);
59 ricConfig = parseRics(ricConfigJson);
60 JsonObject dmaapPublisherConfigJson = root.getAsJsonObject("streams_publishes");
61 if (dmaapPublisherConfigJson == null) {
62 dmaapPublisherConfig = new Properties();
64 dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
66 JsonObject dmaapConsumerConfigJson = root.getAsJsonObject("streams_subscribes");
67 if (dmaapConsumerConfigJson == null) {
68 dmaapConsumerConfig = new Properties();
70 dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
74 public Vector<RicConfig> getRicConfigs() {
75 return this.ricConfig;
78 public Properties getDmaapPublisherConfig() {
79 return dmaapPublisherConfig;
82 public Properties getDmaapConsumerConfig() {
83 return dmaapConsumerConfig;
86 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
87 Vector<RicConfig> result = new Vector<RicConfig>();
88 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
89 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
94 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
95 JsonElement elem = obj.get(memberName);
97 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
102 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
103 return get(obj, memberName).getAsJsonArray();
106 private Properties parseDmaapConfig(JsonObject consumerCfg) throws ServiceException {
107 Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
108 if (topics.size() != 1) {
109 throw new ServiceException("Invalid configuration, number of topic must be one, config: " + topics);
111 JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
112 JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
113 String topicUrl = getAsString(dmaapInfo, "topic_url");
115 Properties dmaapProps = new Properties();
117 URL url = new URL(topicUrl);
119 String userName = "";
120 if (url.getUserInfo() != null) {
121 String[] userInfo = url.getUserInfo().split(":");
122 userName = userInfo[0];
123 passwd = userInfo[1];
125 String urlPath = url.getPath();
126 DmaapUrlPath path = parseDmaapUrlPath(urlPath);
128 dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
129 dmaapProps.put("topic", path.dmaapTopicName);
130 dmaapProps.put("host", url.getHost() + ":" + url.getPort());
131 dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
132 dmaapProps.put("userName", userName);
133 dmaapProps.put("password", passwd);
134 dmaapProps.put("group", path.consumerGroup);
135 dmaapProps.put("id", path.consumerId);
136 dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
137 dmaapProps.put("timeout", 15000);
138 dmaapProps.put("limit", 1000);
139 } catch (MalformedURLException e) {
140 throw new ServiceException("Could not parse the URL", e);
146 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
147 return get(obj, memberName).getAsString();
150 private class DmaapUrlPath {
151 final String dmaapTopicName;
152 final String consumerGroup;
153 final String consumerId;
155 DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
156 this.dmaapTopicName = dmaapTopicName;
157 this.consumerGroup = consumerGroup;
158 this.consumerId = consumerId;
162 private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
163 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
164 if (!(tokens.length == 3 ^ tokens.length == 5)) {
165 throw new ServiceException("The path has incorrect syntax: " + urlPath);
168 final String dmaapTopicName = tokens[2]; // /events/A1-P
169 String consumerGroup = ""; // users
170 String consumerId = ""; // sdnc1
171 if (tokens.length == 5) {
172 consumerGroup = tokens[3];
173 consumerId = tokens[4];
175 return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);