Catalog alignment
[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
17 package org.openecomp.sdc.be.impl;
18
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;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.function.Function;
33 import java.util.stream.Collectors;
34
35 public class ServiceFilterUtils {
36
37
38     private ServiceFilterUtils() {
39     }
40
41
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));
45     }
46
47
48     private static boolean propertyIsUsedInCI(ComponentInstance ci, String ciName, String propertyName) {
49         final List<String> directives = ci.getDirectives();
50         if (!directives.contains(DirectivesUtils.SELECTABLE)) {
51             return false;
52         }
53         if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
54                     || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
55             return false;
56         }
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);
64                  });
65
66     }
67
68     public static Map<String, CINodeFilterDataDefinition> getRenamedNodesFilter(Service service, String oldName,
69             String newName) {
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));
73     }
74
75     private static Pair<String, CINodeFilterDataDefinition> renameOldCiNames(ComponentInstance ci, String oldName,
76             String newName) {
77         ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
78           .filter(property -> isPropertyConstraintChangedByCi(property, oldName))
79           .forEach(property -> renamePropertyCiNames(property, oldName, newName));
80
81         return new Pair<>(ci.getUniqueId(), ci.getNodeFilter());
82     }
83
84     private static void renamePropertyCiNames(RequirementNodeFilterPropertyDataDefinition property, String oldName,
85             String newName) {
86         final List<String> constraints = property.getConstraints().stream().map(getConstraintString(oldName, newName))
87                                                  .collect(Collectors.toList());
88         property.setConstraints(constraints);
89     }
90
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);
97             }
98             return constraintConvertor.convert(uiConstraint);
99         };
100     }
101
102
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());
106     }
107
108
109
110     public static Set<String> getNodesFiltersToBeDeleted(Service service, ComponentInstance inCi) {
111         return getNodesFiltersToBeDeleted(service, inCi.getName());
112     }
113
114
115
116     private static boolean isNodeFilterUsingChangedCi(ComponentInstance ci, String name) {
117         final List<String> directives = ci.getDirectives();
118         if (!directives.contains(DirectivesUtils.SELECTABLE)) {
119             return false;
120         }
121         if (ci.getNodeFilter() == null || ci.getNodeFilter().getProperties() == null
122                     || ci.getNodeFilter().getProperties().getListToscaDataDefinition() == null) {
123             return false;
124         }
125         return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
126                  .anyMatch(property -> isPropertyConstraintChangedByCi(property, name));
127     }
128
129     private static boolean isPropertyConstraintChangedByCi(
130             RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition, String name) {
131         List<String> constraints = requirementNodeFilterPropertyDataDefinition.getConstraints();
132         if (constraints == null) {
133             return false;
134         }
135         return constraints.stream().anyMatch(constraint -> isConstraintChangedByCi(constraint, name));
136     }
137
138     private static boolean isConstraintChangedByCi(String constraint, String name) {
139         UIConstraint uiConstraint = new ConstraintConvertor().convert(constraint);
140         if (uiConstraint == null || uiConstraint.getSourceType() == null) {
141             return false;
142         }
143         if (!uiConstraint.getSourceType().equals(ConstraintConvertor.PROPERTY_CONSTRAINT)) {
144             return false;
145         }
146         return uiConstraint.getSourceName().equals(name);
147     }
148
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());
152     }
153
154     private static boolean isNodeFilterUsingChangedInput(ComponentInstance ci, InputDefinition changedInput) {
155         final List<String> directives = ci.getDirectives();
156         if (!directives.contains(DirectivesUtils.SELECTABLE)) {
157             return false;
158         }
159         return ci.getNodeFilter().getProperties().getListToscaDataDefinition().stream()
160                  .anyMatch(property -> isPropertyConstraintChangedByInput(property, changedInput));
161     }
162
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));
168     }
169
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)) {
173             return false;
174         }
175         return uiConstraint.getValue().equals(changedInput.getName());
176     }
177
178 }