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.oransc.policyagent.exceptions.ServiceException;
37 public class ApplicationConfigParser {
39 private static final String CONFIG = "config";
41 private static Gson gson = new GsonBuilder() //
45 private Vector<RicConfig> ricConfig;
46 private Properties dmaapConsumerConfig;
48 public ApplicationConfigParser() {
51 public void parse(JsonObject root) throws ServiceException {
52 JsonObject ricConfigJson = root.getAsJsonObject(CONFIG);
53 ricConfig = parseRics(ricConfigJson);
54 JsonObject dmaapConfigJson = root.getAsJsonObject("streams_subscribes");
55 dmaapConsumerConfig = parseDmaapConsumerConfig(dmaapConfigJson);
58 public Vector<RicConfig> getRicConfigs() {
59 return this.ricConfig;
62 public Properties getDmaapConsumerConfig() {
63 return dmaapConsumerConfig;
66 private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
67 Vector<RicConfig> result = new Vector<RicConfig>();
68 for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
69 result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
74 private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
75 JsonElement elem = obj.get(memberName);
77 throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
82 private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
83 return get(obj, memberName).getAsJsonArray();
86 private Properties parseDmaapConsumerConfig(JsonObject consumerCfg) throws ServiceException {
87 Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
88 if (topics.size() != 1) {
89 throw new ServiceException("Invalid configuration, number of topic must be one, config: " + topics);
91 JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
92 JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
93 String topicUrl = getAsString(dmaapInfo, "topic_url");
95 Properties dmaapProps = new Properties();
97 URL url = new URL(topicUrl);
100 if (url.getUserInfo() != null) {
101 String[] userInfo = url.getUserInfo().split(":");
102 userName = userInfo[0];
103 passwd = userInfo[1];
105 String urlPath = url.getPath();
106 DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
108 dmaapProps.put("port", url.getPort());
109 dmaapProps.put("server", url.getHost());
110 dmaapProps.put("topic", path.dmaapTopicName);
111 dmaapProps.put("consumerGroup", path.consumerGroup);
112 dmaapProps.put("consumerInstance", path.consumerId);
113 dmaapProps.put("fetchTimeout", 15000);
114 dmaapProps.put("fetchLimit", 1000);
115 dmaapProps.put("userName", userName);
116 dmaapProps.put("password", passwd);
117 } catch (MalformedURLException e) {
118 throw new ServiceException("Could not parse the URL", e);
124 private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
125 return get(obj, memberName).getAsString();
128 private class DmaapConsumerUrlPath {
129 final String dmaapTopicName;
130 final String consumerGroup;
131 final String consumerId;
133 DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
134 this.dmaapTopicName = dmaapTopicName;
135 this.consumerGroup = consumerGroup;
136 this.consumerId = consumerId;
140 private DmaapConsumerUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
141 String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
142 if (tokens.length != 5) {
143 throw new ServiceException("The path has incorrect syntax: " + urlPath);
146 final String dmaapTopicName = tokens[1] + "/" + tokens[2]; // /events/A1-P
147 final String consumerGroup = tokens[3]; // users
148 final String consumerId = tokens[4]; // sdnc1
149 return new DmaapConsumerUrlPath(dmaapTopicName, consumerGroup, consumerId);