aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ToscaFunctionService.java
blob: 28843af54ade97c79c7b7b6568ef28176a12ef9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * -
 *  ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Nordix Foundation.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.impl;

import java.util.List;
import java.util.Map;
import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction;
import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.PropertySource;
import org.openecomp.sdc.be.model.AttributeDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;

@org.springframework.stereotype.Service
public class ToscaFunctionService {

    final List<ToscaFunctionType> functionTypesToUpdateList =
        List.of(ToscaFunctionType.GET_INPUT, ToscaFunctionType.GET_ATTRIBUTE, ToscaFunctionType.GET_PROPERTY, ToscaFunctionType.CONCAT);

    /**
     * Sets the sourceUniqueId, sourceName and propertyUniqueId values in a ToscaFunction based on values from the given component. This method may be
     * useful during creation of a ToscaFunction where these values are not otherwise provided.
     *
     * @param toscaFunctionToUpdate the TOSCA function to update
     * @param selfComponent         the SELF Component
     * @param instancePropertyMap   the SELF Component instances properties
     * @param instanceAttributeMap  the SELF Component instances attributes
     */
    public void updateFunctionWithDataFromSelfComponent(final ToscaFunction toscaFunctionToUpdate, final Component selfComponent,
                                                        final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
                                                        final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
        switch (toscaFunctionToUpdate.getType()) {
            case GET_INPUT: {
                updateGetInputFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent);
                break;
            }
            case GET_PROPERTY:
            case GET_ATTRIBUTE: {
                updateGetPropertyFunction((ToscaGetFunctionDataDefinition) toscaFunctionToUpdate, selfComponent, instancePropertyMap,
                    instanceAttributeMap);
                break;
            }
            case CONCAT:
                updateConcatFunction((ToscaConcatFunction) toscaFunctionToUpdate, selfComponent, instancePropertyMap, instanceAttributeMap);
                break;
        }
    }

    /**
     * Updates the TOSCA concat function parameters, where the parameter is a TOSCA function.
     *
     * @param concatFunction       the TOSCA concat function to update
     * @param selfComponent        the SELF component
     * @param instancePropertyMap  the component instances properties
     * @param instanceAttributeMap the component instances attributes
     */
    private void updateConcatFunction(final ToscaConcatFunction concatFunction, final Component selfComponent,
                                      final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
                                      final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
        concatFunction.getParameters().stream()
            .filter(ToscaFunction.class::isInstance)
            .filter(functionParameter -> functionTypesToUpdateList.contains(functionParameter.getType()))
            .forEach(functionParameter ->
                updateFunctionWithDataFromSelfComponent((ToscaFunction) functionParameter, selfComponent, instancePropertyMap, instanceAttributeMap));
    }

    /**
     * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get_input function.
     *
     * @param toscaGetFunction the TOSCA get_input function to update
     * @param selfComponent    the SELF component
     */
    private void updateGetInputFunction(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component selfComponent) {
        toscaGetFunction.setSourceUniqueId(selfComponent.getUniqueId());
        toscaGetFunction.setSourceName(selfComponent.getName());
        selfComponent.getInputs().stream()
            .filter(inputDefinition -> inputDefinition.getName().equals(toscaGetFunction.getPropertyName()))
            .findAny().ifPresent(input ->
                toscaGetFunction.setPropertyUniqueId(input.getUniqueId())
            );
    }

    /**
     * Updates the Source Unique Id, the Source Name and the Property Unique Id of the TOSCA get function.
     *
     * @param toscaGetFunction     the TOSCA get function to update
     * @param selfComponent        the SELF component
     * @param instancePropertyMap  the component instances properties
     * @param instanceAttributeMap the component instances attributes
     */
    private void updateGetPropertyFunction(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component selfComponent,
                                           final Map<String, List<ComponentInstanceProperty>> instancePropertyMap,
                                           final Map<String, List<AttributeDefinition>> instanceAttributeMap) {
        if (toscaGetFunction.getPropertySource() == PropertySource.SELF) {
            toscaGetFunction.setSourceUniqueId(selfComponent.getUniqueId());
            toscaGetFunction.setSourceName(selfComponent.getName());
            if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY) {
                selfComponent.getProperties().stream()
                    .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
                    .findAny()
                    .ifPresent(property ->
                        toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
                    );
            } else {
                selfComponent.getAttributes().stream()
                    .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
                    .findAny()
                    .ifPresent(attribute ->
                        toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())
                    );
            }
        } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) {
            selfComponent.getComponentInstances().stream()
                .filter(componentInstance -> toscaGetFunction.getSourceName().equals(componentInstance.getName()))
                .findAny()
                .ifPresent(componentInstance -> toscaGetFunction.setSourceUniqueId(componentInstance.getUniqueId()));
            if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY) {
                final List<ComponentInstanceProperty> instanceProperties = instancePropertyMap.get(toscaGetFunction.getSourceUniqueId());
                instanceProperties.stream()
                    .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
                    .findAny()
                    .ifPresent(property ->
                        toscaGetFunction.setPropertyUniqueId(property.getUniqueId())
                    );
            } else {
                final List<AttributeDefinition> instanceAttributes = instanceAttributeMap.get(toscaGetFunction.getSourceUniqueId());
                instanceAttributes.stream()
                    .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0)))
                    .findAny()
                    .ifPresent(attribute ->
                        toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())
                    );
            }
        }
    }

}