DmaapMessageHandler using the agent NBI
[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;
56     @Getter
57     private Properties dmaapConsumerConfig;
58
59     public void parse(JsonObject root) throws ServiceException {
60         JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
61         ricConfigs = parseRics(agentConfigJson);
62         JsonObject dmaapPublisherConfigJson = agentConfigJson.getAsJsonObject("streams_publishes");
63         if (dmaapPublisherConfigJson == null) {
64             dmaapPublisherConfig = new Properties();
65         } else {
66             dmaapPublisherConfig = parseDmaapConfig(dmaapPublisherConfigJson);
67         }
68         JsonObject dmaapConsumerConfigJson = agentConfigJson.getAsJsonObject("streams_subscribes");
69         if (dmaapConsumerConfigJson == null) {
70             dmaapConsumerConfig = new Properties();
71         } else {
72             dmaapConsumerConfig = parseDmaapConfig(dmaapConsumerConfigJson);
73         }
74     }
75
76     private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
77         Vector<RicConfig> result = new Vector<RicConfig>();
78         for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
79             result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
80         }
81         return result;
82     }
83
84     private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
85         JsonElement elem = obj.get(memberName);
86         if (elem == null) {
87             throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
88         }
89         return elem;
90     }
91
92     private JsonArray getAsJsonArray(JsonObject obj, String memberName) throws ServiceException {
93         return get(obj, memberName).getAsJsonArray();
94     }
95
96     private Properties parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
97         Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
98         if (streamConfigEntries.size() != 1) {
99             throw new ServiceException(
100                 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
101         }
102         JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
103         JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
104         String topicUrl = getAsString(dmaapInfo, "topic_url");
105
106         Properties dmaapProps = new Properties();
107         try {
108             URL url = new URL(topicUrl);
109             String passwd = "";
110             String userName = "";
111             if (url.getUserInfo() != null) {
112                 String[] userInfo = url.getUserInfo().split(":");
113                 userName = userInfo[0];
114                 passwd = userInfo[1];
115             }
116             String urlPath = url.getPath();
117             DmaapUrlPath path = parseDmaapUrlPath(urlPath);
118
119             dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
120             dmaapProps.put("topic", path.dmaapTopicName);
121             dmaapProps.put("host", url.getHost() + ":" + url.getPort());
122             dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
123             dmaapProps.put("userName", userName);
124             dmaapProps.put("password", passwd);
125             dmaapProps.put("group", path.consumerGroup);
126             dmaapProps.put("id", path.consumerId);
127             dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
128             dmaapProps.put("timeout", 15000);
129             dmaapProps.put("limit", 1000);
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 }