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.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
28 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
29 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
30 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
31 import org.openecomp.sdc.tosca.services.DataModelUtil;
32 import org.openecomp.sdc.tosca.services.ToscaUtil;
33 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
36 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
37 import org.openecomp.sdc.translator.services.heattotosca.Constants;
38 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 import org.openecomp.sdc.translator.services.heattotosca.TranslationService;
41 public class ResourceTranslationNestedImpl extends ResourceTranslationBase {
43 protected static Logger logger =
44 (Logger) LoggerFactory.getLogger(ResourceTranslationNestedImpl.class);
47 public void translate(TranslateTo translateTo) {
48 mdcDataDebugMessage.debugEntryMessage(null, null);
50 TranslationContext context = translateTo.getContext();
51 FileData nestedFileData =
52 HeatToToscaUtil.getFileData(translateTo.getResource().getType(), context);
53 if (nestedFileData == null) {
54 logger.warn("Nested File '" + translateTo.getResource().getType()
55 + "' is not exist, therefore, the nested resource with the ID '"
56 + translateTo.getResourceId() + "' will be ignored in TOSCA translation");
59 String templateName = FileUtils.getFileWithoutExtention(translateTo.getResource().getType());
60 String substitutionNodeTypeKey = ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat."
63 if (!context.getTranslatedServiceTemplates()
64 .containsKey(translateTo.getResource().getType())) {
66 //substitution service template
67 ServiceTemplate nestedSubstitutionServiceTemplate =
68 createSubstitutionServiceTemplate(translateTo, nestedFileData, templateName);
70 //global substitution service template
71 ServiceTemplate globalSubstitutionServiceTemplate = new HeatToToscaUtil()
72 .fetchGlobalSubstitutionServiceTemplate(translateTo.getServiceTemplate(),
75 //substitution node type
76 NodeType substitutionNodeType = new ToscaAnalyzerServiceImpl()
77 .createInitSubstitutionNodeType(nestedSubstitutionServiceTemplate,
78 ToscaNodeType.ABSTRACT_SUBSTITUTE);
79 DataModelUtil.addNodeType(globalSubstitutionServiceTemplate, substitutionNodeTypeKey,
80 substitutionNodeType);
81 //substitution mapping
83 .handleSubstitutionMapping(context, substitutionNodeTypeKey,
84 nestedSubstitutionServiceTemplate, substitutionNodeType);
86 //add new nested service template
87 context.getTranslatedServiceTemplates()
88 .put(translateTo.getResource().getType(), nestedSubstitutionServiceTemplate);
91 ServiceTemplate substitutionServiceTemplate = context.getTranslatedServiceTemplates()
92 .get(translateTo.getResource().getType());
94 if(DataModelUtil.isNodeTemplateSectionMissingFromServiceTemplate(substitutionServiceTemplate)){
95 handleSubstitutionServiceTemplateWithoutNodeTemplates(
96 templateName, translateTo);
97 mdcDataDebugMessage.debugExitMessage(null, null);
100 NodeTemplate substitutionNodeTemplate =
101 HeatToToscaUtil.createAbstractSubstitutionNodeTemplate(translateTo, templateName,
102 substitutionNodeTypeKey);
103 manageSubstitutionNodeTemplateConnectionPoint(translateTo, nestedFileData,
104 substitutionNodeTemplate, substitutionNodeTypeKey);
105 DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
106 substitutionNodeTemplate);
108 //Add nested node template id to consolidation data
109 ConsolidationDataUtil.updateNestedNodeTemplateId(translateTo);
111 mdcDataDebugMessage.debugExitMessage(null, null);
114 private void handleSubstitutionServiceTemplateWithoutNodeTemplates(String templateName,
115 TranslateTo translateTo) {
116 translateTo.getContext().addServiceTemplateWithoutNodeTemplates(templateName);
117 translateTo.getContext()
118 .addNestedNodeTemplateIdPointsToStWithoutNodeTemplates(translateTo.getTranslatedId());
119 translateTo.getContext().getTranslatedServiceTemplates().remove(translateTo.getResource().getType());
122 private ServiceTemplate createSubstitutionServiceTemplate(TranslateTo translateTo,
123 FileData nestedFileData,
124 String templateName) {
125 ServiceTemplate nestedSubstitutionServiceTemplate =
126 HeatToToscaUtil.createInitSubstitutionServiceTemplate(templateName);
127 translateTo.getContext()
128 .addNestedHeatFileName(ToscaUtil.getServiceTemplateFileName(templateName),
129 translateTo.getResource().getType());
130 new TranslationService().translateHeatFile(nestedSubstitutionServiceTemplate, nestedFileData,
131 translateTo.getContext());
132 return nestedSubstitutionServiceTemplate;
136 private void manageSubstitutionNodeTemplateConnectionPoint(TranslateTo translateTo,
137 FileData nestedFileData,
138 NodeTemplate substitutionNodeTemplate,
139 String substitutionNodeTypeId) {
142 mdcDataDebugMessage.debugEntryMessage(null, null);
144 ServiceTemplate globalSubstitutionTemplate =
145 translateTo.getContext().getTranslatedServiceTemplates()
146 .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
147 NodeType nodeType = globalSubstitutionTemplate.getNode_types().get(substitutionNodeTypeId);
148 handlePortToNetConnections(translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
149 handleSecurityRulesToPortConnections(translateTo, nestedFileData, substitutionNodeTemplate,
151 handleNovaToVolConnection(translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
152 handleContrailV2VmInterfaceToNetworkConnection(translateTo, nestedFileData,
153 substitutionNodeTemplate, nodeType);
154 handleContrailPortToNetConnections(translateTo, nestedFileData, substitutionNodeTemplate,
156 handleVlanSubInterfaceToInterfaceConnections(translateTo, nestedFileData,
157 substitutionNodeTemplate, nodeType);
159 mdcDataDebugMessage.debugExitMessage(null, null);
162 private void handleVlanSubInterfaceToInterfaceConnections(TranslateTo translateTo,
163 FileData nestedFileData,
164 NodeTemplate substitutionNodeTemplate,
168 mdcDataDebugMessage.debugEntryMessage(null, null);
170 ContrailV2VlanToInterfaceResourceConnection linker =
171 new ContrailV2VlanToInterfaceResourceConnection(this, translateTo, nestedFileData,
172 substitutionNodeTemplate, nodeType);
175 mdcDataDebugMessage.debugExitMessage(null, null);
179 private void handleContrailV2VmInterfaceToNetworkConnection(TranslateTo translateTo,
180 FileData nestedFileData,
181 NodeTemplate substitutionNodeTemplate,
185 mdcDataDebugMessage.debugEntryMessage(null, null);
187 ContrailV2VmInterfaceToNetResourceConnection linker =
188 new ContrailV2VmInterfaceToNetResourceConnection(this, translateTo, nestedFileData,
189 substitutionNodeTemplate, nodeType);
192 mdcDataDebugMessage.debugExitMessage(null, null);
195 private void handleNovaToVolConnection(TranslateTo translateTo, FileData nestedFileData,
196 NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
199 mdcDataDebugMessage.debugEntryMessage(null, null);
201 NovaToVolResourceConnection linker =
202 new NovaToVolResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
206 mdcDataDebugMessage.debugExitMessage(null, null);
209 private void handleSecurityRulesToPortConnections(TranslateTo translateTo,
210 FileData nestedFileData,
211 NodeTemplate substitutionNodeTemplate,
215 mdcDataDebugMessage.debugEntryMessage(null, null);
217 SecurityRulesToPortResourceConnection linker =
218 new SecurityRulesToPortResourceConnection(this, translateTo, nestedFileData,
219 substitutionNodeTemplate, nodeType);
222 mdcDataDebugMessage.debugExitMessage(null, null);
225 private void handlePortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
226 NodeTemplate substitutionNodeTemplate,
230 mdcDataDebugMessage.debugEntryMessage(null, null);
232 PortToNetResourceConnection linker =
233 new PortToNetResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
237 mdcDataDebugMessage.debugExitMessage(null, null);
240 private void handleContrailPortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
241 NodeTemplate substitutionNodeTemplate,
245 mdcDataDebugMessage.debugEntryMessage(null, null);
247 ContrailPortToNetResourceConnection linker =
248 new ContrailPortToNetResourceConnection(this, translateTo, nestedFileData,
249 substitutionNodeTemplate, nodeType);
252 mdcDataDebugMessage.debugExitMessage(null, null);