Implement PNFD Model driven conversion
[sdc.git] / openecomp-be / lib / openecomp-tosca-converter-lib / openecomp-tosca-converter-core / src / main / java / org / openecomp / core / converter / impl / pnfd / PnfdQueryExecutor.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.core.converter.impl.pnfd;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26 import org.apache.commons.collections.MapUtils;
27 import org.openecomp.core.converter.pnfd.exception.QueryOperationNotSupportedException;
28 import org.openecomp.core.converter.pnfd.model.ConversionQuery;
29
30 /**
31  * Runs YAML queries
32  */
33 public class PnfdQueryExecutor {
34
35     private PnfdQueryExecutor() {
36
37     }
38
39     /**
40      * Finds if a YAML object contains the provided YAML query.
41      * @param conversionQuery   The query
42      * @param yamlObject        The YAML object to be searched
43      * @return
44      * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise.
45      */
46     public static boolean find(final ConversionQuery conversionQuery, final Object yamlObject) {
47         return find(conversionQuery.getQuery(), yamlObject);
48     }
49
50     /**
51      * Recursive structure combined with {@link #findMap(Map, Map)} to find if a YAML object contains the provided YAML query.
52      * Compares the objects if it's a scalar value, otherwise go further in the YAML hierarchical structure
53      * calling the {@link #findMap(Map, Map)}.
54      * @param query        The current query
55      * @param yamlObject   The current YAML object to be searched
56      * @return
57      * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise.
58      */
59     private static boolean find(final Object query, final Object yamlObject) {
60         if (query == null) {
61             return true;
62         }
63
64         checkSupportedQueryType(query);
65
66         if (query instanceof String) {
67             return query.equals(yamlObject);
68         }
69         if (query instanceof Map) {
70             return findMap((Map) query, (Map) yamlObject);
71         }
72         return false;
73     }
74
75     /**
76      * Recursive structure combined with {@link #find(Object, Object)} to find if a YAML object contains the provided YAML query.     *
77      * @param query        The query current object
78      * @param yamlObject   The YAML object to be searched
79      * @return
80      * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise.
81      */
82     private static boolean findMap(final Map query, final Map yamlObject) {
83         if (MapUtils.isEmpty(query) || MapUtils.isEmpty(yamlObject)) {
84             return false;
85         }
86
87         if (!yamlObject.keySet().containsAll(query.keySet())) {
88             return false;
89         }
90
91         return query.entrySet().parallelStream().allMatch(queryEntryObj -> {
92             final Entry queryEntry = (Entry) queryEntryObj;
93             return find(queryEntry.getValue(), yamlObject.get(queryEntry.getKey()));
94         });
95     }
96
97     /**
98      * Checks the supported types for a query.
99      * @param query    the query to check
100      */
101     private static void checkSupportedQueryType(final Object query) {
102         if (query instanceof String || query instanceof Map) {
103             return;
104         }
105         if (query instanceof List || query instanceof Set) {
106             throw new QueryOperationNotSupportedException("Yaml list query is not supported yet");
107         }
108         throw new QueryOperationNotSupportedException(
109             String.format("Yaml query operation for '%s' is not supported yet", query.getClass())
110         );
111     }
112
113 }