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.google.gson.Gson;
25 import java.util.ArrayList;
26 import java.util.List;
28 import java.util.stream.Collectors;
29 import java.util.stream.Stream;
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;
36 public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunctionParameter {
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<>();
46 public ToscaGetFunctionDataDefinition() {
47 //necessary for JSON conversions
50 public boolean isSubProperty() {
51 return propertyPathFromSource != null && propertyPathFromSource.size() > 1;
55 * Builds the value of a property based on the TOSCA get function information.
57 public String generatePropertyValue() {
58 return new Gson().toJson(getJsonObjectValue());
62 public Object getJsonObjectValue() {
63 if (functionType == null) {
64 throw new IllegalStateException("functionType is required in order to generate the get function value");
66 if (CollectionUtils.isEmpty(propertyPathFromSource)) {
67 throw new IllegalStateException("propertyPathFromSource is required in order to generate the get function value");
70 if (functionType == ToscaGetFunctionType.GET_PROPERTY || functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
71 return buildFunctionValueWithPropertySource();
73 if (functionType == ToscaGetFunctionType.GET_INPUT) {
74 return buildGetInputFunctionValue();
77 throw new UnsupportedOperationException(String.format("ToscaGetFunctionType '%s' is not supported yet", functionType));
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())
86 if (propertySource == PropertySource.SELF) {
87 return Map.of(functionType.getFunctionName(),
88 Stream.concat(Stream.of(PropertySource.SELF.getName()), propertyPathFromSource.stream()).collect(Collectors.toList())
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())
97 return Map.of(functionType.getFunctionName(),
98 Stream.concat(Stream.of(sourceName), propertyPathFromSource.stream()).collect(Collectors.toList())
102 throw new UnsupportedOperationException(String.format("Tosca property source '%s' not supported", propertySource));
105 private Map<String, Object> buildGetInputFunctionValue() {
106 if (this.propertyPathFromSource.size() == 1) {
107 return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource.get(0));
109 return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource);
113 public ToscaFunctionType getType() {
114 if (functionType == null) {
117 switch (functionType) {
119 return ToscaFunctionType.GET_INPUT;
121 return ToscaFunctionType.GET_PROPERTY;
123 return ToscaFunctionType.GET_ATTRIBUTE;
130 public String getValue() {
131 return this.generatePropertyValue();