2 * ===============================LICENSE_START======================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.dcae.apod.analytics.common.service.filter;
\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
32 import java.util.Set;
\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
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
43 * @author Rajiv Singla . Creation Date: 2/10/2017.
\r
45 public class GenericJsonMessageFilter extends AbstractMessageProcessor<JsonMessageFilterProcessorContext> {
\r
47 private static final Logger LOG = LoggerFactory.getLogger(GenericJsonMessageFilter.class);
\r
48 private static final long serialVersionUID = 1L;
\r
50 private final String filterName;
\r
51 private final String jsonPath;
\r
52 private final Set<String> expectedValues;
\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
60 public GenericJsonMessageFilter(final String filterName, final String jsonPath, final String expectedValue) {
\r
61 this(filterName, jsonPath, ImmutableSet.of(expectedValue));
\r
65 public String getProcessorDescription() {
\r
70 public JsonMessageFilterProcessorContext processMessage(final JsonMessageFilterProcessorContext processorContext) {
\r
72 final String jsonMessage = processorContext.getMessage().trim();
\r
74 if (StringUtils.isNotBlank(jsonMessage) && jsonMessage.startsWith("{") && jsonMessage.endsWith("}")) {
\r
76 // locate json path value
\r
77 final DocumentContext documentContext = JsonPath.parse(jsonMessage);
\r
78 String jsonPathValue = null;
\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
85 LOG.debug("Value for jsonPath: {}, jsonPathValue: {}, expected Values: {}",
\r
86 jsonPath, jsonPathValue, expectedValues);
\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
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
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
110 return processorContext;
\r