2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.generator.core.utils;
23 import org.onap.sdc.tosca.datatypes.model.*;
24 import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
25 import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
26 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
27 import org.openecomp.sdc.tosca.services.DataModelUtil;
28 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
29 import org.openecomp.sdc.tosca.services.ToscaUtil;
30 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
34 import static org.openecomp.sdc.tosca.services.DataModelUtil.addSubstitutionNodeTypeRequirements;
37 * The type Generator utils.
39 public class GeneratorUtils {
41 private GeneratorUtils() {
42 // prevent instantiation
45 //TODO : Read from configuration
46 private static final List<String> SUPPORTED_CAPABILITIES = Arrays.asList("host", "os", "endpoint", "scalable");
47 private static final List<String> SUPPORTED_REQUIREMENTS = Collections.singletonList("link");
50 * Add service template to tosca service model.
52 * @param toscaServiceModel the tosca service model
53 * @param serviceTemplate the service template
55 public static void addServiceTemplateToToscaServiceModel(ToscaServiceModel toscaServiceModel,
56 ServiceTemplate serviceTemplate) {
58 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
59 Map<String, ServiceTemplate> serviceTemplates = toscaServiceModel.getServiceTemplates();
60 if (!serviceTemplates.containsKey(serviceTemplateFileName)) {
61 ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplates, serviceTemplate);
63 toscaServiceModel.setServiceTemplates(serviceTemplates);
67 * Gets substitution node type exposed connection points.
69 * @param substitutionNodeType the substitution node type
70 * @param substitutionServiceTemplate the substitution service template
71 * @param toscaServiceModel the tosca service model
72 * @return the substitution node type exposed connection points
74 public static Map<String, Map<String, List<String>>> getSubstitutionNodeTypeExposedConnectionPoints(NodeType substitutionNodeType,
75 ServiceTemplate substitutionServiceTemplate,
76 ToscaServiceModel toscaServiceModel) {
78 Map<String, NodeTemplate> nodeTemplates =
79 substitutionServiceTemplate.getTopology_template().getNode_templates();
80 String nodeTemplateId;
81 NodeTemplate nodeTemplate;
83 Map<String, Map<String, List<String>>> substitutionMapping = new HashMap<>();
84 if (nodeTemplates == null) {
85 return substitutionMapping;
89 Map<String, List<String>> capabilitySubstitutionMapping = new HashMap<>();
90 Map<String, List<String>> requirementSubstitutionMapping = new HashMap<>();
91 substitutionMapping.put("capability", capabilitySubstitutionMapping);
92 substitutionMapping.put("requirement", requirementSubstitutionMapping);
93 List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition;
94 Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment;
95 List<Map<String, RequirementDefinition>> exposedRequirementsDefinition;
96 Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition = new HashMap<>();
97 Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>();
98 Map<String, CapabilityDefinition> exposedCapabilitiesDefinition;
100 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
101 for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
102 nodeTemplateId = entry.getKey();
103 nodeTemplate = entry.getValue();
104 nodeType = nodeTemplate.getType();
105 NodeType flatNodeType = (NodeType) toscaAnalyzerService
106 .getFlatEntity(ToscaElementTypes.NODE_TYPE, nodeType,
107 substitutionServiceTemplate, toscaServiceModel)
110 nodeTypeRequirementsDefinition =
111 getNodeTypeRequirements(flatNodeType, nodeTemplateId, substitutionServiceTemplate,
112 requirementSubstitutionMapping);
113 nodeTemplateRequirementsAssignment = DataModelUtil.getNodeTemplateRequirements(nodeTemplate);
114 fullFilledRequirementsDefinition.put(nodeTemplateId, nodeTemplateRequirementsAssignment);
115 //set substitution node type requirements
116 exposedRequirementsDefinition = toscaAnalyzerService
117 .calculateExposedRequirements(nodeTypeRequirementsDefinition,
118 nodeTemplateRequirementsAssignment);
121 //Filter unsupported requirements
122 Iterator<Map<String, RequirementDefinition>> iterator = exposedRequirementsDefinition.iterator();
123 while (iterator.hasNext()) {
124 Map<String, RequirementDefinition> requirementDefinitionMap = iterator.next();
125 for (Map.Entry<String, RequirementDefinition> requirementDefinitionEntry : requirementDefinitionMap
127 String requirementKey = requirementDefinitionEntry.getKey();
128 if (!SUPPORTED_REQUIREMENTS.contains(requirementKey)) {
133 addSubstitutionNodeTypeRequirements(substitutionNodeType, exposedRequirementsDefinition,
136 addNodeTypeCapabilitiesToSubMapping(nodeTypeCapabilitiesDefinition, capabilitySubstitutionMapping,
137 nodeType, nodeTemplateId, substitutionServiceTemplate, toscaServiceModel);
140 exposedCapabilitiesDefinition = toscaAnalyzerService
141 .calculateExposedCapabilities(nodeTypeCapabilitiesDefinition,
142 fullFilledRequirementsDefinition);
144 //Filter unsupported capabilities
145 Iterator<Map.Entry<String, CapabilityDefinition>> iterator =
146 exposedCapabilitiesDefinition.entrySet().iterator();
147 while (iterator.hasNext()) {
148 Map.Entry<String, CapabilityDefinition> capabilityDefinitionEntry = iterator.next();
149 //Expected Capability is of the format <capabilityId>_<componentName>
150 String capabilityKey = capabilityDefinitionEntry.getKey().split("_")[0];
151 if (!SUPPORTED_CAPABILITIES.contains(capabilityKey)) {
156 DataModelUtil.setNodeTypeCapabilitiesDef(substitutionNodeType, exposedCapabilitiesDefinition);
157 } catch (Exception ex) {
160 return substitutionMapping;
164 * Gets node type requirements.
166 * @param flatNodeType the flat node type
167 * @param templateName the template name
168 * @param serviceTemplate the service template
169 * @param requirementSubstitutionMapping the requirement substitution mapping
170 * @return the node type requirements
172 public static List<Map<String, RequirementDefinition>> getNodeTypeRequirements(NodeType flatNodeType,
174 ServiceTemplate serviceTemplate,
175 Map<String, List<String>> requirementSubstitutionMapping) {
176 List<Map<String, RequirementDefinition>> requirementList = new ArrayList<>();
177 List<String> requirementMapping;
178 if (flatNodeType.getRequirements() != null) {
179 for (Map<String, RequirementDefinition> requirementMap : flatNodeType.getRequirements()) {
180 for (Map.Entry<String, RequirementDefinition> requirementNodeEntry : requirementMap.entrySet()) {
181 ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
182 RequirementDefinition requirementNodeEntryValue = toscaExtensionYamlUtil.yamlToObject(
183 toscaExtensionYamlUtil.objectToYaml(requirementNodeEntry.getValue()),
184 RequirementDefinition.class);
185 if (requirementNodeEntryValue.getOccurrences() == null) {
186 requirementNodeEntryValue.setOccurrences(new Object[] {1, 1});
188 Map<String, RequirementDefinition> requirementDef = new HashMap<>();
189 requirementDef.put(requirementNodeEntry.getKey(), requirementNodeEntryValue);
190 DataModelUtil.addRequirementToList(requirementList, requirementDef);
191 requirementMapping = new ArrayList<>();
192 requirementMapping.add(templateName);
193 requirementMapping.add(requirementNodeEntry.getKey());
194 requirementSubstitutionMapping
195 .put(requirementNodeEntry.getKey() + "_" + templateName, requirementMapping);
196 if (requirementNodeEntryValue.getNode() == null) {
197 requirementNodeEntryValue.setOccurrences(new Object[] {1, 1});
202 return requirementList;
205 private static void addNodeTypeCapabilitiesToSubMapping(Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
206 Map<String, List<String>> capabilitySubstitutionMapping,
207 String type, String templateName,
208 ServiceTemplate substitutionServiceTemplate,
209 ToscaServiceModel toscaServiceModel) {
210 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
211 NodeType flatNodeType = (NodeType) toscaAnalyzerService.getFlatEntity(ToscaElementTypes.NODE_TYPE, type,
212 substitutionServiceTemplate, toscaServiceModel).getFlatEntity();
213 String capabilityKey;
214 List<String> capabilityMapping;
215 if (flatNodeType.getCapabilities() != null) {
216 for (Map.Entry<String, CapabilityDefinition> capabilityNodeEntry : flatNodeType.getCapabilities()
218 capabilityKey = capabilityNodeEntry.getKey() + "_" + templateName;
219 nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityNodeEntry.getValue().clone());
220 capabilityMapping = new ArrayList<>();
221 capabilityMapping.add(templateName);
222 capabilityMapping.add(capabilityNodeEntry.getKey());
223 capabilitySubstitutionMapping.put(capabilityKey, capabilityMapping);