866affc509e6e97e8645debeb4c91d36519d1ddc
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / impl / ServiceFilterUtils.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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 package org.openecomp.sdc.be.impl;
17
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Set;
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;
32
33 public class ServiceFilterUtils {
34
35     private ServiceFilterUtils() {
36     }
37
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));
41     }
42
43     private static boolean propertyIsUsedInCI(ComponentInstance ci, String ciName, String propertyName) {
44         if (CollectionUtils.isEmpty(ci.getDirectives())) {
45             return false;
46         }
47         if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
48             || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
49             return false;
50         }
51         return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream().flatMap(prop -> prop.getConstraints().stream())
52             .map(String::new)
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);
57             });
58     }
59
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));
63     }
64
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());
70     }
71
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);
75     }
76
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);
83             }
84             return constraintConvertor.convert(uiConstraint);
85         };
86     }
87
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());
91     }
92
93     public static Set<String> getNodesFiltersToBeDeleted(Service service, ComponentInstance inCi) {
94         return getNodesFiltersToBeDeleted(service, inCi.getName());
95     }
96
97     private static boolean isNodeFilterUsingChangedCi(ComponentInstance ci, String name) {
98         if (CollectionUtils.isEmpty(ci.getDirectives())) {
99             return false;
100         }
101         if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
102             || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
103             return false;
104         }
105         return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
106             .anyMatch(property -> isPropertyConstraintChangedByCi(property, name));
107     }
108
109     private static boolean isPropertyConstraintChangedByCi(RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition,
110                                                            String name) {
111         List<String> constraints = requirementNodeFilterPropertyDataDefinition.getConstraints();
112         if (constraints == null) {
113             return false;
114         }
115         return constraints.stream().anyMatch(constraint -> isConstraintChangedByCi(constraint, name));
116     }
117
118     private static boolean isConstraintChangedByCi(String constraint, String name) {
119         UIConstraint uiConstraint = new ConstraintConvertor().convert(constraint);
120         if (uiConstraint == null || uiConstraint.getSourceType() == null) {
121             return false;
122         }
123         if (!uiConstraint.getSourceType().equals(ConstraintConvertor.PROPERTY_CONSTRAINT)) {
124             return false;
125         }
126         return uiConstraint.getSourceName().equals(name);
127     }
128
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());
132     }
133
134     private static boolean isNodeFilterUsingChangedInput(ComponentInstance ci, InputDefinition changedInput) {
135         if (CollectionUtils.isEmpty(ci.getDirectives())) {
136             return false;
137         }
138         return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
139             .anyMatch(property -> isPropertyConstraintChangedByInput(property, changedInput));
140     }
141
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));
146     }
147
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)) {
151             return false;
152         }
153         return uiConstraint.getValue().equals(changedInput.getName());
154     }
155 }