Merge "Adapt A1 controller to latest A1 spec"
[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 import java.net.MalformedURLException;
29 import java.net.URL;
30 import java.util.Map.Entry;
31 import java.util.Properties;
32 import java.util.Set;
33 import java.util.Vector;
34 import javax.validation.constraints.NotNull;
35 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
36 import org.oransc.policyagent.exceptions.ServiceException;
37 import org.springframework.http.MediaType;
38
39 public class ApplicationConfigParser {
40
41     private static final String CONFIG = "config";
42
43     private static Gson gson = new GsonBuilder() //
44         .serializeNulls() //
45         .create(); //
46
47     private Vector<RicConfig> ricConfig;
48     private Properties dmaapPublisherConfig;
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 dmaapPublisherConfigJson = root.getAsJsonObject("streams_publishes");
58         if (dmaapPublisherConfigJson == null) {
59             dmaapPublisherConfig = new Properties();
60         } else {
61             dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
62         }
63         JsonObject dmaapConsumerConfigJson = root.getAsJsonObject("streams_subscribes");
64         if (dmaapConsumerConfigJson == null) {
65             dmaapConsumerConfig = new Properties();
66         } else {
67             dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
68         }
69     }
70
71     public Vector<RicConfig> getRicConfigs() {
72         return this.ricConfig;
73     }
74
75     public Properties getDmaapPublisherConfig() {
76         return dmaapPublisherConfig;
77     }
78
79     public Properties getDmaapConsumerConfig() {
80         return dmaapConsumerConfig;
81     }
82
83     private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
84         Vector<RicConfig> result = new Vector<RicConfig>();
85         for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
86             result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
87         }
88         return result;
89     }
90
91     private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
92         JsonElement elem = obj.get(memberName);
93         if (elem == null) {
94             throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
95         }
96         return elem;
97     }
98
99     private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
100         return get(obj, memberName).getAsJsonArray();
101     }
102
103     private Properties parseDmaapConfig(JsonObject consumerCfg) throws ServiceException {
104         Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
105         if (topics.size() != 1) {
106             throw new ServiceException("Invalid configuration, number of topic must be one, config: " + topics);
107         }
108         JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
109         JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
110         String topicUrl = getAsString(dmaapInfo, "topic_url");
111
112         Properties dmaapProps = new Properties();
113         try {
114             URL url = new URL(topicUrl);
115             String passwd = "";
116             String userName = "";
117             if (url.getUserInfo() != null) {
118                 String[] userInfo = url.getUserInfo().split(":");
119                 userName = userInfo[0];
120                 passwd = userInfo[1];
121             }
122             String urlPath = url.getPath();
123             DmaapUrlPath path = parseDmaapUrlPath(urlPath);
124
125             dmaapProps.put("ServiceName", url.getHost()+":"+url.getPort()+"/events");
126             dmaapProps.put("topic", path.dmaapTopicName);
127             dmaapProps.put("host", url.getHost()+":"+url.getPort());
128             dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
129             dmaapProps.put("userName", userName);
130             dmaapProps.put("password", passwd);
131             dmaapProps.put("group", path.consumerGroup);
132             dmaapProps.put("id", path.consumerId);
133             dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
134             dmaapProps.put("timeout", 15000);
135             dmaapProps.put("limit", 1000);
136         } catch (MalformedURLException e) {
137             throw new ServiceException("Could not parse the URL", e);
138         }
139
140         return dmaapProps;
141     }
142
143     private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
144         return get(obj, memberName).getAsString();
145     }
146
147     private class DmaapUrlPath {
148         final String dmaapTopicName;
149         final String consumerGroup;
150         final String consumerId;
151
152         DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
153             this.dmaapTopicName = dmaapTopicName;
154             this.consumerGroup = consumerGroup;
155             this.consumerId = consumerId;
156         }
157     }
158
159     private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
160         String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
161         if (!(tokens.length == 3 ^ tokens.length == 5)) {
162             throw new ServiceException("The path has incorrect syntax: " + urlPath);
163         }
164
165         final String dmaapTopicName =  tokens[2]; // /events/A1-P
166         String consumerGroup = ""; // users
167         String consumerId = ""; // sdnc1
168         if (tokens.length == 5) {
169             consumerGroup = tokens[3];
170             consumerId = tokens[4];
171         }
172         return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);
173     }
174 }