82958a7532a2d57e1e412f4001b720c3b1215a1f
[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.fasterxml.jackson.annotation.JsonIgnore;
25 import com.google.gson.Gson;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
31 import lombok.Data;
32 import org.apache.commons.collections4.CollectionUtils;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
35 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
36
37 @Data
38 public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunctionParameter {
39
40     private String propertyUniqueId;
41     private String propertyName;
42     private PropertySource propertySource;
43     private String sourceUniqueId;
44     private String sourceName;
45     private ToscaGetFunctionType functionType;
46     private List<String> propertyPathFromSource = new ArrayList<>();
47     private List<Object> toscaIndexList;
48
49     public ToscaGetFunctionDataDefinition() {
50         //necessary for JSON conversions
51     }
52
53     @JsonIgnore
54     public boolean isSubProperty() {
55         return propertyPathFromSource != null && propertyPathFromSource.size() > 1;
56     }
57
58     /**
59      * Builds the value of a property based on the TOSCA get function information.
60      */
61     public String generatePropertyValue() {
62         return new Gson().toJson(getJsonObjectValue());
63     }
64
65     @JsonIgnore
66     @Override
67     public Object getJsonObjectValue() {
68         if (functionType == null) {
69             throw new IllegalStateException("functionType is required in order to generate the get function value");
70         }
71         if (CollectionUtils.isEmpty(propertyPathFromSource)) {
72             throw new IllegalStateException("propertyPathFromSource is required in order to generate the get function value");
73         }
74
75         if (functionType == ToscaGetFunctionType.GET_PROPERTY || functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
76             return buildFunctionValueWithPropertySource();
77         }
78         if (functionType == ToscaGetFunctionType.GET_INPUT) {
79             return buildGetInputFunctionValue();
80         }
81
82         throw new UnsupportedOperationException(String.format("ToscaGetFunctionType '%s' is not supported yet", functionType));
83     }
84
85     private Map<String, Object> buildFunctionValueWithPropertySource() {
86         if (propertySource == null) {
87             throw new IllegalStateException(
88                 String.format("propertySource is required in order to generate the %s value", functionType.getFunctionName())
89             );
90         }
91         if (propertySource == PropertySource.SELF) {
92             if (CollectionUtils.isNotEmpty(toscaIndexList)) {
93                 List<Object> parsedIndexList = new ArrayList<Object>();
94                 toscaIndexList.forEach((obj) -> {
95                     parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj);
96                 });
97                 return Map.of(functionType.getFunctionName(),
98                     Stream.concat(Stream.of(PropertySource.SELF.getName()), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList())
99                 );
100             }
101             return Map.of(functionType.getFunctionName(),
102                 Stream.concat(Stream.of(PropertySource.SELF.getName()), propertyPathFromSource.stream()).collect(Collectors.toList())
103             );
104         }
105         if (propertySource == PropertySource.INSTANCE) {
106             if (sourceName == null) {
107                 throw new IllegalStateException(
108                     String.format("sourceName is required in order to generate the %s from INSTANCE value", functionType.getFunctionName())
109                 );
110             }
111             if (CollectionUtils.isNotEmpty(toscaIndexList)) {
112                 List<Object> parsedIndexList = new ArrayList<Object>();
113                 toscaIndexList.forEach((obj) -> {
114                     parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj);
115                 });
116                 return Map.of(functionType.getFunctionName(),
117                     Stream.concat(Stream.of(sourceName), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList())
118                 );
119             }
120             return Map.of(functionType.getFunctionName(),
121                 Stream.concat(Stream.of(sourceName), propertyPathFromSource.stream()).collect(Collectors.toList())
122             );
123         }
124
125         throw new UnsupportedOperationException(String.format("Tosca property source '%s' not supported", propertySource));
126     }
127
128     private Map<String, Object> buildGetInputFunctionValue() {
129         List<Object> propertySourceCopy = new ArrayList<Object>(this.propertyPathFromSource);
130         List<Object> propertySourceOneCopy = new ArrayList<>();
131         propertySourceOneCopy.add(this.propertyPathFromSource.get(0));
132         if (CollectionUtils.isNotEmpty(toscaIndexList)) {
133             propertySourceCopy.addAll(toscaIndexList);
134             propertySourceOneCopy.addAll(toscaIndexList);
135         }
136         if (this.propertyPathFromSource.size() == 1) {
137             return Map.of(this.functionType.getFunctionName(), propertySourceOneCopy);
138         }
139         return Map.of(this.functionType.getFunctionName(), propertySourceCopy);
140     }
141
142     @Override
143     public ToscaFunctionType getType() {
144         if (functionType == null) {
145             return null;
146         }
147         switch (functionType) {
148             case GET_INPUT:
149                 return ToscaFunctionType.GET_INPUT;
150             case GET_PROPERTY:
151                 return ToscaFunctionType.GET_PROPERTY;
152             case GET_ATTRIBUTE:
153                 return ToscaFunctionType.GET_ATTRIBUTE;
154             default:
155                 return null;
156         }
157     }
158
159     @JsonIgnore
160     @Override
161     public String getValue() {
162         return this.generatePropertyValue();
163     }
164
165 }