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