2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.tosca.services;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.Objects;
25 import java.util.stream.Collectors;
26 import org.onap.sdc.tosca.datatypes.model.AttributeDefinition;
27 import org.onap.sdc.tosca.datatypes.model.Constraint;
28 import org.onap.sdc.tosca.datatypes.model.OperationDefinition;
29 import org.onap.sdc.tosca.datatypes.model.PropertyDefinition;
30 import org.onap.sdc.tosca.error.ToscaRuntimeException;
32 public class DataModelCloneUtil {
34 private DataModelCloneUtil() {
35 throw new IllegalStateException("Utility class");
39 * Clone constraints list.
41 * @param constraints the constraints
44 public static List<Constraint> cloneConstraints(List<Constraint> constraints) {
46 if (constraints == null) {
49 return constraints.stream().map(Constraint::clone).collect(Collectors.toList());
53 * Clone property definitions map.
55 * @param propertyDefinitions the property definitions
58 public static Map<String, PropertyDefinition> clonePropertyDefinitions(
59 Map<String, PropertyDefinition> propertyDefinitions) {
60 if (propertyDefinitions == null) {
63 Map<String, PropertyDefinition> clonedProperties = new HashMap<>();
64 for (Map.Entry<String, PropertyDefinition> propertyDefinitionEntry : propertyDefinitions.entrySet()) {
65 clonedProperties.put(propertyDefinitionEntry.getKey(), propertyDefinitionEntry.getValue().clone());
68 return clonedProperties;
72 * Clone attribute definitions map.
74 * @param attributeDefinitions the attribute definitions
77 public static Map<String, AttributeDefinition> cloneAttributeDefinitions(
78 Map<String, AttributeDefinition> attributeDefinitions) {
80 if (attributeDefinitions == null) {
83 Map<String, AttributeDefinition> clonedAttributeDefinitions = new HashMap<>();
84 for (Map.Entry<String, AttributeDefinition> attributeDefinitionEntry : attributeDefinitions.entrySet()) {
85 clonedAttributeDefinitions
86 .put(attributeDefinitionEntry.getKey(), attributeDefinitionEntry.getValue().clone());
89 return clonedAttributeDefinitions;
93 * Clone Map of key String and value String .
95 * @param stringStringMap the map that will be cloned
96 * @return the cloned map
98 public static Map<String, String> cloneStringStringMap(Map<String, String> stringStringMap) {
99 if (Objects.isNull(stringStringMap)) {
102 return new HashMap<>(stringStringMap);
106 * Clone Map of key String and value PropertyDefinition .
108 * @param stringPropertyDefinitionMap the map that will be cloned
109 * @return the cloned map
111 public static Map<String, PropertyDefinition> cloneStringPropertyDefinitionMap(
112 Map<String, PropertyDefinition> stringPropertyDefinitionMap) {
113 if (Objects.isNull(stringPropertyDefinitionMap)) {
117 Map<String, PropertyDefinition> cloneMap = new HashMap<>();
118 ToscaExtensionYamlUtil toscaExtYamlUtil = new ToscaExtensionYamlUtil();
119 for (Map.Entry<String, PropertyDefinition> mapEntry : stringPropertyDefinitionMap.entrySet()) {
120 PropertyDefinition propertyDefinition = toscaExtYamlUtil.yamlToObject(
121 toscaExtYamlUtil.objectToYaml(mapEntry.getValue()), PropertyDefinition.class);
122 cloneMap.put(mapEntry.getKey(), propertyDefinition.clone());
128 * Clone Map of key String and value OperationDefinition or class which is extends from OperationDefinition .
130 * @param input the map that will be cloned
131 * @return the cloned map
133 public static <T extends OperationDefinition> Map<String, T> cloneStringOperationDefinitionMap(
134 Map<String, T> input) {
136 if (Objects.isNull(input)) {
140 Map<String, OperationDefinition> cloneMap = new HashMap<>();
141 for (Map.Entry<String, T> mapEntry : input.entrySet()) {
142 cloneMap.put(mapEntry.getKey(), Objects.isNull(mapEntry.getValue()) ? null : mapEntry.getValue().clone());
145 return (Map<String, T>) cloneMap;
149 * Clone Map of key String and value Object .
151 * @param stringObjectMap the map that will be cloned
152 * @return the cloned map
154 public static Map<String, Object> cloneStringObjectMap(Map<String, Object> stringObjectMap) {
155 if (Objects.isNull(stringObjectMap)) {
159 Map<String, Object> cloneMap = new HashMap<>();
160 for (Map.Entry<String, Object> mapEntry : stringObjectMap.entrySet()) {
161 YamlUtil yamlUtil = new YamlUtil();
162 if (mapEntry.getValue() instanceof Map) {
163 Map cloneObj = yamlUtil.yamlToObject(yamlUtil.objectToYaml(mapEntry.getValue()), Map.class);
164 cloneMap.put(mapEntry.getKey(), cloneObj);
165 } else if (mapEntry.getValue() instanceof List) {
166 List cloneObj = yamlUtil.yamlToObject(yamlUtil.objectToYaml(mapEntry.getValue()), List.class);
167 cloneMap.put(mapEntry.getKey(), cloneObj);
168 } else if (mapEntry.getValue() instanceof Cloneable) {
169 throw new ToscaRuntimeException("Clone Not Supported Exception");
171 cloneMap.put(mapEntry.getKey(), mapEntry.getValue());
178 * Clone List of String.
180 * @param listString the list that will be cloned
181 * @return the cloned list
183 public static List<String> cloneListString(List<String> listString) {
184 if (Objects.isNull(listString)) {
187 return new ArrayList<>(listString);