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 {
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 if (functionType == null) {
59 throw new IllegalStateException("functionType is required in order to generate the get function value");
61 if (CollectionUtils.isEmpty(propertyPathFromSource)) {
62 throw new IllegalStateException("propertyPathFromSource is required in order to generate the get function value");
65 final var gson = new Gson();
66 if (functionType == ToscaGetFunctionType.GET_PROPERTY) {
67 return gson.toJson(buildGetPropertyFunctionValue());
69 if (functionType == ToscaGetFunctionType.GET_INPUT) {
70 return gson.toJson(buildGetInputFunctionValue());
73 throw new UnsupportedOperationException(String.format("ToscaGetFunctionType '%s' is not supported yet", functionType));
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");
80 if (propertySource == PropertySource.SELF) {
81 return Map.of(functionType.getFunctionName(),
82 Stream.concat(Stream.of(PropertySource.SELF.getName()), propertyPathFromSource.stream()).collect(Collectors.toList())
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");
89 return Map.of(functionType.getFunctionName(),
90 Stream.concat(Stream.of(sourceName), propertyPathFromSource.stream()).collect(Collectors.toList())
94 throw new UnsupportedOperationException(String.format("Tosca property source '%s' not supported", propertySource));
97 private Map<String, Object> buildGetInputFunctionValue() {
98 if (this.propertyPathFromSource.size() == 1) {
99 return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource.get(0));
101 return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource);