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