d32db7a0bd379f0aa2d5ca2bec014ad971965833
[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.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
39 import org.oransc.policyagent.exceptions.ServiceException;
40 import org.springframework.http.MediaType;
41
42 public class ApplicationConfigParser {
43
44     private static final String CONFIG = "config";
45
46     private static Gson gson = new GsonBuilder() //
47         .serializeNulls() //
48         .create(); //
49
50     private Vector<RicConfig> ricConfig;
51     private Properties dmaapPublisherConfig;
52     private Properties dmaapConsumerConfig;
53
54     public ApplicationConfigParser() {
55     }
56
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();
63         } else {
64             dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
65         }
66         JsonObject dmaapConsumerConfigJson = root.getAsJsonObject("streams_subscribes");
67         if (dmaapConsumerConfigJson == null) {
68             dmaapConsumerConfig = new Properties();
69         } else {
70             dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
71         }
72     }
73
74     public Vector<RicConfig> getRicConfigs() {
75         return this.ricConfig;
76     }
77
78     public Properties getDmaapPublisherConfig() {
79         return dmaapPublisherConfig;
80     }
81
82     public Properties getDmaapConsumerConfig() {
83         return dmaapConsumerConfig;
84     }
85
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));
90         }
91         return result;
92     }
93
94     private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
95         JsonElement elem = obj.get(memberName);
96         if (elem == null) {
97             throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
98         }
99         return elem;
100     }
101
102     private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
103         return get(obj, memberName).getAsJsonArray();
104     }
105
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);
110         }
111         JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
112         JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
113         String topicUrl = getAsString(dmaapInfo, "topic_url");
114
115         Properties dmaapProps = new Properties();
116         try {
117             URL url = new URL(topicUrl);
118             String passwd = "";
119             String userName = "";
120             if (url.getUserInfo() != null) {
121                 String[] userInfo = url.getUserInfo().split(":");
122                 userName = userInfo[0];
123                 passwd = userInfo[1];
124             }
125             String urlPath = url.getPath();
126             DmaapUrlPath path = parseDmaapUrlPath(urlPath);
127
128             dmaapProps.put("ServiceName", url.getHost());
129             dmaapProps.put("topic", path.dmaapTopicName);
130             dmaapProps.put("host", url.getHost());
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             dmaapProps.put("port", url.getPort());
140         } catch (MalformedURLException e) {
141             throw new ServiceException("Could not parse the URL", e);
142         }
143
144         return dmaapProps;
145     }
146
147     private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
148         return get(obj, memberName).getAsString();
149     }
150
151     private class DmaapUrlPath {
152         final String dmaapTopicName;
153         final String consumerGroup;
154         final String consumerId;
155
156         DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
157             this.dmaapTopicName = dmaapTopicName;
158             this.consumerGroup = consumerGroup;
159             this.consumerId = consumerId;
160         }
161     }
162
163     private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
164         String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
165         if (!(tokens.length == 3 ^ tokens.length == 5)) {
166             throw new ServiceException("The path has incorrect syntax: " + urlPath);
167         }
168
169         final String dmaapTopicName = tokens[1] + "/" + tokens[2]; // /events/A1-P
170         String consumerGroup = ""; // users
171         String consumerId = ""; // sdnc1
172         if (tokens.length == 5) {
173             consumerGroup = tokens[3];
174             consumerId = tokens[4];
175         }
176         return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);
177     }
178 }