Add A1 controller client in policy-agent
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / ApplicationConfigParser.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.configuration;
22
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
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.util.Map.Entry;
32 import java.util.Properties;
33 import java.util.Set;
34 import java.util.Vector;
35
36 import javax.validation.constraints.NotNull;
37
38 import org.oransc.policyagent.exceptions.ServiceException;
39
40 public class ApplicationConfigParser {
41
42     private static final String CONFIG = "config";
43
44     private static Gson gson = new GsonBuilder() //
45         .serializeNulls() //
46         .create(); //
47
48     private Vector<RicConfig> ricConfig;
49     private Properties dmaapConsumerConfig;
50
51     public ApplicationConfigParser() {
52     }
53
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);
59     }
60
61     public Vector<RicConfig> getRicConfigs() {
62         return this.ricConfig;
63     }
64
65     public Properties getDmaapConsumerConfig() {
66         return dmaapConsumerConfig;
67     }
68
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));
73         }
74         return result;
75     }
76
77     private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
78         JsonElement elem = obj.get(memberName);
79         if (elem == null) {
80             throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
81         }
82         return elem;
83     }
84
85     private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
86         return get(obj, memberName).getAsJsonArray();
87     }
88
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);
93         }
94         JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
95         JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
96         String topicUrl = getAsString(dmaapInfo, "topic_url");
97
98         Properties dmaapProps = new Properties();
99         try {
100             URL url = new URL(topicUrl);
101             String passwd = "";
102             String userName = "";
103             if (url.getUserInfo() != null) {
104                 String[] userInfo = url.getUserInfo().split(":");
105                 userName = userInfo[0];
106                 passwd = userInfo[1];
107             }
108             String urlPath = url.getPath();
109             DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
110
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);
122         }
123
124         return dmaapProps;
125     }
126
127     private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
128         return get(obj, memberName).getAsString();
129     }
130
131     private class DmaapConsumerUrlPath {
132         final String dmaapTopicName;
133         final String consumerGroup;
134         final String consumerId;
135
136         DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
137             this.dmaapTopicName = dmaapTopicName;
138             this.consumerGroup = consumerGroup;
139             this.consumerId = consumerId;
140         }
141     }
142
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);
147         }
148
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);
153     }
154 }