push addional code
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / services / ToscaUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.tosca.services;
22
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;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Objects;
34 import java.util.Set;
35 import java.util.UUID;
36
37 /**
38  * The type Tosca util.
39  */
40 public class ToscaUtil {
41
42   /**
43    * Gets service template file name.
44    *
45    * @param serviceTemplate the service template
46    * @return the service template file name
47    */
48   public static String getServiceTemplateFileName(ServiceTemplate serviceTemplate) {
49     if (serviceTemplate == null) {
50       return null;
51     }
52     if (serviceTemplate.getMetadata() == null) {
53       return UUID.randomUUID().toString() + "ServiceTemplate.yaml";
54     }
55     return getServiceTemplateFileName(serviceTemplate.getMetadata().getTemplate_name());
56   }
57
58   /**
59    * Gets service template file name.
60    *
61    * @param templateName the template name
62    * @return the service template file name
63    */
64   public static String getServiceTemplateFileName(String templateName) {
65     return (Objects.isNull(templateName) ? UUID.randomUUID().toString() : templateName)
66         + "ServiceTemplate.yaml";
67   }
68
69   /**
70    * Add service template to map with key file name.
71    *
72    * @param serviceTemplates      the service templates
73    * @param commonServiceTemplate the common service template
74    */
75   public static void addServiceTemplateToMapWithKeyFileName(
76       Map<String, ServiceTemplate> serviceTemplates, ServiceTemplate commonServiceTemplate) {
77     serviceTemplates
78         .put(ToscaUtil.getServiceTemplateFileName(commonServiceTemplate), commonServiceTemplate);
79   }
80
81   /**
82    * Convert type to definition capability definition.
83    *
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
89    */
90   public static CapabilityDefinition convertTypeToDefinition(String type,
91                                                              CapabilityType capabilityType,
92                                                              Map<String, Object> properties,
93                                                              String description) {
94     CapabilityDefinition capabilityDefinition = new CapabilityDefinition();
95     capabilityDefinition.setAttributes(capabilityType.getAttributes());
96     capabilityDefinition.setProperties(capabilityType.getProperties());
97     if (description == null) {
98       capabilityDefinition.setDescription(capabilityType.getDescription());
99     } else {
100       capabilityDefinition.setDescription(description);
101     }
102     capabilityDefinition.setType(type);
103
104     capabilityDefinition.getProperties()
105         .entrySet()
106         .stream()
107         .filter(entry -> properties.containsKey(entry.getKey()))
108         .forEach(entry -> entry.getValue()
109             .set_default(properties.get(entry.getKey())));
110
111
112     return capabilityDefinition;
113
114   }
115
116   /**
117    * Normalize component name node type map.
118    *
119    * @param toscaModel the tosca model
120    * @param components the components
121    * @return the map
122    */
123   public static Map<String, List<NodeType>> normalizeComponentNameNodeType(
124       ToscaServiceModel toscaModel, Set<String> components) {
125
126     Map<String, List<NodeType>> normalizedData = new HashMap<>();
127     toscaModel
128         .getServiceTemplates()
129         .entrySet().stream().filter(entry -> entry
130         .getValue()
131         .getNode_types() != null)
132         .forEach(entry -> entry
133             .getValue()
134             .getNode_types()
135             .entrySet().stream()
136             .filter(nodeTypeEntry -> components
137                 .contains(nodeTypeEntry
138                     .getKey()))
139             .forEach(nodeTypeEntry -> addNodeType(nodeTypeEntry.getKey(), nodeTypeEntry.getValue(),
140                 normalizedData)));
141     return normalizedData;
142   }
143
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<>());
148     }
149     normalizedData.get(key).add(value);
150   }
151 }