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