f19217e69c7f5662f7f1f39c05d0ef29d83314da
[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 implements ToscaFunction, ToscaFunctionParameter {
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         return new Gson().toJson(getJsonObjectValue());
59     }
60
61     @Override
62     public Object getJsonObjectValue() {
63         if (functionType == null) {
64             throw new IllegalStateException("functionType is required in order to generate the get function value");
65         }
66         if (CollectionUtils.isEmpty(propertyPathFromSource)) {
67             throw new IllegalStateException("propertyPathFromSource is required in order to generate the get function value");
68         }
69
70         if (functionType == ToscaGetFunctionType.GET_PROPERTY || functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
71             return buildFunctionValueWithPropertySource();
72         }
73         if (functionType == ToscaGetFunctionType.GET_INPUT) {
74             return buildGetInputFunctionValue();
75         }
76
77         throw new UnsupportedOperationException(String.format("ToscaGetFunctionType '%s' is not supported yet", functionType));
78     }
79
80     private Map<String, Object> buildFunctionValueWithPropertySource() {
81         if (propertySource == null) {
82             throw new IllegalStateException(
83                 String.format("propertySource is required in order to generate the %s value", functionType.getFunctionName())
84             );
85         }
86         if (propertySource == PropertySource.SELF) {
87             return Map.of(functionType.getFunctionName(),
88                 Stream.concat(Stream.of(PropertySource.SELF.getName()), propertyPathFromSource.stream()).collect(Collectors.toList())
89             );
90         }
91         if (propertySource == PropertySource.INSTANCE) {
92             if (sourceName == null) {
93                 throw new IllegalStateException(
94                     String.format("sourceName is required in order to generate the %s from INSTANCE value", functionType.getFunctionName())
95                 );
96             }
97             return Map.of(functionType.getFunctionName(),
98                 Stream.concat(Stream.of(sourceName), propertyPathFromSource.stream()).collect(Collectors.toList())
99             );
100         }
101
102         throw new UnsupportedOperationException(String.format("Tosca property source '%s' not supported", propertySource));
103     }
104
105     private Map<String, Object> buildGetInputFunctionValue() {
106         if (this.propertyPathFromSource.size() == 1) {
107             return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource.get(0));
108         }
109         return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource);
110     }
111
112     @Override
113     public ToscaFunctionType getType() {
114         if (functionType == null) {
115             return null;
116         }
117         switch (functionType) {
118             case GET_INPUT:
119                 return ToscaFunctionType.GET_INPUT;
120             case GET_PROPERTY:
121                 return ToscaFunctionType.GET_PROPERTY;
122             case GET_ATTRIBUTE:
123                 return ToscaFunctionType.GET_ATTRIBUTE;
124             default:
125                 return null;
126         }
127     }
128
129     @Override
130     public String getValue() {
131         return this.generatePropertyValue();
132     }
133
134 }