From 89d3441bb04047d63ff8f4f586773d7c287e1deb Mon Sep 17 00:00:00 2001 From: Daniel Cruz Date: Fri, 22 Mar 2019 08:33:29 -0500 Subject: Add MDC Topic Filtering A feature to provide configurable properties for network topics to extrac fields from JSON strings and place them in a mapped diagnostic context. Issue-ID: POLICY-1499 Change-Id: Icfca0be3b263ccf1612b79ed617e2b1ffb0317e6 Signed-off-by: Daniel Cruz --- .../src/main/feature/config/logback-eelf.xml | 328 ++++++------ feature-mdc-filters/pom.xml | 119 +++++ feature-mdc-filters/src/assembly/assemble_zip.xml | 55 ++ .../feature/config/feature-mdc-filters.properties | 54 ++ .../src/main/feature/install/disable | 32 ++ .../src/main/feature/install/enable | 32 ++ .../drools/mdc/filters/MdcFilterFeature.java | 201 ++++++++ .../policy/drools/mdc/filters/MdcTopicFilter.java | 337 ++++++++++++ ...y.common.endpoints.features.NetLoggerFeatureApi | 1 + ...licy.drools.features.PolicyControllerFeatureAPI | 1 + .../drools/mdc/filters/MdcFilterFeatureTest.java | 170 +++++++ .../drools/mdc/filters/MdcTopicFilterTest.java | 564 +++++++++++++++++++++ feature-mdc-filters/src/test/resources/onset.json | 18 + packages/install/pom.xml | 6 + .../src/main/server/config/logback.xml | 246 ++++----- pom.xml | 1 + 16 files changed, 1877 insertions(+), 288 deletions(-) create mode 100755 feature-mdc-filters/pom.xml create mode 100755 feature-mdc-filters/src/assembly/assemble_zip.xml create mode 100755 feature-mdc-filters/src/main/feature/config/feature-mdc-filters.properties create mode 100755 feature-mdc-filters/src/main/feature/install/disable create mode 100755 feature-mdc-filters/src/main/feature/install/enable create mode 100755 feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcFilterFeature.java create mode 100755 feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java create mode 100755 feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.common.endpoints.features.NetLoggerFeatureApi create mode 100755 feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureAPI create mode 100755 feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java create mode 100755 feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java create mode 100755 feature-mdc-filters/src/test/resources/onset.json diff --git a/feature-eelf/src/main/feature/config/logback-eelf.xml b/feature-eelf/src/main/feature/config/logback-eelf.xml index 90b7f45e..c70a57a3 100644 --- a/feature-eelf/src/main/feature/config/logback-eelf.xml +++ b/feature-eelf/src/main/feature/config/logback-eelf.xml @@ -20,172 +20,166 @@ - - - - - - - - - - - - - - - - - - - - ${defaultPattern} - - - - - ${logDirectory}/${auditLogName}.log - - ${logDirectory}/${auditLogName}.%i.log.zip - - 1 - 5 - - - 10MB - - - ${defaultAuditPattern} - - - - - - - - - ${logDirectory}/${metricsLogName}.log - - ${logDirectory}/${metricsLogName}.%i.log.zip - - 1 - 5 - - - 15MB - - - ${defaultMetricPattern} - - - - - - - - - - ${logDirectory}/${errorLogName}.log - - ${logDirectory}/${errorLogName}.%i.log.zip - - 1 - 5 - - - ERROR - - - 20MB - - - ${defaultErrorPattern} - - - - - - - - - ${debugLogDirectory}/${debugLogName}.log - - ${debugLogDirectory}/${debugLogName}.%i.log.zip - - 1 - 5 - - - 20MB - - - ${debugPattern} - - - - - - - - - ${logDirectory}/${networkLogName}.log - - ${logDirectory}/${networkLogName}.%i.log.zip - - 1 - 5 - - - 15MB - - - ${networkPattern} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + ${defaultPattern} + + + + + ${logDirectory}/${auditLogName}.log + + ${logDirectory}/${auditLogName}.%i.log.zip + + 1 + 5 + + + 10MB + + + ${defaultAuditPattern} + + + + + + + + + ${logDirectory}/${metricsLogName}.log + + ${logDirectory}/${metricsLogName}.%i.log.zip + + 1 + 5 + + + 15MB + + + ${defaultMetricPattern} + + + + + + + + + + ${logDirectory}/${errorLogName}.log + + ${logDirectory}/${errorLogName}.%i.log.zip + + 1 + 5 + + + ERROR + + + 20MB + + + ${defaultErrorPattern} + + + + + + + + + ${debugLogDirectory}/${debugLogName}.log + + ${debugLogDirectory}/${debugLogName}.%i.log.zip + + 1 + 5 + + + 20MB + + + ${debugPattern} + + + + + + + + + ${logDirectory}/${networkLogName}.log + + ${logDirectory}/${networkLogName}.%i.log.zip + + 1 + 5 + + + 15MB + + + ${networkPattern} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/feature-mdc-filters/pom.xml b/feature-mdc-filters/pom.xml new file mode 100755 index 00000000..6ea654ba --- /dev/null +++ b/feature-mdc-filters/pom.xml @@ -0,0 +1,119 @@ + + + + 4.0.0 + + drools-pdp + org.onap.policy.drools-pdp + 1.4.0-SNAPSHOT + + + feature-mdc-filters + + feature-mdc-filters + Loadable module that marks key fields in messages through logback mdc. + + + 1.8 + 1.8 + + + + + + maven-assembly-plugin + + + zipfile + + single + + package + + true + ${project.artifactId}-${project.version} + + src/assembly/assemble_zip.xml + + false + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + + copy-dependencies + + prepare-package + + ${project.build.directory}/assembly/lib + false + true + true + false + false + false + runtime + true + + + + + + + + + + + org.onap.policy.common + policy-endpoints + ${policy.common.version} + provided + + + + org.onap.policy.drools-pdp + policy-management + ${project.version} + provided + + + + org.mockito + mockito-core + 2.13.0 + test + + + + junit + junit + test + + + + diff --git a/feature-mdc-filters/src/assembly/assemble_zip.xml b/feature-mdc-filters/src/assembly/assemble_zip.xml new file mode 100755 index 00000000..c85bbe52 --- /dev/null +++ b/feature-mdc-filters/src/assembly/assemble_zip.xml @@ -0,0 +1,55 @@ + + + + + + feature-mdc-filters + + zip + + + false + + + + target + lib/feature + + feature-mdc-filters-${project.version}.jar + + + + src/main/feature/config + config + 0644 + + + + src/main/feature/install + install + 0744 + + + + + diff --git a/feature-mdc-filters/src/main/feature/config/feature-mdc-filters.properties b/feature-mdc-filters/src/main/feature/config/feature-mdc-filters.properties new file mode 100755 index 00000000..0a53a9f5 --- /dev/null +++ b/feature-mdc-filters/src/main/feature/config/feature-mdc-filters.properties @@ -0,0 +1,54 @@ +### +# ============LICENSE_START======================================================= +# feature-mdc-filters +# ================================================================================ +# Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +### + +# The properties keys follow the controller topic configurations followed by a +# a new topic property, 'mdcFilters'. +#..topics..mdcFilters + +# The value of the property is broken down to the MDC key name to be used by the +# feature followed by the path(s) to the desired field's value. +#dmaap.sink.topics.example.mdcFilters=sampleKey=$.path.to.sample.key + +# The path always begins with '$' as this signifies the root of the JSON document. +# The underlying library used is Jayway JsonPath. The library's query syntax is +# supported for searching a JSON document. The query syntax and some examples +# can be found at: https://github.com/json-path/JsonPath + +# Multiple fields can be found for a given JSON document by a comma separated list +# of pairs. +#dmaap.sink.topics.example.mdcFilters=field1=$.field1,field2=$.field2 + +# If a given topic supports multiple message types that have fields with the same +# name, a '|' separated list can define multiple paths to a field. The feature +# will loop through each path until it finds a match and returns it. +#dmaap.sink.topics.example.mdcFilters=field1=$.field1|$.body.field1 + +# dmaap source filters +dmaap.source.topics.PDPD-CONFIGURATION.mdcFilters=requestID=$.requestID +dmaap.source.topics.DCAE_TOPIC.mdcFilters=requestID=$.requestID +dmaap.source.topics.APPC-CL.mdcFilters=requestID=$.CommonHeader.RequestID +dmaap.source.topics.APPC-LCM-WRITE.mdcFilters=requestID=$.body.output.common-header.request-id +dmaap.source.topics.SDNR-CL-RSP.mdcFilters=requestID=$.body.CommonHeader.RequestID + +# dmaap sink filters +dmaap.sink.topics.POLICY-CL-MGT.mdcFilters=requestID=$.requestID +dmaap.sink.topics.APPC-CL.mdcFilters=requestID=$.CommonHeader.RequestID +dmaap.sink.topics.APPC-LCM-READ.mdcFilters=requestID=$.body.input.common-header.request-id +dmaap.sink.topics.SDNR-CL.mdcFilters=requestID=$.body.CommonHeader.RequestID diff --git a/feature-mdc-filters/src/main/feature/install/disable b/feature-mdc-filters/src/main/feature/install/disable new file mode 100755 index 00000000..25d57374 --- /dev/null +++ b/feature-mdc-filters/src/main/feature/install/disable @@ -0,0 +1,32 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# feature-mdc-filters +# ================================================================================ +# Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +## + +if [[ "${DEBUG}" == "y" ]]; then + set -x +fi + +CONFIG_DIR="${POLICY_HOME}"/config +for mainConfig in ${CONFIG_DIR}/logback.xml ${CONFIG_DIR}/logback-eelf.xml; do + if [ -e "${mainConfig}" ]; then + sed -i --follow-symlinks 's/${abstractNetworkPattern}/${networkPattern}/' "${mainConfig}" + fi +done diff --git a/feature-mdc-filters/src/main/feature/install/enable b/feature-mdc-filters/src/main/feature/install/enable new file mode 100755 index 00000000..57c7cc4a --- /dev/null +++ b/feature-mdc-filters/src/main/feature/install/enable @@ -0,0 +1,32 @@ +#!/bin/bash + +### +# ============LICENSE_START======================================================= +# feature-mdc-filters +# ================================================================================ +# Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. +# ================================================================================ +# 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. +# ============LICENSE_END========================================================= +## + +if [[ "${DEBUG}" == "y" ]]; then + set -x +fi + +CONFIG_DIR="${POLICY_HOME}"/config +for mainConfig in ${CONFIG_DIR}/logback.xml ${CONFIG_DIR}/logback-eelf.xml; do + if [ -e "${mainConfig}" ]; then + sed -i --follow-symlinks 's/${networkPattern}/${abstractNetworkPattern}/' "${mainConfig}" + fi +done diff --git a/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcFilterFeature.java b/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcFilterFeature.java new file mode 100755 index 00000000..369c0aa0 --- /dev/null +++ b/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcFilterFeature.java @@ -0,0 +1,201 @@ +/* + * ============LICENSE_START======================================================= + * feature-mdc-filters + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.mdc.filters; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import org.onap.policy.common.endpoints.event.comm.Topic; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.endpoints.features.NetLoggerFeatureApi; +import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; +import org.onap.policy.drools.features.PolicyControllerFeatureAPI; +import org.onap.policy.drools.persistence.SystemPersistence; +import org.onap.policy.drools.system.PolicyController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +public class MdcFilterFeature implements NetLoggerFeatureApi, PolicyControllerFeatureAPI { + + /** + * Logger. + */ + private static final Logger logger = LoggerFactory.getLogger(MdcFilterFeature.class); + + /** + * Feature properties. + */ + public static final String FEATURE_NAME = "feature-mdc-filters"; + public static final String SOURCE = "source"; + public static final String SINK = "sink"; + public static final String MDC_FILTERS = ".mdcFilters"; + + /** + * Mapping of 'protocol:type:topic' key to a 'MdcTopicFilter' object. + */ + private Map topicFilters = new HashMap<>(); + + /** + * Feature properties map obtained from the feature properties file. + */ + private Properties featureProps = null; + + /** + * Constructor. + */ + public MdcFilterFeature() { + super(); + featureProps = getFeatureProps(); + } + + /** + * Gets the feature properties. + * + * @return the properties for this feature. + */ + protected Properties getFeatureProps() { + return SystemPersistence.manager.getProperties(FEATURE_NAME); + } + + /** + * Sequence number to be used for order of feature implementer execution. + */ + @Override + public int getSequenceNumber() { + return 1; + } + + /** + * Loops through all source and sink topics to find which topics have mdc filters and + * inserts an MdcTopicFilter in to the topicFilters map. + */ + @Override + public boolean afterCreate(PolicyController controller) { + createSourceTopicFilters(controller); + createSinkTopicFilters(controller); + return false; + } + + /** + * Extracts the fields in a JSON string that are to be logged in an abbreviated + * message. The event delivery infrastructure details are put in the MDC as well using + * the keys networkEventType (IN/OUT), networkProtocol (UEB/DMAAP/NOOP/REST), and + * networkTopic. + */ + @Override + public boolean beforeLog(Logger eventLogger, EventType type, CommInfrastructure protocol, String topic, + String message) { + + String filterKey = null; + if (type == EventType.IN) { + filterKey = getTopicKey(protocol.name().toLowerCase(), SOURCE, topic); + } else { + filterKey = getTopicKey(protocol.name().toLowerCase(), SINK, topic); + } + + MDC.put("networkEventType", type.name()); + MDC.put("networkProtocol", protocol.name()); + MDC.put("networkTopic", topic); + + MdcTopicFilter filter = topicFilters.get(filterKey); + if (filter != null) { + for (Map.Entry> entry : filter.find(message).entrySet()) { + String mdcKey = entry.getKey(); + List results = entry.getValue(); + if (results.isEmpty()) { + logger.debug("No results found for key {}", mdcKey); + } else if (results.size() > 1) { + logger.debug("Multple results found for key {}, returning list as a string", mdcKey); + MDC.put(mdcKey, results.toString()); + } else { + MDC.put(mdcKey, results.get(0)); + } + } + } else { + logger.debug("No mdc topic filters exist for key {}", filterKey); + } + + return false; + } + + /** + * Clears the MDC mapping after a message is logged. + */ + @Override + public boolean afterLog(Logger eventLogger, EventType type, CommInfrastructure protocol, String topic, + String message) { + MDC.clear(); + return false; + } + + /** + * Creates a key using the protocol, type, and topic name. + * + * @param protocol defined as ueb, dmaap, noop + * @param type defined as source or sink + * @param topic name of the topic + * @return a key that is the concatenation of the protocol, type, and topic name + */ + private String getTopicKey(String protocol, String type, String topic) { + return protocol + ":" + type + ":" + topic; + } + + /** + * Creates MdcTopicFilters for a source/sink topic based on the type. + * + * @param topic the topic name + * @param type 'source' or 'sink' + */ + private void createTopicFilter(Topic topic, String type) { + String protocol = topic.getTopicCommInfrastructure().name().toLowerCase(); + String topicName = topic.getTopic(); + + String propertyKey = protocol + "." + type + ".topics." + topicName + MDC_FILTERS; + String propertyValue = featureProps.getProperty(propertyKey); + if (propertyValue != null) { + String topicKey = getTopicKey(protocol, type, topicName); + if (!topicFilters.containsKey(topicKey)) { + logger.debug("MdcTopicFilter created for {} {} topic {}", protocol, type, topicName); + topicFilters.put(topicKey, new MdcTopicFilter(propertyValue)); + } else { + logger.debug("An MdcTopicFilter already exists for key {}", topicKey); + } + } else { + logger.debug("No MDC filters defined for {} {} topic {}", protocol, type, topicName); + } + } + + /** + * Creates MdcTopicFilters for the controller's source topics. + */ + private void createSourceTopicFilters(PolicyController controller) { + controller.getTopicSources().forEach(sourceTopic -> createTopicFilter(sourceTopic, SOURCE)); + } + + /** + * Creates MdcTopicFilters for the controller's sink topics. + */ + private void createSinkTopicFilters(PolicyController controller) { + controller.getTopicSinks().forEach(sinkTopic -> createTopicFilter(sinkTopic, SINK)); + } +} diff --git a/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java b/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java new file mode 100755 index 00000000..d0813a93 --- /dev/null +++ b/feature-mdc-filters/src/main/java/org/onap/policy/drools/mdc/filters/MdcTopicFilter.java @@ -0,0 +1,337 @@ +/* + * ============LICENSE_START======================================================= + * feature-mdc-filters + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.mdc.filters; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.onap.policy.drools.protocol.coders.JsonProtocolFilter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MdcTopicFilter { + + private static final Logger logger = LoggerFactory.getLogger(MdcTopicFilter.class); + + public static final String MDC_KEY_ERROR = "mdcKey must be provided"; + public static final String JSON_PATH_ERROR = "json path(s) must be provided"; + + private Map rules = new HashMap<>(); + + public static class FilterRule { + private String mdcKey; + private List paths; + + public FilterRule(String mdcKey, String path) { + this.mdcKey = mdcKey; + this.paths = Arrays.asList(path); + } + + /** + * Constructor. + * + * @param mdcKey the key to the filter rule + * @param paths the list of potential paths to the key + */ + public FilterRule(String mdcKey, List paths) { + this.mdcKey = mdcKey; + this.paths = paths; + } + + public String getMdcKey() { + return mdcKey; + } + + public List getPaths() { + return paths; + } + + protected void setMdcKey(String mdcKey) { + if (mdcKey == null || mdcKey.isEmpty()) { + throw new IllegalArgumentException(MDC_KEY_ERROR); + } + this.mdcKey = mdcKey; + } + + protected void setPaths(List paths) { + if (paths == null || paths.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + this.paths = paths; + } + + protected void addPaths(List paths) { + if (paths == null || paths.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + this.paths.addAll(paths); + } + + protected void addPath(String path) { + if (path == null || path.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + this.paths.add(path); + } + } + + protected MdcTopicFilter(String rawFilters) { + for (String filter : rawFilters.split("\\s*,\\s*")) { + FilterRule rule = createFilterRule(filter); + rules.put(rule.mdcKey, rule); + } + } + + private FilterRule createFilterRule(String filter) { + String[] filterKeyPaths = filter.split("\\s*=\\s*"); + if (filterKeyPaths.length != 2) { + throw new IllegalArgumentException("could not parse filter rule"); + } + + String filterKey = filterKeyPaths[0]; + String paths = filterKeyPaths[1]; + List filterPaths = new ArrayList<>(Arrays.asList(paths.split("(? getFilterRule() { + return new ArrayList<>(rules.values()); + } + + /** + * Gets the filter rule for the specified key. + * + * @param mdcKey the key to the filter rule + * @return the filter rule associated with the key + */ + protected FilterRule getFilterRule(String mdcKey) { + if (mdcKey == null || mdcKey.isEmpty()) { + throw new IllegalArgumentException(MDC_KEY_ERROR); + } + return rules.get(mdcKey); + } + + /** + * Adds a filter rule for the specified key and path. + * + * @param mdcKey the key to the filter rule + * @param path the json path to the key + * @return the filter rule that was added for the topic + */ + protected FilterRule addFilterRule(String mdcKey, String path) { + if (path == null || path.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + return addFilterRule(mdcKey, Arrays.asList(path)); + } + + /** + * Adds a filter rule for the specified key and paths. + * + * @param mdcKey the key to the filter rule + * @param paths the list of potential paths to the key + * @return the filter rule that was added for the topic + */ + protected FilterRule addFilterRule(String mdcKey, List paths) { + if (mdcKey == null || mdcKey.isEmpty()) { + throw new IllegalArgumentException(MDC_KEY_ERROR); + } + + if (paths == null || paths.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + + if (rules.containsKey(mdcKey)) { + throw new IllegalArgumentException("a filter rule already exists for key: " + mdcKey); + } + + FilterRule rule = new FilterRule(mdcKey, paths); + rules.put(mdcKey, rule); + return rule; + } + + /** + * Modifies an existing filter rule by adding the specified path. + * + * @param mdcKey the key to the filter rule + * @param path the path to the key + * @return the filter rule that was modified + */ + protected FilterRule modifyFilterRule(String mdcKey, String path) { + if (path == null || path.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + return modifyFilterRule(mdcKey, Arrays.asList(path)); + } + + /** + * Modifies an existing filter rule by adding the specified paths. + * + * @param mdcKey the key to the filter rule + * @param paths the list of potential paths to the key + * @return the filter rule that was modified + */ + protected FilterRule modifyFilterRule(String mdcKey, List paths) { + if (mdcKey == null || mdcKey.isEmpty()) { + throw new IllegalArgumentException(MDC_KEY_ERROR); + } + + if (paths == null || paths.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + + if (!rules.containsKey(mdcKey)) { + throw new IllegalArgumentException("a filter rule doesn't exist for key: " + mdcKey); + } + + FilterRule rule = rules.get(mdcKey); + rule.addPaths(paths); + return rule; + } + + /** + * Modifies an existing filter rule's key and replaces the paths with the specified + * paths. + * + * @param oldMdcKey the old key to the filter rule + * @param newMdcKey the new key to the filter rule + * @param paths the list of potential paths to the key + * @return the filter rule that was modified + */ + protected FilterRule modifyFilterRule(String oldMdcKey, String newMdcKey, List paths) { + if (oldMdcKey == null || oldMdcKey.isEmpty()) { + throw new IllegalArgumentException("current mdcKey must be provided"); + } + + if (newMdcKey == null || newMdcKey.isEmpty()) { + throw new IllegalArgumentException("new mdcKey must be provided"); + } + + if (oldMdcKey.equals(newMdcKey)) { + throw new IllegalArgumentException("the old and new mdcKey are equivalent"); + } + if (paths == null || paths.isEmpty()) { + throw new IllegalArgumentException(JSON_PATH_ERROR); + } + + if (rules.containsKey(newMdcKey)) { + throw new IllegalArgumentException("a filter rule already exists for key: " + newMdcKey); + } + + FilterRule rule = rules.remove(oldMdcKey); + if (rule == null) { + throw new IllegalArgumentException("a filter rule doesn't exist for key: " + oldMdcKey); + } + + rule.setMdcKey(newMdcKey); + rule.setPaths(paths); + rules.put(newMdcKey, rule); + return rule; + } + + /** + * Deletes all filter rules for the topic filter. + */ + protected void deleteFilterRule() { + rules.clear(); + } + + /** + * Deletes an existing filter rule. + * + * @param mdcKey the key to the filter rule + * @return the filter rule that was deleted + */ + protected FilterRule deleteFilterRule(String mdcKey) { + if (mdcKey == null || mdcKey.isEmpty()) { + throw new IllegalArgumentException(MDC_KEY_ERROR); + } + return rules.remove(mdcKey); + } + + /** + * Finds all fields for each topic filter rule. The results are stored in a map that + * is indexed by the MDC key. Each MDC key has a list of results as multiple + * occurrences of a key can be found in a JSON document. + * + * @param json the json string to be parsed + * @return a map of mdc keys and list of results for each key + */ + protected Map> find(String json) { + Map> results = new HashMap<>(); + for (FilterRule rule : rules.values()) { + List matches = new ArrayList<>(); + for (String path : rule.getPaths()) { + + try { + matches = JsonProtocolFilter.filter(json, path); + } catch (Exception e) { + logger.debug("Could not filter on path {} because of {}", path, e.getMessage(), e); + } + + if (!matches.isEmpty()) { + break; + } else { + logger.error("Could not find path {} in json {}", path, json); + } + + } + results.put(rule.getMdcKey(), matches); + } + return results; + } + + /** + * Finds all occurrences of a field in a JSON document based on the filter rule paths. + * + * @param json the json string to be parsed + * @return a list of matches from the JSON document + */ + protected List find(String json, String mdcKey) { + List matches = new ArrayList<>(); + for (String path : rules.get(mdcKey).getPaths()) { + + try { + matches = JsonProtocolFilter.filter(json, path); + } catch (Exception e) { + logger.debug("Could not filter on path {} because of {}", path, e.getMessage(), e); + } + + if (!matches.isEmpty()) { + break; + } + + } + + if (matches.isEmpty()) { + logger.error("Could not find any matches for key {} in json {}", mdcKey, json); + } + + return matches; + } +} diff --git a/feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.common.endpoints.features.NetLoggerFeatureApi b/feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.common.endpoints.features.NetLoggerFeatureApi new file mode 100755 index 00000000..f2fdb402 --- /dev/null +++ b/feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.common.endpoints.features.NetLoggerFeatureApi @@ -0,0 +1 @@ +org.onap.policy.drools.mdc.filters.MdcFilterFeature diff --git a/feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureAPI b/feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureAPI new file mode 100755 index 00000000..f2fdb402 --- /dev/null +++ b/feature-mdc-filters/src/main/resources/META-INF/services/org.onap.policy.drools.features.PolicyControllerFeatureAPI @@ -0,0 +1 @@ +org.onap.policy.drools.mdc.filters.MdcFilterFeature diff --git a/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java new file mode 100755 index 00000000..afe6df35 --- /dev/null +++ b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcFilterFeatureTest.java @@ -0,0 +1,170 @@ +/* + * ============LICENSE_START======================================================= + * feature-mdc-filters + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.mdc.filters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Properties; + +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; +import org.onap.policy.common.endpoints.event.comm.TopicSink; +import org.onap.policy.common.endpoints.event.comm.TopicSource; +import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; +import org.onap.policy.drools.system.PolicyController; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + +public class MdcFilterFeatureTest { + + /** + * Logger. + */ + private static final Logger logger = LoggerFactory.getLogger(MdcFilterFeatureTest.class); + + /** + * Test topic names for mdc topic filters. + */ + private static final String TEST_TOPIC_A = "org.onap.policy.test-topic-a"; + private static final String TEST_TOPIC_B = "org.onap.policy.test-topic-b"; + + /** + * The mock PolicyController to be used for the junits. + */ + private PolicyController controller; + + /** + * The mock properties to be used for the junits. + */ + private Properties props; + + /** + * An instance of the MdcFilterFeature. + */ + private MdcFilterFeature mdcFilterFeature; + + /** + * Sample json string to be logged. + */ + private String message; + + /** + * Setup. + * @throws IOException thrown if onset.json file could not be read + */ + @Before + public void setUp() throws IOException { + message = new String(Files.readAllBytes(Paths.get("src/test/resources/onset.json"))); + + props = mockFeatureProperties(); + controller = mock(PolicyController.class); + + props.setProperty("dmaap.source.topics", TEST_TOPIC_A); + props.setProperty("dmaap.source.topics." + TEST_TOPIC_A + ".servers", "http://testing123.com/"); + props.setProperty("noop.sink.topics", TEST_TOPIC_B); + + List topicSources = TopicEndpoint.manager.addTopicSources(props); + doReturn(topicSources).when(controller).getTopicSources(); + + List topicSinks = TopicEndpoint.manager.addTopicSinks(props); + doReturn(topicSinks).when(controller).getTopicSinks(); + + mdcFilterFeature = new MdcFilterFeatureImpl(); + mdcFilterFeature.afterCreate(controller); + } + + /** + * Tests extracting fields from a JSON message and place them in the MDC and + * then clearing the MDC. + */ + @Test + public void mdcLogTest() { + mdcFilterFeature.beforeLog(logger, EventType.IN, + CommInfrastructure.DMAAP, TEST_TOPIC_A, message); + + assertEquals("8c1b8bd8-06f7-493f-8ed7-daaa4cc481bc", MDC.get("requestID")); + assertEquals("CL-TEST", MDC.get("closedLoopControlName")); + + assertNotNull(MDC.getCopyOfContextMap()); + + mdcFilterFeature.afterLog(logger, EventType.IN, + CommInfrastructure.DMAAP, TEST_TOPIC_A, message); + + assertNull(MDC.getCopyOfContextMap()); + } + + /** + * Tests that the feature does not search for fields in a JSON message + * if there is not an MdcTopicFilter object for the generated key. + */ + @Test + public void noTopicFilterTest() { + mdcFilterFeature.beforeLog(logger, EventType.OUT, + CommInfrastructure.NOOP, "no-topic", message); + + assertEquals("OUT", MDC.get("networkEventType")); + assertEquals("NOOP", MDC.get("networkProtocol")); + assertEquals("no-topic", MDC.get("networkTopic")); + assertNull(MDC.get("requestID")); + } + + /** + * Creates a simple properties map containing an mdc filter for a test + * topic. + * + * @return a properties map with mdc filter properties. + */ + private Properties mockFeatureProperties() { + Properties props = new Properties(); + + String key = "dmaap.source.topics." + TEST_TOPIC_A + ".mdcFilters"; + String value = "requestID=$.requestID,closedLoopControlName=$.closedLoopControlName"; + props.setProperty(key, value); + + return props; + } + + /** + * Subclass of MdcFilterFeature for junit usage. + */ + private class MdcFilterFeatureImpl extends MdcFilterFeature { + + public MdcFilterFeatureImpl() { + super(); + } + + @Override + protected Properties getFeatureProps() { + return props; + } + } +} diff --git a/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java new file mode 100755 index 00000000..2a5a875d --- /dev/null +++ b/feature-mdc-filters/src/test/java/org/onap/policy/drools/mdc/filters/MdcTopicFilterTest.java @@ -0,0 +1,564 @@ +/* + * ============LICENSE_START======================================================= + * feature-mdc-filters + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.mdc.filters; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.onap.policy.drools.mdc.filters.MdcTopicFilter.FilterRule; + +public class MdcTopicFilterTest { + + /** + * Test the simple case of having one filter rule for a key. + */ + @Test + public void singleFilterOnePathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals("[$.requestID]", rule.getPaths().toString()); + } + + /** + * Tests having one filter rule with a set of potential paths to the key. + */ + @Test + public void singleFilterMultiPathTest() { + String topicFilterProp = "requestID=$.requestID|$.request-id"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals(2, rule.getPaths().size()); + assertEquals("[$.requestID, $.request-id]", rule.getPaths().toString()); + } + + /** + * Tests having two filter rules that each have one key/path pair. + */ + @Test + public void multiFilterSinglePathTest() { + String topicFilterProp = "requestID=$.requestID,closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals(1, rule.getPaths().size()); + assertEquals("[$.requestID]", rule.getPaths().toString()); + + FilterRule rule2 = topicFilter.getFilterRule("closedLoopControlName"); + assertEquals("closedLoopControlName", rule2.getMdcKey()); + assertEquals(1, rule2.getPaths().size()); + assertEquals("[$.closedLoopControlName]", rule2.getPaths().toString()); + } + + /** + * Tests having two filter rules that each have two key/path pairs. + */ + @Test + public void multiFilterMultiPathTest() { + String topicFilterProp = "requestID=$.requestID|$.body.request-id," + + "closedLoopControlName=$.closedLoopControlName" + + "|$.body.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertEquals("requestID", rule.getMdcKey()); + assertEquals(2, rule.getPaths().size()); + assertEquals("[$.requestID, $.body.request-id]", rule.getPaths().toString()); + + FilterRule rule2 = topicFilter.getFilterRule("closedLoopControlName"); + assertEquals("closedLoopControlName", rule2.getMdcKey()); + assertEquals(2, rule2.getPaths().size()); + assertEquals("[$.closedLoopControlName, $.body.closedLoopControlName]", rule2.getPaths().toString()); + } + + /** + * Tests that the regex split logic for '|' in the feature code doesn't + * break parsing when "||" is used as a predicate in a JsonPath query. + */ + @Test + public void addOrPredicateFilterTest() { + String topicFilterProp = "requestID=$.requestID||$.body.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + assertEquals(1, topicFilter.getFilterRule().size()); + assertEquals("requestID", topicFilter.getFilterRule("requestID").getMdcKey()); + assertEquals(Arrays.asList("$.requestID||$.body.requestID"), topicFilter + .getFilterRule("requestID").getPaths()); + } + + /** + * Tests getting all filter rules for a given topic. + */ + @Test + public void getAllFilterRulesTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + assertEquals(3, topicFilter.getFilterRule().size()); + } + + /** + * Tests getting a filter rule by its key. + */ + @Test + public void getFilterRuleTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.getFilterRule("requestID"); + assertNotNull(rule); + } + + /** + * Tests throwing an exception for passing in a null key. + */ + @Test(expected = IllegalArgumentException.class) + public void getFilterRuleNullKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.getFilterRule(null); + } + + /** + * Tests throwing an exception for passing in an empty key. + */ + @Test(expected = IllegalArgumentException.class) + public void getFilterRuleEmptyKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.getFilterRule(""); + } + + /** + * Tests adding a filter rule with a single path. + */ + @Test + public void addFilterRuleSinglePathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + String key = "subRequestID"; + String path = "$.subRequestID"; + FilterRule rule = topicFilter.addFilterRule(key, path); + assertEquals(topicFilter.getFilterRule(key), rule); + } + + /** + * Tests adding a filter rule with multiple paths. + */ + @Test + public void addFilterRuleMultiPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + String key = "subRequestID"; + List paths = Arrays.asList("$.subRequestID", "$.sub-request-id"); + FilterRule rule = topicFilter.addFilterRule(key, paths); + assertEquals(topicFilter.getFilterRule(key), rule); + } + + /** + * Tests throwing an exception for passing a null key and a + * single path. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleNullKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule(null, "$.subRequestID"); + } + + /** + * Tests throwing an exception for passing a null key and a list + * of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleNullKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule(null, Arrays.asList("$.subRequestID")); + } + + /** + * Tests throwing an exception for passing an empty key and + * a single path. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("", "$.subRequestID"); + } + + /** + * Tests throwing an exception for passing an empty key and + * a list of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("", Arrays.asList("$.subRequestID")); + } + + /** + * Tests throwing an exception for passing an empty path string. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("subRequestID", ""); + } + + /** + * Tests throwing an exception for passing an empty paths list. + */ + @Test(expected = IllegalArgumentException.class) + public void addFilterRuleEmptyPathsTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("subRequestID", Arrays.asList()); + } + + /** + * Tests throwing an exception for trying to add a filter with a key that + * already exists with a single filter. + */ + @Test(expected = IllegalArgumentException.class) + public void addExistingFilterRuleStringTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("requestID", "$.test"); + } + + /** + * Tests throwing an exception for trying to add a filter with a key that + * already exists with a list of filters. + */ + @Test(expected = IllegalArgumentException.class) + public void addExistingFilterRuleListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.addFilterRule("requestID", Arrays.asList("$.test")); + } + + /** + * Tests modifying a filter rule to add a new path. + */ + @Test + public void modifyFilterRuleSinglePathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.modifyFilterRule("requestID", "$.request-id"); + assertEquals(topicFilter.getFilterRule("requestID"), rule); + assertEquals(Arrays.asList("$.requestID", "$.request-id"), rule.getPaths()); + } + + /** + * Tests modifying a filter rule to add a list of new paths. + */ + @Test + public void modifyFilterRuleMultiPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.modifyFilterRule("requestID", + Arrays.asList("$.request-id", "$.requestId")); + assertEquals(topicFilter.getFilterRule("requestID"), rule); + assertEquals( + Arrays.asList("$.requestID", "$.request-id", "$.requestId"), + rule.getPaths()); + } + + /** + * Tests modifying a filter rule key. + */ + @Test + public void modifyFilterRuleKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + FilterRule rule = topicFilter.modifyFilterRule("requestID", + "request-id", Arrays.asList("$.request-id")); + assertEquals(topicFilter.getFilterRule("request-id"), rule); + assertEquals("[$.request-id]", rule.getPaths().toString()); + } + + /** + * Tests throwing an exception when passing a null key and + * a single path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule(null, "$.request-id"); + } + + /** + * Tests throwing an exception when passing a null key and + * a list of multiple paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule(null, Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty key and + * a single path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("", "$.request-id"); + } + + /** + * Tests throwing an exception when passing an empty key and + * a list of multiple paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty string path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyPathStringTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", ""); + } + + /** + * Tests throwing an exception when passing an empty list of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", Arrays.asList()); + } + + /** + * Tests throwing an exception when passing a key that is + * not in the filter rules map and a string path. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleMissingKeyStringPathTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("request-id", "$.request-id"); + } + + /** + * Tests throwing an exception when passing a key that is + * not in the filter rules map and a list of paths. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleMissingKeyPathListTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("request-id", Arrays.asList("$.request-id")); + } + + + /** + * Tests throwing an exception when passing a null oldKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullOldKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule(null, "request-id", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty oldKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyOldKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("", "request-id", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing a null newKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNullNewKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", null, Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty newKey. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyNewKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", "", Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when the old and new key are the same. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleSameKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", "requestID", + Arrays.asList("$.request-id")); + } + + /** + * Tests throwing an exception when passing an empty paths list. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleEmptyPathsTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("requestID", "request-id", Arrays.asList()); + } + + /** + * Tests throwing an exception when the old key doesn't exist + * in the rules map. + */ + @Test(expected = IllegalArgumentException.class) + public void modifyFilterRuleNonExistingOldKeyTest() { + String topicFilterProp = "requestID=$.requestID"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.modifyFilterRule("request-id", "id", Arrays.asList("$.request-id")); + } + + /** + * Tests deleting all filter rules in the rules map. + */ + @Test + public void deleteAllFilterRulesTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + assertEquals(3, topicFilter.getFilterRule().size()); + topicFilter.deleteFilterRule(); + assertEquals(0, topicFilter.getFilterRule().size()); + } + + /** + * Tests deleting a single filter rule by its key from the rules map. + */ + @Test + public void deleteFilterRuleTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + assertEquals(3, topicFilter.getFilterRule().size()); + topicFilter.deleteFilterRule("closedLoopControlName"); + assertEquals(2, topicFilter.getFilterRule().size()); + } + + /** + * Tests throwing an exception if the key is null. + */ + @Test(expected = IllegalArgumentException.class) + public void deleteFilterRuleNullKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.deleteFilterRule(null); + } + + /** + * Tests throwing an exception if the key is empty. + */ + @Test(expected = IllegalArgumentException.class) + public void deleteFilterRuleEmptyKeyTest() { + String topicFilterProp = "requestID=$.requestID,subRequestID=$.subRequestID," + + "closedLoopControlName=$.closedLoopControlName"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + topicFilter.deleteFilterRule(""); + } + + /** + * Tests finding all results for each filter rule corresponding to a topic. + */ + @Test + public void findAllTest() { + String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\"," + + "\"controllers\":[{\"name\":\"test-controller\"," + + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\"," + + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}"; + + String topicFilterProp = "requestID=$.requestID,controllerName=$.controllers[0].name," + + "operation=$.controllers[0].operation"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + Map> results = topicFilter.find(message); + assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", + results.get("requestID").get(0)); + assertEquals("test-controller", results.get("controllerName").get(0)); + assertEquals("update", results.get("operation").get(0)); + } + + /** + * Tests finding field matches for a filter rule corresponding to a topic. + */ + @Test + public void findTest() { + String message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\"," + + "\"controllers\":[{\"name\":\"test-controller\"," + + "\"drools\":{\"groupId\":\"org.onap.policy.drools.test\"," + + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}"; + + String topicFilterProp = "requestID=$.requestID,controllerName=$.controllers[0].name," + + "operation=$.controllers[0].operation"; + MdcTopicFilter topicFilter = new MdcTopicFilter(topicFilterProp); + + List results = topicFilter.find(message, "requestID"); + assertEquals("38adde30-cc22-11e8-a8d5-f2801f1b9fd1", results.get(0)); + } +} diff --git a/feature-mdc-filters/src/test/resources/onset.json b/feature-mdc-filters/src/test/resources/onset.json new file mode 100755 index 00000000..fafc93a3 --- /dev/null +++ b/feature-mdc-filters/src/test/resources/onset.json @@ -0,0 +1,18 @@ +{ + "closedLoopEventClient": "DCAE_INSTANCE_ID.dcae-tca", + "policyVersion": "1.0.0.5", + "policyName": "policyTest", + "policyScope": "type=SampleType,closedLoopControlName=SampleClosedLoop", + "target_type": "VM", + "AAI": { + "vserver.is-closed-loop-disabled": false, + "vserver.vserver-name": "example-vserver-name" + }, + "closedLoopAlarmStart": 1484855291527925, + "closedLoopEventStatus": "ONSET", + "closedLoopControlName": "CL-TEST", + "version": "1.0.2", + "target": "vserver.vserver-name", + "requestID": "8c1b8bd8-06f7-493f-8ed7-daaa4cc481bc", + "from": "DCAE" +} \ No newline at end of file diff --git a/packages/install/pom.xml b/packages/install/pom.xml index e603fb6f..7fa5d505 100644 --- a/packages/install/pom.xml +++ b/packages/install/pom.xml @@ -124,6 +124,12 @@ ${project.version} zip + + org.onap.policy.drools-pdp + feature-mdc-filters + ${project.version} + zip + diff --git a/policy-management/src/main/server/config/logback.xml b/policy-management/src/main/server/config/logback.xml index 4fb82768..bf08a5ac 100644 --- a/policy-management/src/main/server/config/logback.xml +++ b/policy-management/src/main/server/config/logback.xml @@ -19,126 +19,130 @@ --> - - - - - - - - - - - - - - - - - - - ${logDir}/${errorLog}.log - - ${logDir}/${errorLog}.%d{yyyy-MM-dd}.%i.log.zip - 50MB - 30 - 10GB - - - WARN - - - ${errorPattern} - - - - - - - - - ${logDir}/${debugLog}.log - - ${logDir}/${debugLog}.%d{yyyy-MM-dd}.%i.log.zip - 50MB - 30 - 10GB - - - ${debugPattern} - - - - - - - - - ${logDir}/${networkLog}.log - - ${logDir}/${networkLog}.%d{yyyy-MM-dd}.%i.log.zip - 50MB - 30 - 10GB - - - ${networkPattern} - - - - - - - - - ${logDir}/${metricLog}.log - - ${logDir}/${metricLog}.%d{yyyy-MM-dd}.%i.log.zip - 50MB - 30 - 10GB - - - - ${metricPattern} - - - - - - - - - ${logDir}/${transactionLog}.log - - ${logDir}/${transactionLog}.%d{yyyy-MM-dd}.%i.log.zip - 50MB - 30 - 10GB - - - - ${transactionPattern} - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + ${logDir}/${errorLog}.log + + ${logDir}/${errorLog}.%d{yyyy-MM-dd}.%i.log.zip + 50MB + 30 + 10GB + + + WARN + + + ${errorPattern} + + + + + + + + + ${logDir}/${debugLog}.log + + ${logDir}/${debugLog}.%d{yyyy-MM-dd}.%i.log.zip + 50MB + 30 + 10GB + + + ${debugPattern} + + + + + + + + + ${logDir}/${networkLog}.log + + ${logDir}/${networkLog}.%d{yyyy-MM-dd}.%i.log.zip + 50MB + 30 + 10GB + + + ${networkPattern} + + + + + + + + + ${logDir}/${metricLog}.log + + ${logDir}/${metricLog}.%d{yyyy-MM-dd}.%i.log.zip + 50MB + 30 + 10GB + + + + ${metricPattern} + + + + + + + + + ${logDir}/${transactionLog}.log + + ${logDir}/${transactionLog}.%d{yyyy-MM-dd}.%i.log.zip + 50MB + 30 + 10GB + + + + ${transactionPattern} + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index c2733c86..8a41bed5 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,7 @@ feature-simulators feature-distributed-locking feature-controller-logging + feature-mdc-filters packages -- cgit 1.2.3-korg