e4899d3018da99da2c199b5effc9000a7223ef23
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / dcae / utils / DcaeConfigurationParser.java
1 /*\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.holmes.common.dcae.utils;\r
18 \r
19 import java.util.Arrays;\r
20 import java.util.List;\r
21 import java.util.Map.Entry;\r
22 import java.util.Set;\r
23 import java.util.stream.Stream;\r
24 import net.sf.json.JSONObject;\r
25 import org.apache.commons.lang3.StringUtils;\r
26 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;\r
27 import org.onap.holmes.common.dcae.entity.Rule;\r
28 import org.onap.holmes.common.dcae.entity.SecurityInfo;\r
29 import org.onap.holmes.common.exception.CorrelationException;\r
30 \r
31 public class DcaeConfigurationParser {\r
32 \r
33     private static final List<String> OBJECT_ATTRS = Arrays\r
34             .asList(new String[]{"streams_subscribes", "streams_publishes", "services_calls", "services_provides"});\r
35 \r
36     public static DcaeConfigurations parse(String jsonStr) throws CorrelationException {\r
37         if (StringUtils.isEmpty(jsonStr)) {\r
38             throw new CorrelationException(\r
39                     "Can not resolve configurations from DCAE. The configuration string is empty");\r
40         }\r
41 \r
42         DcaeConfigurations ret = new DcaeConfigurations();\r
43 \r
44         JSONObject jsonObject = null;\r
45         try {\r
46             jsonObject = JSONObject.fromObject(jsonStr);\r
47         } catch (Exception e) {\r
48             throw new CorrelationException(e.getMessage(), e);\r
49         }\r
50 \r
51         fillInRules(ret, jsonObject);\r
52         fillInPublishesInfo(ret, jsonObject);\r
53 \r
54         if (jsonObject.containsKey("streams_subscribes")) {\r
55 \r
56         }\r
57 \r
58         JSONObject finalJsonObject = jsonObject;\r
59         Stream.of(jsonObject.keySet().toArray(new String[0]))\r
60                 .filter(key -> !OBJECT_ATTRS.contains(key))\r
61                 .forEach(key -> ret.put(key.toString(), finalJsonObject.getString(String.valueOf(key))));\r
62         return ret;\r
63     }\r
64 \r
65     private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) {\r
66         if (jsonObject.containsKey("streams_publishes")) {\r
67             JSONObject publishes = jsonObject.getJSONObject("streams_publishes");\r
68             for (Object key : publishes.keySet()) {\r
69                 ret.addPubSecInfo((String) key,\r
70                         createSecurityInfo((String) key, publishes.getJSONObject((String) key)));\r
71             }\r
72         }\r
73     }\r
74 \r
75     private static SecurityInfo createSecurityInfo(String key, JSONObject entity) {\r
76         SecurityInfo securityInfo = new SecurityInfo();\r
77         securityInfo.setType(entity.getString("type"));\r
78         if (!entity.get("aaf_password").equals("null")) {\r
79             securityInfo.setAafPassword(entity.getString("aaf_password"));\r
80         }\r
81         if (!entity.get("aaf_username").equals("null")) {\r
82             securityInfo.setAafUsername(entity.getString("aaf_username"));\r
83         }\r
84         securityInfo.setSecureTopic(!key.endsWith("unsecure"));\r
85         fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info"));\r
86         return securityInfo;\r
87     }\r
88 \r
89     private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) {\r
90         SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();\r
91         dmaapInfo.setLocation(jsonDmaapInfo.getString("location"));\r
92         dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url"));\r
93         if (!jsonDmaapInfo.get("client_id").equals("null")) {\r
94             dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id"));\r
95         }\r
96         if (!jsonDmaapInfo.get("client_role").equals("null")) {\r
97             dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role"));\r
98         }\r
99     }\r
100 \r
101     private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) {\r
102         Set<Entry<String, Object>> entries = jsonObject.entrySet();\r
103         for (Entry<String, Object> entry : entries) {\r
104             if (entry.getKey().startsWith("holmes.default.rule")) {\r
105                 ret.addDefaultRule(new Rule(entry.getKey(), (String) entry.getValue(), 1));\r
106             }\r
107         }\r
108     }\r
109 }\r