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.translator.services.heattotosca.impl.resourcetranslation;
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
29 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
30 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
31 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
32 import org.openecomp.sdc.tosca.services.DataModelUtil;
33 import org.openecomp.sdc.tosca.services.ToscaUtil;
34 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
37 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
38 import org.openecomp.sdc.translator.services.heattotosca.Constants;
39 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
40 import org.openecomp.sdc.translator.services.heattotosca.TranslationService;
42 import java.util.Objects;
44 public class ResourceTranslationNestedImpl extends ResourceTranslationBase {
46 protected static Logger logger =
47 (Logger) LoggerFactory.getLogger(ResourceTranslationNestedImpl.class);
50 public void translate(TranslateTo translateTo) {
51 mdcDataDebugMessage.debugEntryMessage(null, null);
53 TranslationContext context = translateTo.getContext();
54 FileData nestedFileData =
55 HeatToToscaUtil.getFileData(translateTo.getResource().getType(), context);
56 if (nestedFileData == null) {
57 logger.warn("Nested File '" + translateTo.getResource().getType()
58 + "' is not exist, therefore, the nested resource with the ID '"
59 + translateTo.getResourceId() + "' will be ignored in TOSCA translation");
62 String templateName = FileUtils.getFileWithoutExtention(translateTo.getResource().getType());
63 String substitutionNodeTypeKey = ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat."
66 if (!context.getTranslatedServiceTemplates()
67 .containsKey(translateTo.getResource().getType())) {
69 //substitution service template
70 ServiceTemplate nestedSubstitutionServiceTemplate =
71 createSubstitutionServiceTemplate(translateTo, nestedFileData, templateName);
73 //global substitution service template
74 ServiceTemplate globalSubstitutionServiceTemplate = new HeatToToscaUtil()
75 .fetchGlobalSubstitutionServiceTemplate(translateTo.getServiceTemplate(),
78 //substitution node type
79 NodeType substitutionNodeType = new ToscaAnalyzerServiceImpl()
80 .createInitSubstitutionNodeType(nestedSubstitutionServiceTemplate,
81 ToscaNodeType.ABSTRACT_SUBSTITUTE);
82 DataModelUtil.addNodeType(globalSubstitutionServiceTemplate, substitutionNodeTypeKey,
83 substitutionNodeType);
84 //substitution mapping
86 .handleSubstitutionMapping(context, substitutionNodeTypeKey,
87 nestedSubstitutionServiceTemplate, substitutionNodeType);
89 //add new nested service template
90 context.getTranslatedServiceTemplates()
91 .put(translateTo.getResource().getType(), nestedSubstitutionServiceTemplate);
94 ServiceTemplate substitutionServiceTemplate = context.getTranslatedServiceTemplates()
95 .get(translateTo.getResource().getType());
97 if(DataModelUtil.isNodeTemplateSectionMissingFromServiceTemplate(substitutionServiceTemplate)){
98 handleSubstitutionServiceTemplateWithoutNodeTemplates(translateTo, context, templateName);
99 mdcDataDebugMessage.debugExitMessage(null, null);
102 NodeTemplate substitutionNodeTemplate =
103 HeatToToscaUtil.createAbstractSubstitutionNodeTemplate(translateTo, templateName,
104 substitutionNodeTypeKey);
105 manageSubstitutionNodeTemplateConnectionPoint(translateTo, nestedFileData,
106 substitutionNodeTemplate, substitutionNodeTypeKey);
107 DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
108 substitutionNodeTemplate);
110 //Add nested node template id to consolidation data
111 ConsolidationDataUtil.updateNestedNodeTemplateId(translateTo);
113 mdcDataDebugMessage.debugExitMessage(null, null);
116 private void handleSubstitutionServiceTemplateWithoutNodeTemplates(TranslateTo translateTo,
117 TranslationContext context,
118 String templateName) {
119 context.addServiceTemplateWithoutNodeTemplates(templateName);
120 context.getTranslatedServiceTemplates().remove(translateTo.getResource().getType());
123 private ServiceTemplate createSubstitutionServiceTemplate(TranslateTo translateTo,
124 FileData nestedFileData,
125 String templateName) {
126 ServiceTemplate nestedSubstitutionServiceTemplate =
127 HeatToToscaUtil.createInitSubstitutionServiceTemplate(templateName);
128 translateTo.getContext()
129 .addNestedHeatFileName(ToscaUtil.getServiceTemplateFileName(templateName),
130 translateTo.getResource().getType());
131 new TranslationService().translateHeatFile(nestedSubstitutionServiceTemplate, nestedFileData,
132 translateTo.getContext());
133 return nestedSubstitutionServiceTemplate;
137 private void manageSubstitutionNodeTemplateConnectionPoint(TranslateTo translateTo,
138 FileData nestedFileData,
139 NodeTemplate substitutionNodeTemplate,
140 String substitutionNodeTypeId) {
143 mdcDataDebugMessage.debugEntryMessage(null, null);
145 ServiceTemplate globalSubstitutionTemplate =
146 translateTo.getContext().getTranslatedServiceTemplates()
147 .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
148 NodeType nodeType = globalSubstitutionTemplate.getNode_types().get(substitutionNodeTypeId);
149 handlePortToNetConnections(translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
150 handleSecurityRulesToPortConnections(translateTo, nestedFileData, substitutionNodeTemplate,
152 handleNovaToVolConnection(translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
153 handleContrailV2VmInterfaceToNetworkConnection(translateTo, nestedFileData,
154 substitutionNodeTemplate, nodeType);
155 handleContrailPortToNetConnections(translateTo, nestedFileData, substitutionNodeTemplate,
157 handleVlanSubInterfaceToInterfaceConnections(translateTo, nestedFileData,
158 substitutionNodeTemplate, nodeType);
160 mdcDataDebugMessage.debugExitMessage(null, null);
163 private void handleVlanSubInterfaceToInterfaceConnections(TranslateTo translateTo,
164 FileData nestedFileData,
165 NodeTemplate substitutionNodeTemplate,
169 mdcDataDebugMessage.debugEntryMessage(null, null);
171 ContrailV2VlanToInterfaceResourceConnection linker =
172 new ContrailV2VlanToInterfaceResourceConnection(this, translateTo, nestedFileData,
173 substitutionNodeTemplate, nodeType);
176 mdcDataDebugMessage.debugExitMessage(null, null);
180 private void handleContrailV2VmInterfaceToNetworkConnection(TranslateTo translateTo,
181 FileData nestedFileData,
182 NodeTemplate substitutionNodeTemplate,
186 mdcDataDebugMessage.debugEntryMessage(null, null);
188 ContrailV2VmInterfaceToNetResourceConnection linker =
189 new ContrailV2VmInterfaceToNetResourceConnection(this, translateTo, nestedFileData,
190 substitutionNodeTemplate, nodeType);
193 mdcDataDebugMessage.debugExitMessage(null, null);
196 private void handleNovaToVolConnection(TranslateTo translateTo, FileData nestedFileData,
197 NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
200 mdcDataDebugMessage.debugEntryMessage(null, null);
202 NovaToVolResourceConnection linker =
203 new NovaToVolResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
207 mdcDataDebugMessage.debugExitMessage(null, null);
210 private void handleSecurityRulesToPortConnections(TranslateTo translateTo,
211 FileData nestedFileData,
212 NodeTemplate substitutionNodeTemplate,
216 mdcDataDebugMessage.debugEntryMessage(null, null);
218 SecurityRulesToPortResourceConnection linker =
219 new SecurityRulesToPortResourceConnection(this, translateTo, nestedFileData,
220 substitutionNodeTemplate, nodeType);
223 mdcDataDebugMessage.debugExitMessage(null, null);
226 private void handlePortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
227 NodeTemplate substitutionNodeTemplate,
231 mdcDataDebugMessage.debugEntryMessage(null, null);
233 PortToNetResourceConnection linker =
234 new PortToNetResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
238 mdcDataDebugMessage.debugExitMessage(null, null);
241 private void handleContrailPortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
242 NodeTemplate substitutionNodeTemplate,
246 mdcDataDebugMessage.debugEntryMessage(null, null);
248 ContrailPortToNetResourceConnection linker =
249 new ContrailPortToNetResourceConnection(this, translateTo, nestedFileData,
250 substitutionNodeTemplate, nodeType);
253 mdcDataDebugMessage.debugExitMessage(null, null);