2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.tosca.services;
23 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
24 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
25 import org.openecomp.sdc.tosca.datatypes.model.CapabilityType;
26 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import java.util.Objects;
35 import java.util.UUID;
38 * The type Tosca util.
40 public class ToscaUtil {
43 * Gets service template file name.
45 * @param serviceTemplate the service template
46 * @return the service template file name
48 public static String getServiceTemplateFileName(ServiceTemplate serviceTemplate) {
49 if (serviceTemplate == null) {
52 if (serviceTemplate.getMetadata() == null) {
53 return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
55 return getServiceTemplateFileName(serviceTemplate.getMetadata().getTemplate_name());
59 * Gets service template file name.
61 * @param templateName the template name
62 * @return the service template file name
64 public static String getServiceTemplateFileName(String templateName) {
65 return (Objects.isNull(templateName) ? UUID.randomUUID().toString() : templateName)
66 + "ServiceTemplate.yaml";
70 * Add service template to map with key file name.
72 * @param serviceTemplates the service templates
73 * @param commonServiceTemplate the common service template
75 public static void addServiceTemplateToMapWithKeyFileName(
76 Map<String, ServiceTemplate> serviceTemplates, ServiceTemplate commonServiceTemplate) {
78 .put(ToscaUtil.getServiceTemplateFileName(commonServiceTemplate), commonServiceTemplate);
82 * Convert type to definition capability definition.
84 * @param type the type
85 * @param capabilityType the capability type
86 * @param properties the properties
87 * @param description the description
88 * @return the capability definition
90 public static CapabilityDefinition convertTypeToDefinition(String type,
91 CapabilityType capabilityType,
92 Map<String, Object> properties,
94 CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
95 capabilityDefinition.setAttributes(capabilityType.getAttributes());
96 capabilityDefinition.setProperties(capabilityType.getProperties());
97 if (description == null) {
98 capabilityDefinition.setDescription(capabilityType.getDescription());
100 capabilityDefinition.setDescription(description);
102 capabilityDefinition.setType(type);
104 capabilityDefinition.getProperties()
107 .filter(entry -> properties.containsKey(entry.getKey()))
108 .forEach(entry -> entry.getValue()
109 .set_default(properties.get(entry.getKey())));
112 return capabilityDefinition;
117 * Normalize component name node type map.
119 * @param toscaModel the tosca model
120 * @param components the components
123 public static Map<String, List<NodeType>> normalizeComponentNameNodeType(
124 ToscaServiceModel toscaModel, Set<String> components) {
126 Map<String, List<NodeType>> normalizedData = new HashMap<>();
128 .getServiceTemplates()
129 .entrySet().stream().filter(entry -> entry
131 .getNode_types() != null)
132 .forEach(entry -> entry
136 .filter(nodeTypeEntry -> components
137 .contains(nodeTypeEntry
139 .forEach(nodeTypeEntry -> addNodeType(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(),
141 return normalizedData;
144 private static void addNodeType(String key, NodeType value,
145 Map<String, List<NodeType>> normalizedData) {
146 if (!normalizedData.containsKey(key)) {
147 normalizedData.put(key, new ArrayList<>());
149 normalizedData.get(key).add(value);