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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.components.impl;
24 import java.util.List;
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;
35 @org.springframework.stereotype.Service
36 public class ToscaFunctionService {
38 final List<ToscaFunctionType> functionTypesToUpdateList =
39 List.of(ToscaFunctionType.GET_INPUT, ToscaFunctionType.GET_ATTRIBUTE, ToscaFunctionType.GET_PROPERTY, ToscaFunctionType.CONCAT);
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.
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
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()) {
55 updateGetInputFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent);
60 updateGetPropertyFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent, instancePropertyMap,
61 instanceAttributeMap);
65 updateConcatFunction((ToscaConcatFunction) toscaFunctionToUpdate, selfComponent, instancePropertyMap, instanceAttributeMap);
71 * Updates the TOSCA concat function parameters, where the parameter is a TOSCA function.
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
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));
89 * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get_input function.
91 * @param toscaGetFunction the TOSCA get_input function to update
92 * @param selfComponent the SELF component
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())
105 * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get function.
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
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)))
122 .ifPresent(property ->
123 toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
126 selfComponent.getAttributes().stream()
127 .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
129 .ifPresent(attribute ->
130 toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())
133 } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) {
134 selfComponent.getComponentInstances().stream()
135 .filter(componentInstance -> toscaGetFunction.getSourceName().equals(componentInstance.getName()))
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)))
143 .ifPresent(property ->
144 toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
147 final List<AttributeDefinition> instanceAttributes = instanceAttributeMap.get(toscaGetFunction.getSourceUniqueId());
148 instanceAttributes.stream()
149 .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
151 .ifPresent(attribute ->
152 toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())