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.oransc.policyagent.exceptions.ServiceException;
40 public class ApplicationConfigParser {
42 private static final String CONFIG = "config";
44 private static Gson gson = new GsonBuilder() //
48 private Vector<RicConfig> ricConfig;
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 dmaapConfigJson = root.getAsJsonObject("streams_subscribes");
58 dmaapConsumerConfig = parseDmaapConsumerConfig(dmaapConfigJson);
61 public Vector<RicConfig> getRicConfigs() {
62 return this.ricConfig;
65 public Properties getDmaapConsumerConfig() {
66 return dmaapConsumerConfig;
69 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
70 Vector<RicConfig> result = new Vector<RicConfig>();
71 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
72 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
77 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
78 JsonElement elem = obj.get(memberName);
80 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
85 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
86 return get(obj, memberName).getAsJsonArray();
89 private Properties parseDmaapConsumerConfig(JsonObject consumerCfg) throws ServiceException {
90 Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
91 if (topics.size() != 1) {
92 throw new ServiceException("Invalid configuration, number of topic must be one, config: " + topics);
94 JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
95 JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
96 String topicUrl = getAsString(dmaapInfo, "topic_url");
98 Properties dmaapProps = new Properties();
100 URL url = new URL(topicUrl);
102 String userName = "";
103 if (url.getUserInfo() != null) {
104 String[] userInfo = url.getUserInfo().split(":");
105 userName = userInfo[0];
106 passwd = userInfo[1];
108 String urlPath = url.getPath();
109 DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
111 dmaapProps.put("port", url.getPort());
112 dmaapProps.put("server", url.getHost());
113 dmaapProps.put("topic", path.dmaapTopicName);
114 dmaapProps.put("consumerGroup", path.consumerGroup);
115 dmaapProps.put("consumerInstance", path.consumerId);
116 dmaapProps.put("fetchTimeout", 15000);
117 dmaapProps.put("fetchLimit", 1000);
118 dmaapProps.put("userName", userName);
119 dmaapProps.put("password", passwd);
120 } catch (MalformedURLException e) {
121 throw new ServiceException("Could not parse the URL", e);
127 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
128 return get(obj, memberName).getAsString();
131 private class DmaapConsumerUrlPath {
132 final String dmaapTopicName;
133 final String consumerGroup;
134 final String consumerId;
136 DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
137 this.dmaapTopicName = dmaapTopicName;
138 this.consumerGroup = consumerGroup;
139 this.consumerId = consumerId;
143 private DmaapConsumerUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
144 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
145 if (tokens.length != 5) {
146 throw new ServiceException("The path has incorrect syntax: " + urlPath);
149 final String dmaapTopicName = tokens[1] + "/" + tokens[2]; // /events/A1-P
150 final String consumerGroup = tokens[3]; // users
151 final String consumerId = tokens[4]; // sdnc1
152 return new DmaapConsumerUrlPath(dmaapTopicName, consumerGroup, consumerId);