22ba03ff27694201ac8e542aafcad4af287a28a8
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / utils / PropertiesUtils.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.components.utils;
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.collections.MapUtils;
21 import org.apache.commons.collections4.ListUtils;
22 import org.apache.commons.collections4.SetUtils;
23 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
24 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
25 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
26 import org.openecomp.sdc.be.model.CapabilityDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
29 import org.openecomp.sdc.be.model.InputDefinition;
30 import org.openecomp.sdc.be.model.PropertyDefinition;
31 import org.openecomp.sdc.be.model.Resource;
32
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Objects;
39 import java.util.Optional;
40 import java.util.Set;
41 import java.util.function.Function;
42 import java.util.function.Predicate;
43 import java.util.stream.Collectors;
44
45 import static org.openecomp.sdc.be.components.property.GetInputUtils.isGetInputValueForInput;
46
47 public class PropertiesUtils {
48
49     private PropertiesUtils() {
50         //Hiding implicit default constructor
51     }
52
53     public static List<PropertyDefinition> getProperties(Component service) {
54         List<PropertyDefinition> properties = service.getProperties();
55         if (properties == null) {
56             properties = new ArrayList<>();
57         }
58         Set<PropertyDefinition> serviceProperties = new HashSet<>(properties);
59         SetUtils.emptyIfNull(serviceProperties)
60                 .forEach(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition,
61                         service.getInputs()));
62         Set<PropertyDefinition> inputs = ListUtils.emptyIfNull(service.getInputs()).stream()
63                         .map(PropertyDefinition::new)
64                         .collect(Collectors.toSet());
65         serviceProperties.addAll(inputs);
66         serviceProperties = serviceProperties.stream()
67                 .filter(distinctByKey(PropertyDefinition::getName))
68                 .collect(Collectors.toSet());
69         return new ArrayList<>(serviceProperties);
70     }
71
72     public static PropertyDefinition resolvePropertyValueFromInput(PropertyDefinition propertyDefinition,
73                                                                    List<InputDefinition> componentInputs) {
74         if (Objects.isNull(propertyDefinition) || CollectionUtils.isEmpty(componentInputs)) {
75             return propertyDefinition;
76         }
77         Optional<InputDefinition> mappedInput = componentInputs.stream()
78                 .filter(componentInput -> Objects.nonNull(componentInput.getPropertyId())
79                         && componentInput.getPropertyId().equals(propertyDefinition.getUniqueId()))
80                 .findFirst();
81         mappedInput.ifPresent(inputDefinition -> propertyDefinition.setValue(inputDefinition.getValue()));
82         return propertyDefinition;
83     }
84
85
86     public static Optional<ComponentInstanceProperty> isCapabilityProperty(String propertyUniqueId,
87                                                Component containerComponent) {
88
89         Optional<List<ComponentInstanceProperty>> capPropertiesOptional = getCapProperties(containerComponent);
90
91         if(capPropertiesOptional.isPresent()) {
92             return capPropertiesOptional.get().stream().filter(propertyDefinition ->
93                     propertyDefinition.getUniqueId().equals(propertyUniqueId)).findAny();
94         } else {
95             return Optional.empty();
96         }
97     }
98
99     private static Optional<List<ComponentInstanceProperty>> getCapProperties(Component containerComponent) {
100         Map<String, List<CapabilityDefinition>> componentCapabilities = containerComponent.getCapabilities();
101         if(MapUtils.isEmpty(componentCapabilities)){
102             return Optional.empty();
103         }
104         List<CapabilityDefinition> capabilityDefinitionList = componentCapabilities.values()
105                 .stream().flatMap(Collection::stream).collect(Collectors.toList());
106         if(CollectionUtils.isEmpty(capabilityDefinitionList)){
107             return Optional.empty();
108         }
109         List<ComponentInstanceProperty> allComponentInstanceCapProperties= new ArrayList<>();
110         capabilityDefinitionList.stream().filter(capabilityDefinition -> CollectionUtils.isNotEmpty(capabilityDefinition
111                 .getProperties())).collect(Collectors.toList()).forEach(capabilityDefinition ->
112                 allComponentInstanceCapProperties.addAll(capabilityDefinition.getProperties()));
113         return Optional.of(allComponentInstanceCapProperties);
114     }
115
116     public static Optional<CapabilityDefinition> getPropertyCapabilityOfChildInstance(String propertyParentUniqueId,
117                                                                                       Map<String, List<CapabilityDefinition>>
118                                                                                componentCapabilities) {
119         if(MapUtils.isEmpty(componentCapabilities)){
120             return Optional.empty();
121         }
122         List<CapabilityDefinition> capabilityDefinitionList = componentCapabilities.values()
123                 .stream().flatMap(Collection::stream).collect(Collectors.toList());
124         if(CollectionUtils.isEmpty(capabilityDefinitionList)){
125             return Optional.empty();
126         }
127         return capabilityDefinitionList.stream()
128                 .filter(capabilityDefinition -> capabilityDefinition.getUniqueId().equals(propertyParentUniqueId) &&
129                         capabilityDefinition.getPath().size() == 1)
130                 .findAny();
131     }
132
133     public static Optional<CapabilityDefinition> getPropertyCapabilityFromAllCapProps(String propertyParentUniqueId,
134                                                                                       List<CapabilityDefinition>
135                                                                                    capabilityDefinitionList) {
136         return capabilityDefinitionList.stream()
137                 .filter(capabilityDefinition -> capabilityDefinition.getUniqueId().equals(propertyParentUniqueId))
138                 .findAny();
139     }
140
141     public static boolean isNodeProperty(String propertyName, List<PropertyDefinition> properties) {
142
143         return !CollectionUtils.isEmpty(properties) && properties.stream().anyMatch(property -> property.getName()
144                 .equals(propertyName));
145     }
146
147     private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
148         Set<Object> seen = new HashSet<>();
149         return t -> seen.add(keyExtractor.apply(t));
150     }
151
152     public static Optional<ComponentInstanceProperty> getPropertyByInputId(Component component, String inputId) {
153         List<InputDefinition> componentInputs = component.getInputs();
154         if(CollectionUtils.isEmpty(componentInputs)) {
155             return Optional.empty();
156         }
157         Optional<InputDefinition> inputDefinition = componentInputs.stream().filter(cip -> cip.getUniqueId()
158                 .equals(inputId)).findFirst();
159         if(!inputDefinition.isPresent()) {
160             return Optional.empty();
161         }
162         Optional<List<ComponentInstanceProperty>> capProperties = getCapProperties(component);
163         if(!capProperties.isPresent()) {
164             return Optional.empty();
165         }
166
167         return capProperties.get().stream().filter(capProp -> CollectionUtils.isNotEmpty(capProp.getGetInputValues()) &&
168                 capProp.getGetInputValues().stream().anyMatch(capPropInp -> capPropInp.getInputId().equals(inputId)) &&
169                 capProp.getUniqueId().equals(inputDefinition.get().getPropertyId())).findAny();
170     }
171
172     public static List<ComponentInstanceProperty> getCapabilityProperty(ComponentInstanceProperty capabilityProperty,
173                                                                   String inputId) {
174         List<ComponentInstanceProperty> resList = new ArrayList<>();
175         List<GetInputValueDataDefinition> inputsValues = capabilityProperty.getGetInputValues();
176         if (CollectionUtils.isNotEmpty(inputsValues) &&  inputsValues.stream().anyMatch(inputData ->
177                 isGetInputValueForInput(inputData, inputId))) {
178                 resList.add(capabilityProperty);
179         }
180         return resList;
181     }
182
183     public static boolean isNodeServiceProxy(Component component) {
184         if (component.getComponentType().equals(ComponentTypeEnum.SERVICE)) {
185             return true;
186         }
187         Resource resource = (Resource) component;
188         ResourceTypeEnum resType = resource.getResourceType();
189         return resType.equals(ResourceTypeEnum.ServiceProxy);
190     }
191 }