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