Support for TOSCA functions for Service Import
[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 org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
27 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
28 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
29 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
30 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
31 import org.openecomp.sdc.be.model.AttributeDefinition;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
34
35 @org.springframework.stereotype.Service
36 public class ToscaFunctionService {
37
38     final List<ToscaFunctionType> functionTypesToUpdateList =
39         List.of(ToscaFunctionType.GET_INPUT, ToscaFunctionType.GET_ATTRIBUTE, ToscaFunctionType.GET_PROPERTY, ToscaFunctionType.CONCAT);
40
41     /**
42      * Sets the sourceUniqueId, sourceName and propertyUniqueId values in a ToscaFunction based on values from the given component. This method may be
43      * useful during creation of a ToscaFunction where these values are not otherwise provided.
44      *
45      * @param toscaFunctionToUpdate the TOSCA function to update
46      * @param selfComponent         the SELF Component
47      * @param instancePropertyMap   the SELF Component instances properties
48      * @param instanceAttributeMap  the SELF Component instances attributes
49      */
50     public void updateFunctionWithDataFromSelfComponent(final ToscaFunction toscaFunctionToUpdate, final Component selfComponent,
51                                                         final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
52                                                         final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
53         switch (toscaFunctionToUpdate.getType()) {
54             case GET_INPUT: {
55                 updateGetInputFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent);
56                 break;
57             }
58             case GET_PROPERTY:
59             case GET_ATTRIBUTE: {
60                 updateGetPropertyFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent, instancePropertyMap,
61                     instanceAttributeMap);
62                 break;
63             }
64             case CONCAT:
65                 updateConcatFunction((ToscaConcatFunction) toscaFunctionToUpdate, selfComponent, instancePropertyMap, instanceAttributeMap);
66                 break;
67         }
68     }
69
70     /**
71      * Updates the TOSCA concat function parameters, where the parameter is a TOSCA function.
72      *
73      * @param concatFunction       the TOSCA concat function to update
74      * @param selfComponent        the SELF component
75      * @param instancePropertyMap  the component instances properties
76      * @param instanceAttributeMap the component instances attributes
77      */
78     private void updateConcatFunction(final ToscaConcatFunction concatFunction, final Component selfComponent,
79                                       final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
80                                       final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
81         concatFunction.getParameters().stream()
82             .filter(ToscaFunction.class::isInstance)
83             .filter(functionParameter -> functionTypesToUpdateList.contains(functionParameter.getType()))
84             .forEach(functionParameter ->
85                 updateFunctionWithDataFromSelfComponent((ToscaFunction) functionParameter, selfComponent, instancePropertyMap, instanceAttributeMap));
86     }
87
88     /**
89      * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get_input function.
90      *
91      * @param toscaGetFunction the TOSCA get_input function to update
92      * @param selfComponent    the SELF component
93      */
94     private void updateGetInputFunction(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component selfComponent) {
95         toscaGetFunction.setSourceUniqueId(selfComponent.getUniqueId());
96         toscaGetFunction.setSourceName(selfComponent.getName());
97         selfComponent.getInputs().stream()
98             .filter(inputDefinition -> inputDefinition.getName().equals(toscaGetFunction.getPropertyName()))
99             .findAny().ifPresent(input ->
100                 toscaGetFunction.setPropertyUniqueId(input.getUniqueId())
101             );
102     }
103
104     /**
105      * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get function.
106      *
107      * @param toscaGetFunction     the TOSCA get function to update
108      * @param selfComponent        the SELF component
109      * @param instancePropertyMap  the component instances properties
110      * @param instanceAttributeMap the component instances attributes
111      */
112     private void updateGetPropertyFunction(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component selfComponent,
113                                            final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
114                                            final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
115         if (toscaGetFunction.getPropertySource() == PropertySource.SELF) {
116             toscaGetFunction.setSourceUniqueId(selfComponent.getUniqueId());
117             toscaGetFunction.setSourceName(selfComponent.getName());
118             if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY) {
119                 selfComponent.getProperties().stream()
120                     .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
121                     .findAny()
122                     .ifPresent(property ->
123                         toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
124                     );
125             } else {
126                 selfComponent.getAttributes().stream()
127                     .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
128                     .findAny()
129                     .ifPresent(attribute ->
130                         toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())
131                     );
132             }
133         } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) {
134             selfComponent.getComponentInstances().stream()
135                 .filter(componentInstance -> toscaGetFunction.getSourceName().equals(componentInstance.getName()))
136                 .findAny()
137                 .ifPresent(componentInstance -> toscaGetFunction.setSourceUniqueId(componentInstance.getUniqueId()));
138             if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY) {
139                 final List<ComponentInstanceProperty> instanceProperties = instancePropertyMap.get(toscaGetFunction.getSourceUniqueId());
140                 instanceProperties.stream()
141                     .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
142                     .findAny()
143                     .ifPresent(property ->
144                         toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
145                     );
146             } else {
147                 final List<AttributeDefinition> instanceAttributes = instanceAttributeMap.get(toscaGetFunction.getSourceUniqueId());
148                 instanceAttributes.stream()
149                     .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
150                     .findAny()
151                     .ifPresent(attribute ->
152                         toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())
153                     );
154             }
155         }
156     }
157
158 }