9284dd89cd1b1a3cd9af63fc92dee528c276a8ee
[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.HashMap;\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 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 = JSONObject.fromObject(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 \r
56         if (jsonObject.containsKey("streams_subscribes")) {\r
57 \r
58         }\r
59 \r
60         JSONObject finalJsonObject = jsonObject;\r
61         Stream.of(jsonObject.keySet().toArray(new String[0]))\r
62                 .filter(key -> !OBJECT_ATTRS.contains(key))\r
63                 .forEach(key -> ret.put(key.toString(), finalJsonObject.getString(String.valueOf(key))));\r
64         return ret;\r
65     }\r
66 \r
67     private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) {\r
68         if (jsonObject.containsKey("streams_publishes")) {\r
69             JSONObject publishes = jsonObject.getJSONObject("streams_publishes");\r
70             for (Object key : publishes.keySet()) {\r
71                 ret.addPubSecInfo((String) key,\r
72                         createSecurityInfo((String) key, publishes.getJSONObject((String) key)));\r
73             }\r
74         }\r
75     }\r
76 \r
77     private static SecurityInfo createSecurityInfo(String key, JSONObject entity) {\r
78         SecurityInfo securityInfo = new SecurityInfo();\r
79         securityInfo.setType(entity.getString("type"));\r
80         if (!entity.get("aaf_password").equals("null")) {\r
81             securityInfo.setAafPassword(entity.getString("aaf_password"));\r
82         }\r
83         if (!entity.get("aaf_username").equals("null")) {\r
84             securityInfo.setAafUsername(entity.getString("aaf_username"));\r
85         }\r
86         securityInfo.setSecureTopic(!key.endsWith("unsecure"));\r
87         fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info"));\r
88         return securityInfo;\r
89     }\r
90 \r
91     private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) {\r
92         SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();\r
93         dmaapInfo.setLocation(jsonDmaapInfo.getString("location"));\r
94         dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url"));\r
95         if (!jsonDmaapInfo.get("client_id").equals("null")) {\r
96             dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id"));\r
97         }\r
98         if (!jsonDmaapInfo.get("client_role").equals("null")) {\r
99             dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role"));\r
100         }\r
101     }\r
102 \r
103     private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) {\r
104         Set<Entry<String, Object>> entries = jsonObject.entrySet();\r
105         for (Entry<String, Object> entry : entries) {\r
106             if (entry.getKey().startsWith("holmes.default.rule")) {\r
107                 String value = (String) entry.getValue();\r
108                 String[] contents = value.split(RULE_CONTENT_SPLIT);\r
109                 ret.addDefaultRule(new Rule(entry.getKey(), contents[0], contents[1], 1));\r
110             }\r
111         }\r
112     }\r
113 }\r