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.datatypes.elements;
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25 import com.google.gson.Gson;
26 import java.util.ArrayList;
27 import java.util.List;
29 import java.util.stream.Collectors;
30 import java.util.stream.Stream;
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;
38 public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunctionParameter {
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;
49 public ToscaGetFunctionDataDefinition() {
50 //necessary for JSON conversions
54 public boolean isSubProperty() {
55 return propertyPathFromSource != null && propertyPathFromSource.size() > 1;
59 * Builds the value of a property based on the TOSCA get function information.
61 public String generatePropertyValue() {
62 return new Gson().toJson(getJsonObjectValue());
67 public Object getJsonObjectValue() {
68 if (functionType == null) {
69 throw new IllegalStateException("functionType is required in order to generate the get function value");
71 if (CollectionUtils.isEmpty(propertyPathFromSource)) {
72 throw new IllegalStateException("propertyPathFromSource is required in order to generate the get function value");
75 if (functionType == ToscaGetFunctionType.GET_PROPERTY || functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
76 return buildFunctionValueWithPropertySource();
78 if (functionType == ToscaGetFunctionType.GET_INPUT) {
79 return buildGetInputFunctionValue();
82 throw new UnsupportedOperationException(String.format("ToscaGetFunctionType '%s' is not supported yet", functionType));
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())
91 if (propertySource == PropertySource.SELF) {
92 if (toscaIndexList.size() > 0) {
93 List<Object> parsedIndexList = new ArrayList<Object>();
94 toscaIndexList.forEach((obj) -> {
95 parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj);
97 return Map.of(functionType.getFunctionName(),
98 Stream.concat(Stream.of(PropertySource.SELF.getName()), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList())
101 return Map.of(functionType.getFunctionName(),
102 Stream.concat(Stream.of(PropertySource.SELF.getName()), propertyPathFromSource.stream()).collect(Collectors.toList())
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())
111 if (toscaIndexList.size() > 0) {
112 List<Object> parsedIndexList = new ArrayList<Object>();
113 toscaIndexList.forEach((obj) -> {
114 parsedIndexList.add(StringUtils.isNumeric(obj.toString()) ? Integer.parseInt(obj.toString()) : obj);
116 return Map.of(functionType.getFunctionName(),
117 Stream.concat(Stream.of(sourceName), Stream.concat(propertyPathFromSource.stream(),parsedIndexList.stream())).collect(Collectors.toList())
120 return Map.of(functionType.getFunctionName(),
121 Stream.concat(Stream.of(sourceName), propertyPathFromSource.stream()).collect(Collectors.toList())
125 throw new UnsupportedOperationException(String.format("Tosca property source '%s' not supported", propertySource));
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 (toscaIndexList.size() > 0) {
133 propertySourceCopy.addAll(toscaIndexList);
134 propertySourceOneCopy.addAll(toscaIndexList);
136 if (this.propertyPathFromSource.size() == 1) {
137 return Map.of(this.functionType.getFunctionName(), propertySourceOneCopy);
139 return Map.of(this.functionType.getFunctionName(), propertySourceCopy);
143 public ToscaFunctionType getType() {
144 if (functionType == null) {
147 switch (functionType) {
149 return ToscaFunctionType.GET_INPUT;
151 return ToscaFunctionType.GET_PROPERTY;
153 return ToscaFunctionType.GET_ATTRIBUTE;
161 public String getValue() {
162 return this.generatePropertyValue();