79d64b7dd40cd3b18470ed55f1a084564e579072
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.template.search.handler;
22
23 import com.google.common.collect.Lists;
24 import com.google.gson.JsonPrimitive;
25 import org.springframework.data.mongodb.core.query.Criteria;
26
27 import java.util.List;
28 import java.util.regex.Pattern;
29
30 /**
31  * This class is a helper class for constructing apropriate criteria for query send to mongodb based on type of value.
32  * Query is build to search mongodb for templates that contains key-value pairs that satisfy given criteria.
33  * Value is oftype JsonPrimitive, based on its primitive java type following criteria are build to get proper document:
34  * -for string - there is a regex expression that ignores every meta character inside passed argument and searches for exact literal match ignoring case;
35  * -for number - all numbers are treated as double (mongodb number type equivalent)
36  * -for boolean - exact match, used string representation of boolean in search
37  **/
38
39 public class PrimitiveValueCriteriaBuilder {
40
41     private final List<ValueTypeHandler> typeHandlers;
42
43     public PrimitiveValueCriteriaBuilder() {
44         typeHandlers = Lists.newArrayList(new StringValueHandler(), new NumberValueHandler(), new BoolValueHandler());
45     }
46
47     public Criteria applyValueCriteriaBasedOnPrimitiveType(Criteria baseCriteria, JsonPrimitive jsonPrimitive) {
48         ValueTypeHandler typeHandler = typeHandlers.stream()
49                 .filter(el -> el.isProperTypeHandler(jsonPrimitive))
50                 .findFirst()
51                 .orElseThrow(() ->
52                         new IllegalArgumentException(String.format(
53                                 "Expected json primitive, but given value: %s is of type: %s and could not be decoded",
54                                 jsonPrimitive, jsonPrimitive.getClass().toString())));
55         return typeHandler.chainCriteriaForValue(baseCriteria, jsonPrimitive);
56     }
57
58     private interface ValueTypeHandler {
59         boolean isProperTypeHandler(JsonPrimitive value);
60
61         Criteria chainCriteriaForValue(Criteria criteria, JsonPrimitive value);
62     }
63
64     private class BoolValueHandler implements ValueTypeHandler {
65         public boolean isProperTypeHandler(JsonPrimitive value) {
66             return value.isBoolean();
67         }
68
69         public Criteria chainCriteriaForValue(Criteria criteria, JsonPrimitive value) {
70             return criteria.is(value.getAsString());
71         }
72
73     }
74
75     private class NumberValueHandler implements ValueTypeHandler {
76         public boolean isProperTypeHandler(JsonPrimitive value) {
77             return value.isNumber();
78         }
79
80         public Criteria chainCriteriaForValue(Criteria baseCriteria, JsonPrimitive value) {
81             return baseCriteria.is(value.getAsDouble());
82         }
83     }
84
85     private class StringValueHandler implements ValueTypeHandler {
86         public boolean isProperTypeHandler(JsonPrimitive value) {
87             return value.isString();
88         }
89
90         public Criteria chainCriteriaForValue(Criteria baseCriteria, JsonPrimitive value) {
91             return baseCriteria.regex(makeRegexCaseInsensitive(value.getAsString()));
92         }
93
94         private Pattern makeRegexCaseInsensitive(String base) {
95             String metaCharEscaped = convertToIgnoreMetaChars(base);
96             return Pattern.compile("^" + metaCharEscaped + "$", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
97         }
98
99         private String convertToIgnoreMetaChars(String valueWithMetaChars) {
100             return Pattern.quote(valueWithMetaChars);
101         }
102     }
103 }