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