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