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.
16 package org.openecomp.sdc.be.impl;
18 import java.util.List;
21 import java.util.function.Function;
22 import java.util.stream.Collectors;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.javatuples.Pair;
25 import org.openecomp.sdc.be.datamodel.utils.ConstraintConvertor;
26 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.InputDefinition;
30 import org.openecomp.sdc.be.model.Service;
31 import org.openecomp.sdc.be.ui.model.UIConstraint;
33 public class ServiceFilterUtils {
35 private ServiceFilterUtils() {
38 public static boolean isNodeFilterAffectedByPropertyRemoval(Service service, String ciName, String propertyName) {
39 return service.getComponentInstances().stream().filter(ci -> ci.getNodeFilter() != null)
40 .anyMatch(ci -> propertyIsUsedInCI(ci, ciName, propertyName));
43 private static boolean propertyIsUsedInCI(ComponentInstance ci, String ciName, String propertyName) {
44 if (CollectionUtils.isEmpty(ci.getDirectives())) {
47 if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
48 || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
51 return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream().flatMap(prop -> prop.getConstraints().stream())
53 .filter(constraint -> new ConstraintConvertor().convert(constraint).getSourceType().equals(ConstraintConvertor.PROPERTY_CONSTRAINT))
54 .anyMatch(constraintStr -> {
55 UIConstraint uiConstraint = new ConstraintConvertor().convert(constraintStr);
56 return uiConstraint.getSourceName().equals(ciName) && uiConstraint.getValue().equals(propertyName);
60 public static Map<String, CINodeFilterDataDefinition> getRenamedNodesFilter(Service service, String oldName, String newName) {
61 return service.getComponentInstances().stream().filter(ci -> isNodeFilterUsingChangedCi(ci, oldName))
62 .map(ci -> renameOldCiNames(ci, oldName, newName)).collect(Collectors.toMap(Pair::getValue0, Pair::getValue1));
65 private static Pair<String, CINodeFilterDataDefinition> renameOldCiNames(ComponentInstance ci, String oldName, String newName) {
66 ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
67 .filter(property -> isPropertyConstraintChangedByCi(property, oldName))
68 .forEach(property -> renamePropertyCiNames(property, oldName, newName));
69 return new Pair<>(ci.getUniqueId(), ci.getNodeFilter());
72 private static void renamePropertyCiNames(RequirementNodeFilterPropertyDataDefinition property, String oldName, String newName) {
73 final List<String> constraints = property.getConstraints().stream().map(getConstraintString(oldName, newName)).collect(Collectors.toList());
74 property.setConstraints(constraints);
77 private static Function<String, String> getConstraintString(String oldName, String newName) {
78 return constraint -> {
79 final ConstraintConvertor constraintConvertor = new ConstraintConvertor();
80 UIConstraint uiConstraint = constraintConvertor.convert(constraint);
81 if (uiConstraint.getSourceName().equals(oldName)) {
82 uiConstraint.setSourceName(newName);
84 return constraintConvertor.convert(uiConstraint);
88 public static Set<String> getNodesFiltersToBeDeleted(Service service, String ciName) {
89 return service.getComponentInstances().stream().filter(ci -> isNodeFilterUsingChangedCi(ci, ciName)).map(ComponentInstance::getName)
90 .collect(Collectors.toSet());
93 public static Set<String> getNodesFiltersToBeDeleted(Service service, ComponentInstance inCi) {
94 return getNodesFiltersToBeDeleted(service, inCi.getName());
97 private static boolean isNodeFilterUsingChangedCi(ComponentInstance ci, String name) {
98 if (CollectionUtils.isEmpty(ci.getDirectives())) {
101 if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
102 || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
105 return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
106 .anyMatch(property -> isPropertyConstraintChangedByCi(property, name));
109 private static boolean isPropertyConstraintChangedByCi(RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition,
111 List<String> constraints = requirementNodeFilterPropertyDataDefinition.getConstraints();
112 if (constraints == null) {
115 return constraints.stream().anyMatch(constraint -> isConstraintChangedByCi(constraint, name));
118 private static boolean isConstraintChangedByCi(String constraint, String name) {
119 UIConstraint uiConstraint = new ConstraintConvertor().convert(constraint);
120 if (uiConstraint == null || uiConstraint.getSourceType() == null) {
123 if (!uiConstraint.getSourceType().equals(ConstraintConvertor.PROPERTY_CONSTRAINT)) {
126 return uiConstraint.getSourceName().equals(name);
129 public static Set<String> getNodesFiltersToBeDeleted(Service service, InputDefinition changedInput) {
130 return service.getComponentInstances().stream().filter(ci -> isNodeFilterUsingChangedInput(ci, changedInput)).map(ComponentInstance::getName)
131 .collect(Collectors.toSet());
134 private static boolean isNodeFilterUsingChangedInput(ComponentInstance ci, InputDefinition changedInput) {
135 if (CollectionUtils.isEmpty(ci.getDirectives())) {
138 return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
139 .anyMatch(property -> isPropertyConstraintChangedByInput(property, changedInput));
142 private static boolean isPropertyConstraintChangedByInput(RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition,
143 InputDefinition changedInput) {
144 List<String> constraints = requirementNodeFilterPropertyDataDefinition.getConstraints();
145 return constraints.stream().anyMatch(constraint -> isConstraintChangedByInput(constraint, changedInput));
148 private static boolean isConstraintChangedByInput(String constraint, InputDefinition changedInput) {
149 UIConstraint uiConstraint = new ConstraintConvertor().convert(constraint);
150 if (!uiConstraint.getSourceType().equals(ConstraintConvertor.SERVICE_INPUT_CONSTRAINT)) {
153 return uiConstraint.getValue().equals(changedInput.getName());