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