Add DCAE configuration parsing tools 81/6781/1
authorGuangrong Fu <fu.guangrong@zte.com.cn>
Mon, 7 Aug 2017 03:41:43 +0000 (11:41 +0800)
committerGuangrong Fu <fu.guangrong@zte.com.cn>
Mon, 7 Aug 2017 03:41:43 +0000 (11:41 +0800)
Add DCAE configurations parsing classes.
Add corresponding unit test codes.

Change-Id: I7711ede272d470af9a596539691ac224cf96fd5d
Issue-ID: HOLMES-25
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java [new file with mode: 0644]
holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/Rule.java [new file with mode: 0644]
holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/SecurityInfo.java [new file with mode: 0644]
holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java [new file with mode: 0644]
holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java [new file with mode: 0644]
holmes-actions/src/test/resources/dcae.config.json [new file with mode: 0644]

diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java
new file mode 100644 (file)
index 0000000..b08261f
--- /dev/null
@@ -0,0 +1,58 @@
+/**\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.entity;\r
+\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+import lombok.NoArgsConstructor;\r
+\r
+@NoArgsConstructor\r
+public class DcaeConfigurations extends HashMap<String, Object>{\r
+    private Map<String, SecurityInfo> streamsPublishes = new HashMap<>();\r
+    private Map<String, SecurityInfo> streamsSubscribes = new HashMap<>();\r
+    private List<Rule> rules = new ArrayList<>();\r
+\r
+    public void addDefaultRule(Rule rule) {\r
+        if (null == rule) {\r
+            return;\r
+        }\r
+\r
+        this.rules.add(rule);\r
+    }\r
+\r
+    public List<Rule> getDefaultRules() {\r
+        return this.rules;\r
+    }\r
+\r
+    public SecurityInfo addPubSecInfo(String key, SecurityInfo value) {\r
+        return this.streamsPublishes.put(key, value);\r
+    }\r
+\r
+    public SecurityInfo getPubSecInfo(String key) {\r
+        return this.streamsPublishes.get(key);\r
+    }\r
+\r
+    public SecurityInfo addSubSecInfo(String key, SecurityInfo value) {\r
+        return this.streamsSubscribes.put(key, value);\r
+    }\r
+\r
+    public SecurityInfo getSubSecInfo(String key) {\r
+        return this.streamsSubscribes.get(key);\r
+    }\r
+}\r
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/Rule.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/Rule.java
new file mode 100644 (file)
index 0000000..4ff1d05
--- /dev/null
@@ -0,0 +1,29 @@
+/**\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.entity;\r
+\r
+import lombok.AllArgsConstructor;\r
+import lombok.Getter;\r
+import lombok.Setter;\r
+\r
+@Getter\r
+@Setter\r
+@AllArgsConstructor\r
+public class Rule {\r
+    private String name;\r
+    private String contents;\r
+}\r
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/SecurityInfo.java b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/SecurityInfo.java
new file mode 100644 (file)
index 0000000..6c6fa56
--- /dev/null
@@ -0,0 +1,39 @@
+/**\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.entity;\r
+\r
+import lombok.Getter;\r
+import lombok.Setter;\r
+\r
+@Getter\r
+@Setter\r
+public class SecurityInfo {\r
+    private String type = "message_router";\r
+    private String aafPassword;\r
+    private String aafUsername;\r
+    private boolean secureTopic = false;\r
+    private DmaapInfo dmaapInfo = new DmaapInfo();\r
+\r
+    @Getter\r
+    @Setter\r
+    public class DmaapInfo {\r
+        private String location;\r
+        private String clientId;\r
+        private String clientRole;\r
+        private String topicUrl;\r
+    }\r
+}\r
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
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java
new file mode 100644 (file)
index 0000000..e20ed5e
--- /dev/null
@@ -0,0 +1,64 @@
+/*\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 static org.hamcrest.core.IsEqual.equalTo;\r
+import static org.junit.Assert.assertThat;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+import java.net.URI;\r
+import java.net.URISyntaxException;\r
+import java.net.URL;\r
+import org.junit.Test;\r
+import org.onap.holmes.common.dcae.entity.DcaeConfigurations;\r
+import org.onap.holmes.common.dcae.entity.SecurityInfo;\r
+\r
+public class DcaeConfigurationParserTest {\r
+\r
+    @Test\r
+    public void parse() throws Exception {\r
+        DcaeConfigurations obj = DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"));\r
+\r
+        assertThat(obj.getDefaultRules().size(), equalTo(1));\r
+        assertThat(obj.get("collector.keystore.alias"), equalTo("dynamically generated"));\r
+        assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getAafPassword(), equalTo("aaf_password"));\r
+        assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getDmaapInfo().getLocation(), equalTo("mtl5"));\r
+    }\r
+\r
+    private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException {\r
+        URL url = DcaeConfigurationParserTest.class.getClassLoader().getResource(fileName);\r
+        File configFile = new File(new URI(url.toString()).getPath());\r
+        BufferedReader br = new BufferedReader(new FileReader(configFile));\r
+\r
+        final StringBuilder sb = new StringBuilder();\r
+        br.lines().forEach(line -> {\r
+            sb.append(line);\r
+        });\r
+\r
+        try {\r
+            br.close();\r
+        } catch (IOException e) {\r
+            // Do nothing\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+}
\ No newline at end of file
diff --git a/holmes-actions/src/test/resources/dcae.config.json b/holmes-actions/src/test/resources/dcae.config.json
new file mode 100644 (file)
index 0000000..6fe72e8
--- /dev/null
@@ -0,0 +1,64 @@
+{\r
+  "holmes.default.rule.volte.scenario1": "package dcae.ves.test\nimport org.onap.some.related.packages;\nrule\"SameVNF_Relation_Rule\"\nsalience 120\nno-loop true\nwhen\n$root : VesAlarm(\n$sourceId: sourceId, sourceId != null && !sourceId.equals(\"\"),\nspecificProblem in ( \"LSS_cpiPCSCFFailReg(121297)\", \"LSS_cpiSIPRetransmitInvite(120267)\" ),\n$eventId: eventId)\n$child : VesAlarm( eventId != $eventId,\nCorrelationUtil.getInstance().isTopologicallyRelated(sourceId, $sourceId),\nspecificProblem in (\"LSS_externalLinkDown(4271)\",\"LSS_failedAttachReqsRateExceeded(4272)\"),\nthis after [-60s, 60s] $root)\nthen\nDmaapService.publishResult(...);\nend",\r
+  "collector.schema.file": "./etc/CommonEventFormat_27.2.json",\r
+  "collector.service.port": 8080,\r
+  "collector.dmaap.streamid": "fault=sec_fault,roadm-sec-to-hp|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert",\r
+  "collector.schema.checkflag": 1,\r
+  "tomcat.maxthreads": "200",\r
+  "collector.keystore.passwordfile": "/opt/app/dcae-certificate/.password",\r
+  "streams_subscribes": {},\r
+  "services_calls": {},\r
+  "services_provides": {},\r
+  "collector.inputQueue.maxPending": 8096,\r
+  "header.authflag": 0,\r
+  "collector.keystore.file.location": "/opt/app/dcae-certificate/keystore.jks",\r
+  "collector.service.secure.port": -1,\r
+  "header.authlist": "userid1,base64encodepwd1|userid2,base64encodepwd2",\r
+  "collector.keystore.alias": "dynamically generated",\r
+  "streams_publishes": {\r
+    "sec_measurement": {\r
+      "type": "message_router",\r
+      "aaf_password": "aaf_password",\r
+      "dmaap_info": {\r
+        "location": "mtl5",\r
+        "client_id": "111111",\r
+        "client_role": "com.att.dcae.member",\r
+        "topic_url": "https://mrlocal:3905/events/com.att.dcae.dmaap.FTL2.SEC-MEASUREMENT-OUTPUT"\r
+      },\r
+      "aaf_username": "aaf_username"\r
+    },\r
+    "sec_fault_unsecure": {\r
+      "type": "message_router",\r
+      "aaf_password": null,\r
+      "dmaap_info": {\r
+        "location": "mtl5",\r
+        "client_id": null,\r
+        "client_role": null,\r
+        "topic_url": "http://ueb.global:3904/events/DCAE-SE-COLLECTOR-EVENTS-DEV"\r
+      },\r
+      "aaf_username": null\r
+    },\r
+    "sec_measurement_unsecure": {\r
+      "type": "message_router",\r
+      "aaf_password": null,\r
+      "dmaap_info": {\r
+        "location": "mtl5",\r
+        "client_id": null,\r
+        "client_role": null,\r
+        "topic_url": "http://ueb.global:3904/events/DCAE-SE-COLLECTOR-EVENTS-DEV"\r
+      },\r
+      "aaf_username": null\r
+    },\r
+    "sec_fault": {\r
+      "type": "message_router",\r
+      "aaf_password": "aaf_password",\r
+      "dmaap_info": {\r
+        "location": "mtl5",\r
+        "client_id": "222222",\r
+        "client_role": "com.att.dcae.member",\r
+        "topic_url": "https://mrlocal:3905/events/com.att.dcae.dmaap.FTL2.SEC-FAULT-OUTPUT"\r
+      },\r
+      "aaf_username": "aaf_username"\r
+    }\r
+  }\r
+}
\ No newline at end of file