Merge "Add Policy agent blueprint"
[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 streamCfg) throws ServiceException {
95         Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
96         if (streamConfigEntries.size() != 1) {
97             throw new ServiceException(
98                 "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
99         }
100         JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
101         JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
102         String topicUrl = getAsString(dmaapInfo, "topic_url");
103
104         Properties dmaapProps = new Properties();
105         try {
106             URL url = new URL(topicUrl);
107             String passwd = "";
108             String userName = "";
109             if (url.getUserInfo() != null) {
110                 String[] userInfo = url.getUserInfo().split(":");
111                 userName = userInfo[0];
112                 passwd = userInfo[1];
113             }
114             String urlPath = url.getPath();
115             DmaapUrlPath path = parseDmaapUrlPath(urlPath);
116
117             dmaapProps.put("ServiceName", url.getHost() + ":" + url.getPort() + "/events");
118             dmaapProps.put("topic", path.dmaapTopicName);
119             dmaapProps.put("host", url.getHost() + ":" + url.getPort());
120             dmaapProps.put("contenttype", MediaType.APPLICATION_JSON.toString());
121             dmaapProps.put("userName", userName);
122             dmaapProps.put("password", passwd);
123             dmaapProps.put("group", path.consumerGroup);
124             dmaapProps.put("id", path.consumerId);
125             dmaapProps.put("TransportType", ProtocolTypeConstants.HTTPNOAUTH.toString());
126             dmaapProps.put("timeout", 15000);
127             dmaapProps.put("limit", 1000);
128         } catch (MalformedURLException e) {
129             throw new ServiceException("Could not parse the URL", e);
130         }
131
132         return dmaapProps;
133     }
134
135     private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
136         return get(obj, memberName).getAsString();
137     }
138
139     private class DmaapUrlPath {
140         final String dmaapTopicName;
141         final String consumerGroup;
142         final String consumerId;
143
144         DmaapUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
145             this.dmaapTopicName = dmaapTopicName;
146             this.consumerGroup = consumerGroup;
147             this.consumerId = consumerId;
148         }
149     }
150
151     private DmaapUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
152         String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
153         if (!(tokens.length == 3 ^ tokens.length == 5)) {
154             throw new ServiceException("The path has incorrect syntax: " + urlPath);
155         }
156
157         final String dmaapTopicName = tokens[2]; // /events/A1-P
158         String consumerGroup = ""; // users
159         String consumerId = ""; // sdnc1
160         if (tokens.length == 5) {
161             consumerGroup = tokens[3];
162             consumerId = tokens[4];
163         }
164         return new DmaapUrlPath(dmaapTopicName, consumerGroup, consumerId);
165     }
166 }