TCA: Replace any openecomp reference by onap
[dcaegen2/analytics/tca.git] / dcae-analytics-common / src / main / java / org / onap / dcae / apod / analytics / common / service / filter / GenericJsonMessageFilter.java
-/*\r
- * ===============================LICENSE_START======================================\r
- *  dcae-analytics\r
- * ================================================================================\r
- *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- *  Licensed under the Apache License, Version 2.0 (the "License");\r
- *  you may not use this file except in compliance with the License.\r
- *   You may obtain a copy of the License at\r
- *\r
- *          http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- *  Unless required by applicable law or agreed to in writing, software\r
- *  distributed under the License is distributed on an "AS IS" BASIS,\r
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- *  See the License for the specific language governing permissions and\r
- *  limitations under the License.\r
- *  ============================LICENSE_END===========================================\r
- */\r
-\r
-package org.openecomp.dcae.apod.analytics.common.service.filter;\r
-\r
-import com.google.common.collect.ImmutableSet;\r
-import com.jayway.jsonpath.DocumentContext;\r
-import com.jayway.jsonpath.JsonPath;\r
-import com.jayway.jsonpath.PathNotFoundException;\r
-import org.apache.commons.lang3.StringUtils;\r
-import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import java.util.List;\r
-import java.util.Set;\r
-\r
-/**\r
- * A Generic Json Message Filter which filter the json message based on given json Path and list of expected values\r
- * for that json path. The {@link JsonMessageFilterProcessorContext#isMatched} flag will be changed as per table below:\r
- * <pre>\r
- *      Incoming message is blank or invalid Json                               =  null\r
- *      Incoming message path is matches expected values                        = true\r
- *      Incoming message does not match expected values or path does not exist  = false\r
- * </pre>\r
- * <p>\r
- * @author Rajiv Singla . Creation Date: 2/10/2017.\r
- */\r
-public class GenericJsonMessageFilter extends AbstractMessageProcessor<JsonMessageFilterProcessorContext> {\r
-\r
-    private static final Logger LOG = LoggerFactory.getLogger(GenericJsonMessageFilter.class);\r
-    private static final long serialVersionUID = 1L;\r
-\r
-    private final String filterName;\r
-    private final String jsonPath;\r
-    private final Set<String> expectedValues;\r
-\r
-    public GenericJsonMessageFilter(final String filterName, final String jsonPath, final Set<String> expectedValues) {\r
-        this.filterName = filterName;\r
-        this.jsonPath = jsonPath;\r
-        this.expectedValues = expectedValues;\r
-    }\r
-\r
-    public GenericJsonMessageFilter(final String filterName, final String jsonPath, final String expectedValue) {\r
-        this(filterName, jsonPath, ImmutableSet.of(expectedValue));\r
-    }\r
-\r
-    @Override\r
-    public String getProcessorDescription() {\r
-        return filterName;\r
-    }\r
-\r
-    @Override\r
-    public JsonMessageFilterProcessorContext processMessage(final JsonMessageFilterProcessorContext processorContext) {\r
-\r
-        final String jsonMessage = processorContext.getMessage().trim();\r
-\r
-        if (StringUtils.isNotBlank(jsonMessage) && jsonMessage.startsWith("{") && jsonMessage.endsWith("}")) {\r
-\r
-            // locate json path value\r
-            final DocumentContext documentContext = JsonPath.parse(jsonMessage);\r
-            String jsonPathValue = null;\r
-            try {\r
-                final List jsonPathValues = documentContext.read(jsonPath);\r
-                final Object pathValue = jsonPathValues.isEmpty() ? null :  jsonPathValues.get(0);\r
-                jsonPathValue = pathValue instanceof Number ? pathValue.toString() : (String) pathValue;\r
-            } catch (PathNotFoundException ex) {\r
-                LOG.info("Unable to find json Path: {}. Exception: {}, Json Message: {}", jsonPath, ex, jsonMessage);\r
-            }\r
-\r
-            LOG.debug("Value for jsonPath: {}, jsonPathValue: {}, expected Values: {}",\r
-                    jsonPath, jsonPathValue, expectedValues);\r
-\r
-            // if json path value is null or we json value is not present in expect values then terminate early\r
-            if (jsonPathValue == null || !expectedValues.contains(jsonPathValue)) {\r
-                final String terminatingMessage = String.format("Filter match unsuccessful. " +\r
-                                "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s",\r
-                        jsonPath, jsonPathValue, expectedValues);\r
-                processorContext.setMatched(false);\r
-                setTerminatingProcessingMessage(terminatingMessage, processorContext);\r
-            } else {\r
-                final String finishProcessingMessage = String.format("Filter match successful. " +\r
-                                "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s",\r
-                        jsonPath, jsonPathValue, expectedValues);\r
-                processorContext.setMatched(true);\r
-                setFinishedProcessingMessage(finishProcessingMessage, processorContext);\r
-            }\r
-        } else {\r
-            // if incoming message is blank of valid Json then matched flag will be null\r
-            final String terminatingMessage = "Incoming json message is blank or not json. " +\r
-                    "Json filter cannot be applied";\r
-            processorContext.setMatched(null);\r
-            setTerminatingProcessingMessage(terminatingMessage, processorContext);\r
-        }\r
-\r
-        return processorContext;\r
-    }\r
-}\r
+/*
+ * ===============================LICENSE_START======================================
+ *  dcae-analytics
+ * ================================================================================
+ *    Copyright © 2017 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.dcae.apod.analytics.common.service.filter;
+
+import com.google.common.collect.ImmutableSet;
+import com.jayway.jsonpath.DocumentContext;
+import com.jayway.jsonpath.JsonPath;
+import com.jayway.jsonpath.PathNotFoundException;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * A Generic Json Message Filter which filter the json message based on given json Path and list of expected values
+ * for that json path. The {@link JsonMessageFilterProcessorContext#isMatched} flag will be changed as per table below:
+ * <pre>
+ *      Incoming message is blank or invalid Json                               =  null
+ *      Incoming message path is matches expected values                        = true
+ *      Incoming message does not match expected values or path does not exist  = false
+ * </pre>
+ * <p>
+ * @author Rajiv Singla . Creation Date: 2/10/2017.
+ */
+public class GenericJsonMessageFilter extends AbstractMessageProcessor<JsonMessageFilterProcessorContext> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(GenericJsonMessageFilter.class);
+    private static final long serialVersionUID = 1L;
+
+    private final String filterName;
+    private final String jsonPath;
+    private final Set<String> expectedValues;
+
+    public GenericJsonMessageFilter(final String filterName, final String jsonPath, final Set<String> expectedValues) {
+        this.filterName = filterName;
+        this.jsonPath = jsonPath;
+        this.expectedValues = expectedValues;
+    }
+
+    public GenericJsonMessageFilter(final String filterName, final String jsonPath, final String expectedValue) {
+        this(filterName, jsonPath, ImmutableSet.of(expectedValue));
+    }
+
+    @Override
+    public String getProcessorDescription() {
+        return filterName;
+    }
+
+    @Override
+    public JsonMessageFilterProcessorContext processMessage(final JsonMessageFilterProcessorContext processorContext) {
+
+        final String jsonMessage = processorContext.getMessage().trim();
+
+        if (StringUtils.isNotBlank(jsonMessage) && jsonMessage.startsWith("{") && jsonMessage.endsWith("}")) {
+
+            // locate json path value
+            final DocumentContext documentContext = JsonPath.parse(jsonMessage);
+            String jsonPathValue = null;
+            try {
+                final List jsonPathValues = documentContext.read(jsonPath);
+                final Object pathValue = jsonPathValues.isEmpty() ? null :  jsonPathValues.get(0);
+                jsonPathValue = pathValue instanceof Number ? pathValue.toString() : (String) pathValue;
+            } catch (PathNotFoundException ex) {
+                LOG.info("Unable to find json Path: {}. Exception: {}, Json Message: {}", jsonPath, ex, jsonMessage);
+            }
+
+            LOG.debug("Value for jsonPath: {}, jsonPathValue: {}, expected Values: {}",
+                    jsonPath, jsonPathValue, expectedValues);
+
+            // if json path value is null or we json value is not present in expect values then terminate early
+            if (jsonPathValue == null || !expectedValues.contains(jsonPathValue)) {
+                final String terminatingMessage = String.format("Filter match unsuccessful. " +
+                                "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s",
+                        jsonPath, jsonPathValue, expectedValues);
+                processorContext.setMatched(false);
+                setTerminatingProcessingMessage(terminatingMessage, processorContext);
+            } else {
+                final String finishProcessingMessage = String.format("Filter match successful. " +
+                                "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s",
+                        jsonPath, jsonPathValue, expectedValues);
+                processorContext.setMatched(true);
+                setFinishedProcessingMessage(finishProcessingMessage, processorContext);
+            }
+        } else {
+            // if incoming message is blank of valid Json then matched flag will be null
+            final String terminatingMessage = "Incoming json message is blank or not json. " +
+                    "Json filter cannot be applied";
+            processorContext.setMatched(null);
+            setTerminatingProcessingMessage(terminatingMessage, processorContext);
+        }
+
+        return processorContext;
+    }
+}