Support of get_property in property assignment
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / ToscaGetFunctionDataDefinition.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.datatypes.elements;
23
24 import com.google.gson.Gson;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
30 import lombok.Data;
31 import org.apache.commons.collections4.CollectionUtils;
32 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
33 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
34
35 @Data
36 public class ToscaGetFunctionDataDefinition {
37
38     private String propertyUniqueId;
39     private String propertyName;
40     private PropertySource propertySource;
41     private String sourceUniqueId;
42     private String sourceName;
43     private ToscaGetFunctionType functionType;
44     private List<String> propertyPathFromSource = new ArrayList<>();
45
46     public ToscaGetFunctionDataDefinition() {
47         //necessary for JSON conversions
48     }
49
50     public boolean isSubProperty() {
51         return propertyPathFromSource != null && propertyPathFromSource.size() > 1;
52     }
53
54     /**
55      * Builds the value of a property based on the TOSCA get function information.
56      */
57     public String generatePropertyValue() {
58         if (functionType == null) {
59             throw new IllegalStateException("functionType is required in order to generate the get function value");
60         }
61         if (CollectionUtils.isEmpty(propertyPathFromSource)) {
62             throw new IllegalStateException("propertyPathFromSource is required in order to generate the get function value");
63         }
64
65         final var gson = new Gson();
66         if (functionType == ToscaGetFunctionType.GET_PROPERTY) {
67             return gson.toJson(buildGetPropertyFunctionValue());
68         }
69         if (functionType == ToscaGetFunctionType.GET_INPUT) {
70             return gson.toJson(buildGetInputFunctionValue());
71         }
72
73         throw new UnsupportedOperationException(String.format("ToscaGetFunctionType '%s' is not supported yet", functionType));
74     }
75
76     private Map<String, Object> buildGetPropertyFunctionValue() {
77         if (propertySource == null) {
78             throw new IllegalStateException("propertySource is required in order to generate the get_property value");
79         }
80         if (propertySource == PropertySource.SELF) {
81             return Map.of(functionType.getFunctionName(),
82                 Stream.concat(Stream.of(PropertySource.SELF.getName()), propertyPathFromSource.stream()).collect(Collectors.toList())
83             );
84         }
85         if (propertySource == PropertySource.INSTANCE) {
86             if (sourceName == null) {
87                 throw new IllegalStateException("sourceName is required in order to generate the get_property from INSTANCE value");
88             }
89             return Map.of(functionType.getFunctionName(),
90                 Stream.concat(Stream.of(sourceName), propertyPathFromSource.stream()).collect(Collectors.toList())
91             );
92         }
93
94         throw new UnsupportedOperationException(String.format("Tosca property source '%s' not supported", propertySource));
95     }
96
97     private Map<String, Object> buildGetInputFunctionValue() {
98         if (this.propertyPathFromSource.size() == 1) {
99             return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource.get(0));
100         }
101         return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource);
102     }
103
104 }