From 0cfd77aa83f4e3e314d125fbaef461e32099c0bc Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Mon, 7 Aug 2017 11:41:43 +0800 Subject: Add DCAE configuration parsing tools Add DCAE configurations parsing classes. Add corresponding unit test codes. Change-Id: I7711ede272d470af9a596539691ac224cf96fd5d Issue-ID: HOLMES-25 Signed-off-by: Guangrong Fu --- .../common/dcae/entity/DcaeConfigurations.java | 58 +++++++++++ .../org/onap/holmes/common/dcae/entity/Rule.java | 29 ++++++ .../holmes/common/dcae/entity/SecurityInfo.java | 39 ++++++++ .../common/dcae/utils/DcaeConfigurationParser.java | 109 +++++++++++++++++++++ .../dcae/utils/DcaeConfigurationParserTest.java | 64 ++++++++++++ holmes-actions/src/test/resources/dcae.config.json | 64 ++++++++++++ 6 files changed, 363 insertions(+) create mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java create mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/Rule.java create mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/SecurityInfo.java create mode 100644 holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java create mode 100644 holmes-actions/src/test/resources/dcae.config.json 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 index 0000000..b08261f --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/DcaeConfigurations.java @@ -0,0 +1,58 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.common.dcae.entity; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +public class DcaeConfigurations extends HashMap{ + private Map streamsPublishes = new HashMap<>(); + private Map streamsSubscribes = new HashMap<>(); + private List rules = new ArrayList<>(); + + public void addDefaultRule(Rule rule) { + if (null == rule) { + return; + } + + this.rules.add(rule); + } + + public List getDefaultRules() { + return this.rules; + } + + public SecurityInfo addPubSecInfo(String key, SecurityInfo value) { + return this.streamsPublishes.put(key, value); + } + + public SecurityInfo getPubSecInfo(String key) { + return this.streamsPublishes.get(key); + } + + public SecurityInfo addSubSecInfo(String key, SecurityInfo value) { + return this.streamsSubscribes.put(key, value); + } + + public SecurityInfo getSubSecInfo(String key) { + return this.streamsSubscribes.get(key); + } +} 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 index 0000000..4ff1d05 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/Rule.java @@ -0,0 +1,29 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.common.dcae.entity; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class Rule { + private String name; + private String contents; +} 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 index 0000000..6c6fa56 --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/entity/SecurityInfo.java @@ -0,0 +1,39 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.common.dcae.entity; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class SecurityInfo { + private String type = "message_router"; + private String aafPassword; + private String aafUsername; + private boolean secureTopic = false; + private DmaapInfo dmaapInfo = new DmaapInfo(); + + @Getter + @Setter + public class DmaapInfo { + private String location; + private String clientId; + private String clientRole; + private String topicUrl; + } +} 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 index 0000000..260b26a --- /dev/null +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParser.java @@ -0,0 +1,109 @@ +/* + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.common.dcae.utils; + +import java.util.Arrays; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Stream; +import net.sf.json.JSONObject; +import org.apache.commons.lang3.StringUtils; +import org.onap.holmes.common.dcae.entity.DcaeConfigurations; +import org.onap.holmes.common.dcae.entity.Rule; +import org.onap.holmes.common.dcae.entity.SecurityInfo; +import org.onap.holmes.common.exception.CorrelationException; + +public class DcaeConfigurationParser { + + private static final List OBJECT_ATTRS = Arrays + .asList(new String[]{"streams_subscribes", "streams_publishes", "services_calls", "services_provides"}); + + public static DcaeConfigurations parse(String jsonStr) throws CorrelationException { + if (StringUtils.isEmpty(jsonStr)) { + throw new CorrelationException( + "Can not resolve configurations from DCAE. The configuration string is empty"); + } + + DcaeConfigurations ret = new DcaeConfigurations(); + + JSONObject jsonObject = null; + try { + jsonObject = JSONObject.fromObject(jsonStr); + } catch (Exception e) { + throw new CorrelationException(e.getMessage(), e); + } + + fillInRules(ret, jsonObject); + fillInPublishesInfo(ret, jsonObject); + + if (jsonObject.containsKey("streams_subscribes")) { + + } + + JSONObject finalJsonObject = jsonObject; + Stream.of(jsonObject.keySet().toArray(new String[0])) + .filter(key -> !OBJECT_ATTRS.contains(key)) + .forEach(key -> ret.put(key.toString(), finalJsonObject.getString(String.valueOf(key)))); + return ret; + } + + private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) { + if (jsonObject.containsKey("streams_publishes")) { + JSONObject publishes = jsonObject.getJSONObject("streams_publishes"); + for (Object key : publishes.keySet()) { + ret.addPubSecInfo((String) key, + createSecurityInfo((String) key, publishes.getJSONObject((String) key))); + } + } + } + + private static SecurityInfo createSecurityInfo(String key, JSONObject entity) { + SecurityInfo securityInfo = new SecurityInfo(); + securityInfo.setType(entity.getString("type")); + if (!entity.get("aaf_password").equals("null")) { + securityInfo.setAafPassword(entity.getString("aaf_password")); + } + if (!entity.get("aaf_username").equals("null")) { + securityInfo.setAafUsername(entity.getString("aaf_username")); + } + securityInfo.setSecureTopic(!key.endsWith("unsecure")); + fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info")); + return securityInfo; + } + + private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) { + SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo(); + dmaapInfo.setLocation(jsonDmaapInfo.getString("location")); + dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url")); + if (!jsonDmaapInfo.get("client_id").equals("null")) { + dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id")); + } + if (!jsonDmaapInfo.get("client_role").equals("null")) { + dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role")); + } + } + + private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) { + Set> entries = jsonObject.entrySet(); + for (Entry entry : entries) { + if (entry.getKey().startsWith("holmes.default.rule")) { + ret.addDefaultRule(new Rule(entry.getKey(), (String) entry.getValue())); + } + } + } +} 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 index 0000000..e20ed5e --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dcae/utils/DcaeConfigurationParserTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.holmes.common.dcae.utils; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import org.junit.Test; +import org.onap.holmes.common.dcae.entity.DcaeConfigurations; +import org.onap.holmes.common.dcae.entity.SecurityInfo; + +public class DcaeConfigurationParserTest { + + @Test + public void parse() throws Exception { + DcaeConfigurations obj = DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json")); + + assertThat(obj.getDefaultRules().size(), equalTo(1)); + assertThat(obj.get("collector.keystore.alias"), equalTo("dynamically generated")); + assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getAafPassword(), equalTo("aaf_password")); + assertThat(((SecurityInfo) obj.getPubSecInfo("sec_measurement")).getDmaapInfo().getLocation(), equalTo("mtl5")); + } + + private String readConfigurationsFromFile(String fileName) throws URISyntaxException, FileNotFoundException { + URL url = DcaeConfigurationParserTest.class.getClassLoader().getResource(fileName); + File configFile = new File(new URI(url.toString()).getPath()); + BufferedReader br = new BufferedReader(new FileReader(configFile)); + + final StringBuilder sb = new StringBuilder(); + br.lines().forEach(line -> { + sb.append(line); + }); + + try { + br.close(); + } catch (IOException e) { + // Do nothing + } + return sb.toString(); + } + +} \ 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 index 0000000..6fe72e8 --- /dev/null +++ b/holmes-actions/src/test/resources/dcae.config.json @@ -0,0 +1,64 @@ +{ + "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", + "collector.schema.file": "./etc/CommonEventFormat_27.2.json", + "collector.service.port": 8080, + "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", + "collector.schema.checkflag": 1, + "tomcat.maxthreads": "200", + "collector.keystore.passwordfile": "/opt/app/dcae-certificate/.password", + "streams_subscribes": {}, + "services_calls": {}, + "services_provides": {}, + "collector.inputQueue.maxPending": 8096, + "header.authflag": 0, + "collector.keystore.file.location": "/opt/app/dcae-certificate/keystore.jks", + "collector.service.secure.port": -1, + "header.authlist": "userid1,base64encodepwd1|userid2,base64encodepwd2", + "collector.keystore.alias": "dynamically generated", + "streams_publishes": { + "sec_measurement": { + "type": "message_router", + "aaf_password": "aaf_password", + "dmaap_info": { + "location": "mtl5", + "client_id": "111111", + "client_role": "com.att.dcae.member", + "topic_url": "https://mrlocal:3905/events/com.att.dcae.dmaap.FTL2.SEC-MEASUREMENT-OUTPUT" + }, + "aaf_username": "aaf_username" + }, + "sec_fault_unsecure": { + "type": "message_router", + "aaf_password": null, + "dmaap_info": { + "location": "mtl5", + "client_id": null, + "client_role": null, + "topic_url": "http://ueb.global:3904/events/DCAE-SE-COLLECTOR-EVENTS-DEV" + }, + "aaf_username": null + }, + "sec_measurement_unsecure": { + "type": "message_router", + "aaf_password": null, + "dmaap_info": { + "location": "mtl5", + "client_id": null, + "client_role": null, + "topic_url": "http://ueb.global:3904/events/DCAE-SE-COLLECTOR-EVENTS-DEV" + }, + "aaf_username": null + }, + "sec_fault": { + "type": "message_router", + "aaf_password": "aaf_password", + "dmaap_info": { + "location": "mtl5", + "client_id": "222222", + "client_role": "com.att.dcae.member", + "topic_url": "https://mrlocal:3905/events/com.att.dcae.dmaap.FTL2.SEC-FAULT-OUTPUT" + }, + "aaf_username": "aaf_username" + } + } +} \ No newline at end of file -- cgit 1.2.3-korg