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