Add Logic for Parsing Sub Info from DCAE
[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 package org.onap.holmes.common.dcae.utils;\r
17 \r
18 import java.util.Arrays;\r
19 import java.util.List;\r
20 import java.util.Map.Entry;\r
21 import java.util.Set;\r
22 import java.util.stream.Stream;\r
23 import net.sf.json.JSONObject;\r
24 import org.apache.commons.lang3.StringUtils;\r
25 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;\r
26 import org.onap.holmes.common.dcae.entity.Rule;\r
27 import org.onap.holmes.common.dcae.entity.SecurityInfo;\r
28 import org.onap.holmes.common.exception.CorrelationException;\r
29 \r
30 public class DcaeConfigurationParser {\r
31 \r
32     private static final String RULE_CONTENT_SPLIT = "\\$\\$\\$";\r
33 \r
34     private static final List<String> OBJECT_ATTRS = Arrays\r
35             .asList(new String[]{"streams_subscribes", "streams_publishes", "services_calls", "services_provides"});\r
36 \r
37     public static DcaeConfigurations parse(String jsonStr) throws CorrelationException {\r
38         if (StringUtils.isEmpty(jsonStr)) {\r
39             throw new CorrelationException(\r
40                     "Can not resolve configurations from DCAE. The configuration string is empty.");\r
41         }\r
42 \r
43         DcaeConfigurations ret = new DcaeConfigurations();\r
44 \r
45         JSONObject jsonObject = null;\r
46         try {\r
47             jsonObject = JSONObject.fromObject(jsonStr);\r
48         } catch (Exception e) {\r
49             throw new CorrelationException(e.getMessage(), e);\r
50         }\r
51 \r
52         fillInRules(ret, jsonObject);\r
53         fillInPublishesInfo(ret, jsonObject);\r
54         fillInSubscribesInfo(ret, jsonObject);\r
55 \r
56         JSONObject finalJsonObject = jsonObject;\r
57         Stream.of(jsonObject.keySet().toArray(new String[0]))\r
58                 .filter(key -> !OBJECT_ATTRS.contains(key))\r
59                 .forEach(key -> ret.put(key.toString(), finalJsonObject.getString(String.valueOf(key))));\r
60         return ret;\r
61     }\r
62 \r
63     private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) {\r
64         if (jsonObject.containsKey("streams_publishes")) {\r
65             JSONObject publishes = jsonObject.getJSONObject("streams_publishes");\r
66             for (Object key : publishes.keySet()) {\r
67                 ret.addPubSecInfo((String) key,\r
68                         createSecurityInfo((String) key, publishes.getJSONObject((String) key)));\r
69             }\r
70         }\r
71     }\r
72 \r
73     private static void fillInSubscribesInfo(DcaeConfigurations ret, JSONObject jsonObject) {\r
74         if (jsonObject.containsKey("streams_subscribes")) {\r
75             JSONObject subscribes = jsonObject.getJSONObject("streams_subscribes");\r
76             for (Object key : subscribes.keySet()) {\r
77                 ret.addSubSecInfo((String) key,\r
78                         createSecurityInfo((String) key, subscribes.getJSONObject((String) key)));\r
79             }\r
80         }\r
81     }\r
82 \r
83     private static SecurityInfo createSecurityInfo(String key, JSONObject entity) {\r
84         SecurityInfo securityInfo = new SecurityInfo();\r
85         if (entity.containsKey("type")) {\r
86             securityInfo.setType(entity.getString("type"));\r
87         }\r
88         if (entity.containsKey("aaf_password")) {\r
89             securityInfo.setAafPassword(entity.getString("aaf_password"));\r
90         }\r
91         if (entity.containsKey("aaf_username")) {\r
92             securityInfo.setAafUsername(entity.getString("aaf_username"));\r
93         }\r
94         securityInfo.setSecureTopic(!key.endsWith("unsecure"));\r
95         fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info"));\r
96         return securityInfo;\r
97     }\r
98 \r
99     private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) {\r
100         SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();\r
101         if (jsonDmaapInfo.containsKey("location")){\r
102             dmaapInfo.setLocation(jsonDmaapInfo.getString("location"));\r
103         }\r
104         if (jsonDmaapInfo.containsKey("topic_url")) {\r
105             dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url"));\r
106         }\r
107         if (jsonDmaapInfo.containsKey("client_id")) {\r
108             dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id"));\r
109         }\r
110         if (jsonDmaapInfo.containsKey("client_role")) {\r
111             dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role"));\r
112         }\r
113         if (jsonDmaapInfo.containsKey("type")) {\r
114             dmaapInfo.setType(jsonDmaapInfo.getString("type"));\r
115         }\r
116     }\r
117 \r
118     private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) {\r
119         Set<Entry<String, Object>> entries = jsonObject.entrySet();\r
120         for (Entry<String, Object> entry : entries) {\r
121             if (entry.getKey().startsWith("holmes.default.rule")) {\r
122                 String value = (String) entry.getValue();\r
123                 String[] contents = value.split(RULE_CONTENT_SPLIT);\r
124                 ret.addDefaultRule(new Rule(entry.getKey(), contents[0], contents[1], 1));\r
125             }\r
126         }\r
127     }\r
128 }\r