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 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;
38 @org.springframework.stereotype.Service
39 public class ToscaFunctionService {
41 final List<ToscaFunctionType> functionTypesToUpdateList =
42 List.of(ToscaFunctionType.GET_INPUT, ToscaFunctionType.GET_ATTRIBUTE, ToscaFunctionType.GET_PROPERTY, ToscaFunctionType.CONCAT);
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.
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
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()) {
58 updateGetInputFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent);
63 updateGetPropertyFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent, instancePropertyMap,
64 instanceAttributeMap);
68 updateConcatFunction((ToscaConcatFunction) toscaFunctionToUpdate, selfComponent, instancePropertyMap, instanceAttributeMap);
74 * Updates the TOSCA concat function parameters, where the parameter is a TOSCA function.
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
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));
92 * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get_input function.
94 * @param toscaGetFunction the TOSCA get_input function to update
95 * @param selfComponent the SELF component
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())
108 * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get function.
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
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);
124 if (isGetPropertyOrPropertyIdNotSetAndComponentHasProperties(toscaGetFunction, selfComponent)) {
125 setPropertyIdFromProperty(selfComponent, toscaGetFunction);
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);
132 if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY || StringUtils.isEmpty(toscaGetFunction.getPropertyUniqueId())) {
133 setPropertyIdFromInstanceProperty(instancePropertyMap, toscaGetFunction);
138 private boolean isGetAttributeAndComponentHasAttributes(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component component) {
139 return toscaGetFunction.getType() == ToscaFunctionType.GET_ATTRIBUTE && CollectionUtils.isNotEmpty(component.getAttributes());
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());
146 private void setPropertyIdFromAttribute(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) {
147 component.getAttributes().stream()
148 .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
150 .ifPresent(attribute -> toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId()));
153 private void setPropertyIdFromProperty(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) {
154 component.getProperties().stream()
155 .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
157 .ifPresent(property ->
158 toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
162 private void setSourceIdFromInstance(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) {
163 component.getComponentInstances().stream()
164 .filter(componentInstance -> toscaGetFunction.getSourceName().equals(componentInstance.getName()))
166 .ifPresent(componentInstance -> toscaGetFunction.setSourceUniqueId(componentInstance.getUniqueId()));
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)))
175 .ifPresent(attribute ->
176 toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())
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)))
187 .ifPresent(property ->
188 toscaGetFunction.setPropertyUniqueId(property.getUniqueId())