Add DCAE configuration parsing tools
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / dcae / utils / DcaeConfigurationParser.java
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java
new file mode 100644 (file)
index 0000000..260b26a
--- /dev/null
@@ -0,0 +1,109 @@
+/*\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.holmes.common.dcae.utils;\r
+\r
+import java.util.Arrays;\r
+import java.util.List;\r
+import java.util.Map.Entry;\r
+import java.util.Set;\r
+import java.util.stream.Stream;\r
+import net.sf.json.JSONObject;\r
+import org.apache.commons.lang3.StringUtils;\r
+import org.onap.holmes.common.dcae.entity.DcaeConfigurations;\r
+import org.onap.holmes.common.dcae.entity.Rule;\r
+import org.onap.holmes.common.dcae.entity.SecurityInfo;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
+\r
+public class DcaeConfigurationParser {\r
+\r
+    private static final List<String> OBJECT_ATTRS = Arrays\r
+            .asList(new String[]{"streams_subscribes", "streams_publishes", "services_calls", "services_provides"});\r
+\r
+    public static DcaeConfigurations parse(String jsonStr) throws CorrelationException {\r
+        if (StringUtils.isEmpty(jsonStr)) {\r
+            throw new CorrelationException(\r
+                    "Can not resolve configurations from DCAE. The configuration string is empty");\r
+        }\r
+\r
+        DcaeConfigurations ret = new DcaeConfigurations();\r
+\r
+        JSONObject jsonObject = null;\r
+        try {\r
+            jsonObject = JSONObject.fromObject(jsonStr);\r
+        } catch (Exception e) {\r
+            throw new CorrelationException(e.getMessage(), e);\r
+        }\r
+\r
+        fillInRules(ret, jsonObject);\r
+        fillInPublishesInfo(ret, jsonObject);\r
+\r
+        if (jsonObject.containsKey("streams_subscribes")) {\r
+\r
+        }\r
+\r
+        JSONObject finalJsonObject = jsonObject;\r
+        Stream.of(jsonObject.keySet().toArray(new String[0]))\r
+                .filter(key -> !OBJECT_ATTRS.contains(key))\r
+                .forEach(key -> ret.put(key.toString(), finalJsonObject.getString(String.valueOf(key))));\r
+        return ret;\r
+    }\r
+\r
+    private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) {\r
+        if (jsonObject.containsKey("streams_publishes")) {\r
+            JSONObject publishes = jsonObject.getJSONObject("streams_publishes");\r
+            for (Object key : publishes.keySet()) {\r
+                ret.addPubSecInfo((String) key,\r
+                        createSecurityInfo((String) key, publishes.getJSONObject((String) key)));\r
+            }\r
+        }\r
+    }\r
+\r
+    private static SecurityInfo createSecurityInfo(String key, JSONObject entity) {\r
+        SecurityInfo securityInfo = new SecurityInfo();\r
+        securityInfo.setType(entity.getString("type"));\r
+        if (!entity.get("aaf_password").equals("null")) {\r
+            securityInfo.setAafPassword(entity.getString("aaf_password"));\r
+        }\r
+        if (!entity.get("aaf_username").equals("null")) {\r
+            securityInfo.setAafUsername(entity.getString("aaf_username"));\r
+        }\r
+        securityInfo.setSecureTopic(!key.endsWith("unsecure"));\r
+        fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info"));\r
+        return securityInfo;\r
+    }\r
+\r
+    private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) {\r
+        SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();\r
+        dmaapInfo.setLocation(jsonDmaapInfo.getString("location"));\r
+        dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url"));\r
+        if (!jsonDmaapInfo.get("client_id").equals("null")) {\r
+            dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id"));\r
+        }\r
+        if (!jsonDmaapInfo.get("client_role").equals("null")) {\r
+            dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role"));\r
+        }\r
+    }\r
+\r
+    private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) {\r
+        Set<Entry<String, Object>> entries = jsonObject.entrySet();\r
+        for (Entry<String, Object> entry : entries) {\r
+            if (entry.getKey().startsWith("holmes.default.rule")) {\r
+                ret.addDefaultRule(new Rule(entry.getKey(), (String) entry.getValue()));\r
+            }\r
+        }\r
+    }\r
+}\r