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 java.util.Arrays;
\r
19 import java.util.List;
\r
20 import java.util.Map.Entry;
\r
21 import java.util.Set;
\r
22 import java.util.stream.Stream;
\r
23 import net.sf.json.JSONObject;
\r
24 import org.apache.commons.lang3.StringUtils;
\r
25 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
\r
26 import org.onap.holmes.common.dcae.entity.Rule;
\r
27 import org.onap.holmes.common.dcae.entity.SecurityInfo;
\r
28 import org.onap.holmes.common.exception.CorrelationException;
\r
30 public class DcaeConfigurationParser {
\r
32 private static final String RULE_CONTENT_SPLIT = "\\$\\$\\$";
\r
34 private static final List<String> OBJECT_ATTRS = Arrays
\r
35 .asList(new String[]{"streams_subscribes", "streams_publishes", "services_calls", "services_provides"});
\r
37 public static DcaeConfigurations parse(String jsonStr) throws CorrelationException {
\r
38 if (StringUtils.isEmpty(jsonStr)) {
\r
39 throw new CorrelationException(
\r
40 "Can not resolve configurations from DCAE. The configuration string is empty.");
\r
43 DcaeConfigurations ret = new DcaeConfigurations();
\r
45 JSONObject jsonObject = null;
\r
47 jsonObject = JSONObject.fromObject(jsonStr);
\r
48 } catch (Exception e) {
\r
49 throw new CorrelationException(e.getMessage(), e);
\r
52 fillInRules(ret, jsonObject);
\r
53 fillInPublishesInfo(ret, jsonObject);
\r
54 fillInSubscribesInfo(ret, jsonObject);
\r
56 JSONObject finalJsonObject = jsonObject;
\r
57 Stream.of(jsonObject.keySet().toArray(new String[0]))
\r
58 .filter(key -> !OBJECT_ATTRS.contains(key))
\r
59 .forEach(key -> ret.put(key.toString(), finalJsonObject.getString(String.valueOf(key))));
\r
63 private static void fillInPublishesInfo(DcaeConfigurations ret, JSONObject jsonObject) {
\r
64 if (jsonObject.containsKey("streams_publishes")) {
\r
65 JSONObject publishes = jsonObject.getJSONObject("streams_publishes");
\r
66 for (Object key : publishes.keySet()) {
\r
67 ret.addPubSecInfo((String) key,
\r
68 createSecurityInfo((String) key, publishes.getJSONObject((String) key)));
\r
73 private static void fillInSubscribesInfo(DcaeConfigurations ret, JSONObject jsonObject) {
\r
74 if (jsonObject.containsKey("streams_subscribes")) {
\r
75 JSONObject subscribes = jsonObject.getJSONObject("streams_subscribes");
\r
76 for (Object key : subscribes.keySet()) {
\r
77 ret.addSubSecInfo((String) key,
\r
78 createSecurityInfo((String) key, subscribes.getJSONObject((String) key)));
\r
83 private static SecurityInfo createSecurityInfo(String key, JSONObject entity) {
\r
84 SecurityInfo securityInfo = new SecurityInfo();
\r
85 if (entity.containsKey("type")) {
\r
86 securityInfo.setType(entity.getString("type"));
\r
88 if (entity.containsKey("aaf_password")) {
\r
89 securityInfo.setAafPassword(entity.getString("aaf_password"));
\r
91 if (entity.containsKey("aaf_username")) {
\r
92 securityInfo.setAafUsername(entity.getString("aaf_username"));
\r
94 securityInfo.setSecureTopic(!key.endsWith("unsecure"));
\r
95 fillInDmaapInfo(securityInfo, entity.getJSONObject("dmaap_info"));
\r
96 return securityInfo;
\r
99 private static void fillInDmaapInfo(SecurityInfo securityInfo, JSONObject jsonDmaapInfo) {
\r
100 SecurityInfo.DmaapInfo dmaapInfo = securityInfo.getDmaapInfo();
\r
101 if (jsonDmaapInfo.containsKey("location")){
\r
102 dmaapInfo.setLocation(jsonDmaapInfo.getString("location"));
\r
104 if (jsonDmaapInfo.containsKey("topic_url")) {
\r
105 dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url"));
\r
107 if (jsonDmaapInfo.containsKey("client_id")) {
\r
108 dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id"));
\r
110 if (jsonDmaapInfo.containsKey("client_role")) {
\r
111 dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role"));
\r
113 if (jsonDmaapInfo.containsKey("type")) {
\r
114 dmaapInfo.setType(jsonDmaapInfo.getString("type"));
\r
118 private static void fillInRules(DcaeConfigurations ret, JSONObject jsonObject) {
\r
119 Set<Entry<String, Object>> entries = jsonObject.entrySet();
\r
120 for (Entry<String, Object> entry : entries) {
\r
121 if (entry.getKey().startsWith("holmes.default.rule")) {
\r
122 String value = (String) entry.getValue();
\r
123 String[] contents = value.split(RULE_CONTENT_SPLIT);
\r
124 ret.addDefaultRule(new Rule(entry.getKey(), contents[0], contents[1], 1));
\r