diff options
author | Guangrong Fu <fu.guangrong@zte.com.cn> | 2017-08-07 11:41:43 +0800 |
---|---|---|
committer | Guangrong Fu <fu.guangrong@zte.com.cn> | 2017-08-07 11:41:43 +0800 |
commit | 0cfd77aa83f4e3e314d125fbaef461e32099c0bc (patch) | |
tree | f29601148c735eff9c3dee765b6441b6e05e9c4c /holmes-actions/src/main/java | |
parent | 1e8a0cb8274f753cd629fd261030e75c2d22a709 (diff) |
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 <fu.guangrong@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/main/java')
4 files changed, 235 insertions, 0 deletions
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<String, Object>{
+ private Map<String, SecurityInfo> streamsPublishes = new HashMap<>();
+ private Map<String, SecurityInfo> streamsSubscribes = new HashMap<>();
+ private List<Rule> rules = new ArrayList<>();
+
+ public void addDefaultRule(Rule rule) {
+ if (null == rule) {
+ return;
+ }
+
+ this.rules.add(rule);
+ }
+
+ public List<Rule> 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<String> 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<Entry<String, Object>> entries = jsonObject.entrySet();
+ for (Entry<String, Object> entry : entries) {
+ if (entry.getKey().startsWith("holmes.default.rule")) {
+ ret.addDefaultRule(new Rule(entry.getKey(), (String) entry.getValue()));
+ }
+ }
+ }
+}
|