Import services with sub prop tosca functions
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ToscaFunctionService.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.components.impl;
23
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import org.apache.commons.collections.CollectionUtils;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
30 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
31 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
32 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
33 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
34 import org.openecomp.sdc.be.model.AttributeDefinition;
35 import org.openecomp.sdc.be.model.Component;
36 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
37
38 @org.springframework.stereotype.Service
39 public class ToscaFunctionService {
40
41     final List<ToscaFunctionType> functionTypesToUpdateList =
42         List.of(ToscaFunctionType.GET_INPUT, ToscaFunctionType.GET_ATTRIBUTE, ToscaFunctionType.GET_PROPERTY, ToscaFunctionType.CONCAT);
43
44     /**
45      * Sets the sourceUniqueId, sourceName and propertyUniqueId values in a ToscaFunction based on values from the given component. This method may be
46      * useful during creation of a ToscaFunction where these values are not otherwise provided.
47      *
48      * @param toscaFunctionToUpdate the TOSCA function to update
49      * @param selfComponent         the SELF Component
50      * @param instancePropertyMap   the SELF Component instances properties
51      * @param instanceAttributeMap  the SELF Component instances attributes
52      */
53     public void updateFunctionWithDataFromSelfComponent(final ToscaFunction toscaFunctionToUpdate, final Component selfComponent,
54                                                         final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
55                                                         final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
56         switch (toscaFunctionToUpdate.getType()) {
57             case GET_INPUT: {
58                 updateGetInputFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent);
59                 break;
60             }
61             case GET_PROPERTY:
62             case GET_ATTRIBUTE: {
63                 updateGetPropertyFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent, instancePropertyMap,
64                     instanceAttributeMap);
65                 break;
66             }
67             case CONCAT:
68                 updateConcatFunction((ToscaConcatFunction) toscaFunctionToUpdate, selfComponent, instancePropertyMap, instanceAttributeMap);
69                 break;
70         }
71     }
72
73     /**
74      * Updates the TOSCA concat function parameters, where the parameter is a TOSCA function.
75      *
76      * @param concatFunction       the TOSCA concat function to update
77      * @param selfComponent        the SELF component
78      * @param instancePropertyMap  the component instances properties
79      * @param instanceAttributeMap the component instances attributes
80      */
81     private void updateConcatFunction(final ToscaConcatFunction concatFunction, final Component selfComponent,
82                                       final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
83                                       final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
84         concatFunction.getParameters().stream()
85             .filter(ToscaFunction.class::isInstance)
86             .filter(functionParameter -> functionTypesToUpdateList.contains(functionParameter.getType()))
87             .forEach(functionParameter ->
88                 updateFunctionWithDataFromSelfComponent((ToscaFunction) functionParameter, selfComponent, instancePropertyMap, instanceAttributeMap));
89     }
90
91     /**
92      * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get_input function.
93      *
94      * @param toscaGetFunction the TOSCA get_input function to update
95      * @param selfComponent    the SELF component
96      */
97     private void updateGetInputFunction(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component selfComponent) {
98         toscaGetFunction.setSourceUniqueId(selfComponent.getUniqueId());
99         toscaGetFunction.setSourceName(selfComponent.getName());
100         selfComponent.getInputs().stream()
101             .filter(inputDefinition -> inputDefinition.getName().equals(toscaGetFunction.getPropertyName()))
102             .findAny().ifPresent(input ->
103                 toscaGetFunction.setPropertyUniqueId(input.getUniqueId())
104             );
105     }
106
107     /**
108      * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get function.
109      *
110      * @param toscaGetFunction     the TOSCA get function to update
111      * @param selfComponent        the SELF component
112      * @param instancePropertyMap  the component instances properties
113      * @param instanceAttributeMap the component instances attributes
114      */
115     private void updateGetPropertyFunction(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component selfComponent,
116                                            final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
117                                            final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
118         if (toscaGetFunction.getPropertySource() == PropertySource.SELF) {
119             toscaGetFunction.setSourceUniqueId(selfComponent.getUniqueId());
120             toscaGetFunction.setSourceName(selfComponent.getName());
121             if (isGetAttributeAndComponentHasAttributes(toscaGetFunction, selfComponent)) {
122                 setPropertyIdFromAttribute(selfComponent, toscaGetFunction);
123             }
124             if (isGetPropertyOrPropertyIdNotSetAndComponentHasProperties(toscaGetFunction, selfComponent)) {
125                 setPropertyIdFromProperty(selfComponent, toscaGetFunction);
126             }
127         } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE && CollectionUtils.isNotEmpty(selfComponent.getComponentInstances())) {
128             setSourceIdFromInstance(selfComponent, toscaGetFunction);
129             if (toscaGetFunction.getType() == ToscaFunctionType.GET_ATTRIBUTE) {
130                 setPropertyIdFromInstanceAttribute(instanceAttributeMap, toscaGetFunction);
131             }
132             if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY || StringUtils.isEmpty(toscaGetFunction.getPropertyUniqueId())) {
133                 setPropertyIdFromInstanceProperty(instancePropertyMap, toscaGetFunction);
134             }
135         }
136     }
137     
138     private boolean isGetAttributeAndComponentHasAttributes(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component component) {
139         return toscaGetFunction.getType() == ToscaFunctionType.GET_ATTRIBUTE && CollectionUtils.isNotEmpty(component.getAttributes());
140     }
141     
142     private boolean isGetPropertyOrPropertyIdNotSetAndComponentHasProperties(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component component) {
143         return (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY || StringUtils.isEmpty(toscaGetFunction.getPropertyUniqueId())) && CollectionUtils.isNotEmpty(component.getProperties());
144     }
145     
146     private void setPropertyIdFromAttribute(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) {
147         component.getAttributes().stream()
148             .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
149             .findAny()
150             .ifPresent(attribute -> toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId()));
151     }
152     
153     private void setPropertyIdFromProperty(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) {
154         component.getProperties().stream()
155             .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
156             .findAny()
157             .ifPresent(property ->
158                 toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
159             );
160     }
161     
162     private void setSourceIdFromInstance(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) {
163         component.getComponentInstances().stream()
164             .filter(componentInstance -> toscaGetFunction.getSourceName().equals(componentInstance.getName()))
165             .findAny()
166             .ifPresent(componentInstance -> toscaGetFunction.setSourceUniqueId(componentInstance.getUniqueId()));
167     }
168     
169     private void setPropertyIdFromInstanceAttribute(final Map<String, List<AttributeDefinition>> instanceAttributeMap, final ToscaGetFunctionDataDefinition toscaGetFunction) {
170         final List<AttributeDefinition> instanceAttributes = instanceAttributeMap.get(toscaGetFunction.getSourceUniqueId());
171         if (CollectionUtils.isNotEmpty(instanceAttributes)) {
172             instanceAttributes.stream()
173                 .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
174                 .findAny()
175                 .ifPresent(attribute ->
176                     toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())
177                 );
178         }
179     }
180     
181     private void setPropertyIdFromInstanceProperty(final Map<String, List<ComponentInstanceProperty>> instancePropertyMap, final ToscaGetFunctionDataDefinition toscaGetFunction) {
182         final List<ComponentInstanceProperty> instanceProperties = instancePropertyMap.get(toscaGetFunction.getSourceUniqueId());
183         if (CollectionUtils.isNotEmpty(instanceProperties)) {
184             instanceProperties.stream()
185                 .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
186                 .findAny()
187                 .ifPresent(property ->
188                     toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
189                 );
190         }
191     }
192
193 }