TCA: Support for VES/A&AI enrichment
[dcaegen2/analytics/tca.git] / dcae-analytics-common / src / main / java / org / openecomp / dcae / apod / analytics / common / service / filter / GenericJsonMessageFilter.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.common.service.filter;\r
22 \r
23 import com.google.common.collect.ImmutableSet;\r
24 import com.jayway.jsonpath.DocumentContext;\r
25 import com.jayway.jsonpath.JsonPath;\r
26 import com.jayway.jsonpath.PathNotFoundException;\r
27 import org.apache.commons.lang3.StringUtils;\r
28 import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor;\r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 \r
32 import java.util.Set;\r
33 \r
34 /**\r
35  * A Generic Json Message Filter which filter the json message based on given json Path and list of expected values\r
36  * for that json path. The {@link JsonMessageFilterProcessorContext#isMatched} flag will be changed as per table below:\r
37  * <pre>\r
38  *      Incoming message is blank or invalid Json                               =  null\r
39  *      Incoming message path is matches expected values                        = true\r
40  *      Incoming message does not match expected values or path does not exist  = false\r
41  * </pre>\r
42  * <p>\r
43  * @author Rajiv Singla . Creation Date: 2/10/2017.\r
44  */\r
45 public class GenericJsonMessageFilter extends AbstractMessageProcessor<JsonMessageFilterProcessorContext> {\r
46 \r
47     private static final Logger LOG = LoggerFactory.getLogger(GenericJsonMessageFilter.class);\r
48     private static final long serialVersionUID = 1L;\r
49 \r
50     private final String filterName;\r
51     private final String jsonPath;\r
52     private final Set<String> expectedValues;\r
53 \r
54     public GenericJsonMessageFilter(final String filterName, final String jsonPath, final Set<String> expectedValues) {\r
55         this.filterName = filterName;\r
56         this.jsonPath = jsonPath;\r
57         this.expectedValues = expectedValues;\r
58     }\r
59 \r
60     public GenericJsonMessageFilter(final String filterName, final String jsonPath, final String expectedValue) {\r
61         this(filterName, jsonPath, ImmutableSet.of(expectedValue));\r
62     }\r
63 \r
64     @Override\r
65     public String getProcessorDescription() {\r
66         return filterName;\r
67     }\r
68 \r
69     @Override\r
70     public JsonMessageFilterProcessorContext processMessage(final JsonMessageFilterProcessorContext processorContext) {\r
71 \r
72         final String jsonMessage = processorContext.getMessage().trim();\r
73 \r
74         if (StringUtils.isNotBlank(jsonMessage) && jsonMessage.startsWith("{") && jsonMessage.endsWith("}")) {\r
75 \r
76             // locate json path value\r
77             final DocumentContext documentContext = JsonPath.parse(jsonMessage);\r
78             String jsonPathValue = null;\r
79             try {\r
80                 jsonPathValue = documentContext.read(jsonPath, String.class);\r
81             } catch (PathNotFoundException ex) {\r
82                 LOG.info("Unable to find json Path: {}. Exception: {}, Json Message: {}", jsonPath, ex, jsonMessage);\r
83             }\r
84 \r
85             LOG.debug("Value for jsonPath: {}, jsonPathValue: {}, expected Values: {}",\r
86                     jsonPath, jsonPathValue, expectedValues);\r
87 \r
88             // if json path value is null or we json value is not present in expect values then terminate early\r
89             if (jsonPathValue == null || !expectedValues.contains(jsonPathValue)) {\r
90                 final String terminatingMessage = String.format("Filter match unsuccessful. " +\r
91                                 "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s",\r
92                         jsonPath, jsonPathValue, expectedValues);\r
93                 processorContext.setMatched(false);\r
94                 setTerminatingProcessingMessage(terminatingMessage, processorContext);\r
95             } else {\r
96                 final String finishProcessingMessage = String.format("Filter match successful. " +\r
97                                 "JsonPath: %s, Actual JsonPathValue: %s, Excepted Json Path Values: %s",\r
98                         jsonPath, jsonPathValue, expectedValues);\r
99                 processorContext.setMatched(true);\r
100                 setFinishedProcessingMessage(finishProcessingMessage, processorContext);\r
101             }\r
102         } else {\r
103             // if incoming message is blank of valid Json then matched flag will be null\r
104             final String terminatingMessage = "Incoming json message is blank or not json. " +\r
105                     "Json filter cannot be applied";\r
106             processorContext.setMatched(null);\r
107             setTerminatingProcessingMessage(terminatingMessage, processorContext);\r
108         }\r
109 \r
110         return processorContext;\r
111     }\r
112 }\r