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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.core.converter.impl.pnfd;
22 import java.util.List;
24 import java.util.Map.Entry;
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;
33 public class PnfdQueryExecutor {
35 private PnfdQueryExecutor() {
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
44 * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise.
46 public static boolean find(final ConversionQuery conversionQuery, final Object yamlObject) {
47 return find(conversionQuery.getQuery(), yamlObject);
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
57 * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise.
59 private static boolean find(final Object query, final Object yamlObject) {
64 checkSupportedQueryType(query);
66 if (query instanceof String) {
67 return query.equals(yamlObject);
69 if (query instanceof Map) {
70 return findMap((Map) query, (Map) yamlObject);
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
80 * {@code true} if the YAML query structure was found in the YAML object, {@code false} otherwise.
82 private static boolean findMap(final Map query, final Map yamlObject) {
83 if (MapUtils.isEmpty(query) || MapUtils.isEmpty(yamlObject)) {
87 if (!yamlObject.keySet().containsAll(query.keySet())) {
91 return query.entrySet().parallelStream().allMatch(queryEntryObj -> {
92 final Entry queryEntry = (Entry) queryEntryObj;
93 return find(queryEntry.getValue(), yamlObject.get(queryEntry.getKey()));
98 * Checks the supported types for a query.
99 * @param query the query to check
101 private static void checkSupportedQueryType(final Object query) {
102 if (query instanceof String || query instanceof Map) {
105 if (query instanceof List || query instanceof Set) {
106 throw new QueryOperationNotSupportedException("Yaml list query is not supported yet");
108 throw new QueryOperationNotSupportedException(
109 String.format("Yaml query operation for '%s' is not supported yet", query.getClass())