2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * 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.
17 package org.openecomp.sdc.be.impl;
19 import org.javatuples.Pair;
20 import org.openecomp.sdc.be.components.impl.utils.DirectivesUtils;
21 import org.openecomp.sdc.be.datamodel.utils.ConstraintConvertor;
22 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
23 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition;
24 import org.openecomp.sdc.be.model.ComponentInstance;
25 import org.openecomp.sdc.be.model.InputDefinition;
26 import org.openecomp.sdc.be.model.Service;
27 import org.openecomp.sdc.be.ui.model.UIConstraint;
29 import java.util.List;
32 import java.util.function.Function;
33 import java.util.stream.Collectors;
35 public class ServiceFilterUtils {
38 private ServiceFilterUtils() {
42 public static boolean isNodeFilterAffectedByPropertyRemoval(Service service, String ciName, String propertyName) {
43 return service.getComponentInstances().stream().filter(ci -> ci.getNodeFilter() != null)
44 .anyMatch(ci -> propertyIsUsedInCI(ci, ciName, propertyName));
48 private static boolean propertyIsUsedInCI(ComponentInstance ci, String ciName, String propertyName) {
49 final List<String> directives = ci.getDirectives();
50 if (!directives.contains(DirectivesUtils.SELECTABLE)) {
53 if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
54 || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
57 return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
58 .flatMap(prop -> prop.getConstraints().stream()).map(String::new)
59 .filter(constraint -> new ConstraintConvertor().convert(constraint).getSourceType()
60 .equals(ConstraintConvertor.PROPERTY_CONSTRAINT))
61 .anyMatch(constraintStr -> {
62 UIConstraint uiConstraint = new ConstraintConvertor().convert(constraintStr);
63 return uiConstraint.getSourceName().equals(ciName) && uiConstraint.getValue().equals(propertyName);
68 public static Map<String, CINodeFilterDataDefinition> getRenamedNodesFilter(Service service, String oldName,
70 return service.getComponentInstances().stream().filter(ci -> isNodeFilterUsingChangedCi(ci, oldName))
71 .map(ci -> renameOldCiNames(ci, oldName, newName))
72 .collect(Collectors.toMap(Pair::getValue0, Pair::getValue1));
75 private static Pair<String, CINodeFilterDataDefinition> renameOldCiNames(ComponentInstance ci, String oldName,
77 ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
78 .filter(property -> isPropertyConstraintChangedByCi(property, oldName))
79 .forEach(property -> renamePropertyCiNames(property, oldName, newName));
81 return new Pair<>(ci.getUniqueId(), ci.getNodeFilter());
84 private static void renamePropertyCiNames(RequirementNodeFilterPropertyDataDefinition property, String oldName,
86 final List<String> constraints = property.getConstraints().stream().map(getConstraintString(oldName, newName))
87 .collect(Collectors.toList());
88 property.setConstraints(constraints);
91 private static Function<String, String> getConstraintString(String oldName, String newName) {
92 return constraint -> {
93 final ConstraintConvertor constraintConvertor = new ConstraintConvertor();
94 UIConstraint uiConstraint = constraintConvertor.convert(constraint);
95 if (uiConstraint.getSourceName().equals(oldName)) {
96 uiConstraint.setSourceName(newName);
98 return constraintConvertor.convert(uiConstraint);
103 public static Set<String> getNodesFiltersToBeDeleted(Service service, String ciName) {
104 return service.getComponentInstances().stream().filter(ci -> isNodeFilterUsingChangedCi(ci, ciName))
105 .map(ComponentInstance::getName).collect(Collectors.toSet());
110 public static Set<String> getNodesFiltersToBeDeleted(Service service, ComponentInstance inCi) {
111 return getNodesFiltersToBeDeleted(service, inCi.getName());
116 private static boolean isNodeFilterUsingChangedCi(ComponentInstance ci, String name) {
117 final List<String> directives = ci.getDirectives();
118 if (!directives.contains(DirectivesUtils.SELECTABLE)) {
121 if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
122 || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
125 return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
126 .anyMatch(property -> isPropertyConstraintChangedByCi(property, name));
129 private static boolean isPropertyConstraintChangedByCi(
130 RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition, String name) {
131 List<String> constraints = requirementNodeFilterPropertyDataDefinition.getConstraints();
132 if (constraints == null) {
135 return constraints.stream().anyMatch(constraint -> isConstraintChangedByCi(constraint, name));
138 private static boolean isConstraintChangedByCi(String constraint, String name) {
139 UIConstraint uiConstraint = new ConstraintConvertor().convert(constraint);
140 if (uiConstraint == null || uiConstraint.getSourceType() == null) {
143 if (!uiConstraint.getSourceType().equals(ConstraintConvertor.PROPERTY_CONSTRAINT)) {
146 return uiConstraint.getSourceName().equals(name);
149 public static Set<String> getNodesFiltersToBeDeleted(Service service, InputDefinition changedInput) {
150 return service.getComponentInstances().stream().filter(ci -> isNodeFilterUsingChangedInput(ci, changedInput))
151 .map(ComponentInstance::getName).collect(Collectors.toSet());
154 private static boolean isNodeFilterUsingChangedInput(ComponentInstance ci, InputDefinition changedInput) {
155 final List<String> directives = ci.getDirectives();
156 if (!directives.contains(DirectivesUtils.SELECTABLE)) {
159 return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
160 .anyMatch(property -> isPropertyConstraintChangedByInput(property, changedInput));
163 private static boolean isPropertyConstraintChangedByInput(
164 RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition,
165 InputDefinition changedInput) {
166 List<String> constraints = requirementNodeFilterPropertyDataDefinition.getConstraints();
167 return constraints.stream().anyMatch(constraint -> isConstraintChangedByInput(constraint, changedInput));
170 private static boolean isConstraintChangedByInput(String constraint, InputDefinition changedInput) {
171 UIConstraint uiConstraint = new ConstraintConvertor().convert(constraint);
172 if (!uiConstraint.getSourceType().equals(ConstraintConvertor.SERVICE_INPUT_CONSTRAINT)) {
175 return uiConstraint.getValue().equals(changedInput.getName());