2 * Copyright 2017 ZTE Corporation.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
16 package org.onap.holmes.common.dcae.utils;
\r
18 import com.alibaba.fastjson.JSON;
\r
19 import com.alibaba.fastjson.JSONObject;
\r
20 import java.util.Arrays;
\r
21 import java.util.List;
\r
22 import java.util.Map.Entry;
\r
23 import java.util.Set;
\r
24 import java.util.stream.Stream;
\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
31 public class DcaeConfigurationParser {
\r
33 private static final String RULE_CONTENT_SPLIT = "\\$\\$\\$";
\r
35 private static final List<String> OBJECT_ATTRS = Arrays
\r
36 .asList(new String[]{"streams_subscribes", "streams_publishes", "services_calls", "services_provides"});
\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
44 DcaeConfigurations ret = new DcaeConfigurations();
\r
46 JSONObject jsonObject = null;
\r
48 jsonObject = JSON.parseObject(jsonStr);
\r
49 } catch (Exception e) {
\r
50 throw new CorrelationException(e.getMessage(), e);
\r
53 fillInRules(ret, jsonObject);
\r
54 fillInPublishesInfo(ret, jsonObject);
\r
55 fillInSubscribesInfo(ret, jsonObject);
\r
57 JSONObject finalJsonObject = jsonObject;
\r
58 Stream.of(jsonObject.keySet().toArray(new String[0]))
\r
59 .filter(key -> !OBJECT_ATTRS.contains(key))
\r
60 .forEach(key -> ret.put(key, finalJsonObject.getString(String.valueOf(key))));
\r
64 private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) {
\r
65 if (jsonObject.containsKey("streams_publishes")) {
\r
66 JSONObject publishes = jsonObject.getJSONObject("streams_publishes");
\r
67 for (Object key : publishes.keySet()) {
\r
68 ret.addPubSecInfo((String) key,
\r
69 createSecurityInfo((String) key, publishes.getJSONObject((String) key)));
\r
74 private static void fillInSubscribesInfo(DcaeConfigurations ret, JSONObject jsonObject) {
\r
75 if (jsonObject.containsKey("streams_subscribes")) {
\r
76 JSONObject subscribes = jsonObject.getJSONObject("streams_subscribes");
\r
77 for (Object key : subscribes.keySet()) {
\r
78 ret.addSubSecInfo((String) key,
\r
79 createSecurityInfo((String) key, subscribes.getJSONObject((String) key)));
\r
84 private static SecurityInfo createSecurityInfo(String key, JSONObject entity) {
\r
85 SecurityInfo securityInfo = new SecurityInfo();
\r
86 if (entity.containsKey("type")) {
\r
87 securityInfo.setType(entity.getString("type"));
\r
89 if (entity.containsKey("aaf_password")) {
\r
90 securityInfo.setAafPassword(entity.getString("aaf_password"));
\r
92 if (entity.containsKey("aaf_username")) {
\r
93 securityInfo.setAafUsername(entity.getString("aaf_username"));
\r
95 securityInfo.setSecureTopic(!key.endsWith("unsecure"));
\r
96 fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info"));
\r
97 return securityInfo;
\r
100 private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) {
\r
101 SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();
\r
102 if (jsonDmaapInfo.containsKey("location")){
\r
103 dmaapInfo.setLocation(jsonDmaapInfo.getString("location"));
\r
105 if (jsonDmaapInfo.containsKey("topic_url")) {
\r
106 dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url"));
\r
108 if (jsonDmaapInfo.containsKey("client_id")) {
\r
109 dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id"));
\r
111 if (jsonDmaapInfo.containsKey("client_role")) {
\r
112 dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role"));
\r
114 if (jsonDmaapInfo.containsKey("type")) {
\r
115 dmaapInfo.setType(jsonDmaapInfo.getString("type"));
\r
119 private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) {
\r
120 Set<Entry<String, Object>> entries = jsonObject.entrySet();
\r
121 for (Entry<String, Object> entry : entries) {
\r
122 if (entry.getKey().startsWith("holmes.default.rule")) {
\r
123 String value = (String) entry.getValue();
\r
124 String[] contents = value.split(RULE_CONTENT_SPLIT);
\r
125 ret.addDefaultRule(new Rule(entry.getKey(), contents[0], contents[1], 1));
\r