X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=holmes-actions%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Fdcae%2Futils%2FDcaeConfigurationParser.java;h=2c878b0f1b89bea3e9088d299fac6ea60722a063;hb=ec0a5672d39fc3caed1292abe1c4e1c2f814d2e5;hp=260b26a918beac8da5e97028499403964806c612;hpb=0cfd77aa83f4e3e314d125fbaef461e32099c0bc;p=holmes%2Fcommon.git 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 index 260b26a..2c878b0 100644 --- 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 @@ -13,15 +13,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.holmes.common.dcae.utils; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; 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; @@ -30,35 +30,34 @@ import org.onap.holmes.common.exception.CorrelationException; public class DcaeConfigurationParser { + private static final String RULE_CONTENT_SPLIT = "\\$\\$\\$"; + 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"); + "Can not resolve configurations from DCAE. The configuration string is empty."); } DcaeConfigurations ret = new DcaeConfigurations(); JSONObject jsonObject = null; try { - jsonObject = JSONObject.fromObject(jsonStr); + jsonObject = JSON.parseObject(jsonStr); } catch (Exception e) { throw new CorrelationException(e.getMessage(), e); } fillInRules(ret, jsonObject); fillInPublishesInfo(ret, jsonObject); - - if (jsonObject.containsKey("streams_subscribes")) { - - } + fillInSubscribesInfo(ret, jsonObject); 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)))); + .forEach(key -> ret.put(key, finalJsonObject.getString(String.valueOf(key)))); return ret; } @@ -72,13 +71,25 @@ public class DcaeConfigurationParser { } } + private static void fillInSubscribesInfo(DcaeConfigurations ret, JSONObject jsonObject) { + if (jsonObject.containsKey("streams_subscribes")) { + JSONObject subscribes = jsonObject.getJSONObject("streams_subscribes"); + for (Object key : subscribes.keySet()) { + ret.addSubSecInfo((String) key, + createSecurityInfo((String) key, subscribes.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")) { + if (entity.containsKey("type")) { + securityInfo.setType(entity.getString("type")); + } + if (entity.containsKey("aaf_password")) { securityInfo.setAafPassword(entity.getString("aaf_password")); } - if (!entity.get("aaf_username").equals("null")) { + if (entity.containsKey("aaf_username")) { securityInfo.setAafUsername(entity.getString("aaf_username")); } securityInfo.setSecureTopic(!key.endsWith("unsecure")); @@ -88,21 +99,30 @@ public class DcaeConfigurationParser { 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")) { + if (jsonDmaapInfo.containsKey("location")){ + dmaapInfo.setLocation(jsonDmaapInfo.getString("location")); + } + if (jsonDmaapInfo.containsKey("topic_url")) { + dmaapInfo.setTopicUrl(jsonDmaapInfo.getString("topic_url")); + } + if (jsonDmaapInfo.containsKey("client_id")) { dmaapInfo.setClientId(jsonDmaapInfo.getString("client_id")); } - if (!jsonDmaapInfo.get("client_role").equals("null")) { + if (jsonDmaapInfo.containsKey("client_role")) { dmaapInfo.setClientRole(jsonDmaapInfo.getString("client_role")); } + if (jsonDmaapInfo.containsKey("type")) { + dmaapInfo.setType(jsonDmaapInfo.getString("type")); + } } 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())); + String value = (String) entry.getValue(); + String[] contents = value.split(RULE_CONTENT_SPLIT); + ret.addDefaultRule(new Rule(entry.getKey(), contents[0], contents[1], 1)); } } }