7ba33c47e42d83f7f3542fc27c00632b6b4a352e
[sdc.git] /
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.translator.services.heattotosca.impl.resourcetranslation;
22
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;
40
41 public class ResourceTranslationNestedImpl extends ResourceTranslationBase {
42
43   protected static Logger logger =
44       (Logger) LoggerFactory.getLogger(ResourceTranslationNestedImpl.class);
45
46   @Override
47   public void translate(TranslateTo translateTo) {
48     mdcDataDebugMessage.debugEntryMessage(null, null);
49
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");
57       return;
58     }
59     String templateName = FileUtils.getFileWithoutExtention(translateTo.getResource().getType());
60     String substitutionNodeTypeKey = ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat."
61         + templateName;
62
63     if (!context.getTranslatedServiceTemplates()
64         .containsKey(translateTo.getResource().getType())) {
65
66       //substitution service template
67       ServiceTemplate nestedSubstitutionServiceTemplate =
68           createSubstitutionServiceTemplate(translateTo, nestedFileData, templateName);
69
70       //global substitution service template
71       ServiceTemplate globalSubstitutionServiceTemplate = new HeatToToscaUtil()
72           .fetchGlobalSubstitutionServiceTemplate(translateTo.getServiceTemplate(),
73               context);
74
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
82       HeatToToscaUtil
83           .handleSubstitutionMapping(context, substitutionNodeTypeKey,
84               nestedSubstitutionServiceTemplate, substitutionNodeType);
85
86       //add new nested service template
87       context.getTranslatedServiceTemplates()
88           .put(translateTo.getResource().getType(), nestedSubstitutionServiceTemplate);
89     }
90
91     ServiceTemplate substitutionServiceTemplate = context.getTranslatedServiceTemplates()
92         .get(translateTo.getResource().getType());
93
94     if(DataModelUtil.isNodeTemplateSectionMissingFromServiceTemplate(substitutionServiceTemplate)){
95       handleSubstitutionServiceTemplateWithoutNodeTemplates(
96           templateName, translateTo);
97       mdcDataDebugMessage.debugExitMessage(null, null);
98       return;
99     }
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);
107
108     //Add nested node template id to consolidation data
109     ConsolidationDataUtil.updateNestedNodeTemplateId(translateTo);
110
111     mdcDataDebugMessage.debugExitMessage(null, null);
112   }
113
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());
120   }
121
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;
133   }
134
135
136   private void manageSubstitutionNodeTemplateConnectionPoint(TranslateTo translateTo,
137                                                              FileData nestedFileData,
138                                                              NodeTemplate substitutionNodeTemplate,
139                                                              String substitutionNodeTypeId) {
140
141
142     mdcDataDebugMessage.debugEntryMessage(null, null);
143
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,
150         nodeType);
151     handleNovaToVolConnection(translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
152     handleContrailV2VmInterfaceToNetworkConnection(translateTo, nestedFileData,
153         substitutionNodeTemplate, nodeType);
154     handleContrailPortToNetConnections(translateTo, nestedFileData, substitutionNodeTemplate,
155         nodeType);
156     handleVlanSubInterfaceToInterfaceConnections(translateTo, nestedFileData,
157         substitutionNodeTemplate, nodeType);
158
159     mdcDataDebugMessage.debugExitMessage(null, null);
160   }
161
162   private void handleVlanSubInterfaceToInterfaceConnections(TranslateTo translateTo,
163                                                             FileData nestedFileData,
164                                                             NodeTemplate substitutionNodeTemplate,
165                                                             NodeType nodeType) {
166
167
168     mdcDataDebugMessage.debugEntryMessage(null, null);
169
170     ContrailV2VlanToInterfaceResourceConnection linker =
171         new ContrailV2VlanToInterfaceResourceConnection(this, translateTo, nestedFileData,
172             substitutionNodeTemplate, nodeType);
173     linker.connect();
174
175     mdcDataDebugMessage.debugExitMessage(null, null);
176   }
177
178
179   private void handleContrailV2VmInterfaceToNetworkConnection(TranslateTo translateTo,
180                                                               FileData nestedFileData,
181                                                               NodeTemplate substitutionNodeTemplate,
182                                                               NodeType nodeType) {
183
184
185     mdcDataDebugMessage.debugEntryMessage(null, null);
186
187     ContrailV2VmInterfaceToNetResourceConnection linker =
188         new ContrailV2VmInterfaceToNetResourceConnection(this, translateTo, nestedFileData,
189             substitutionNodeTemplate, nodeType);
190     linker.connect();
191
192     mdcDataDebugMessage.debugExitMessage(null, null);
193   }
194
195   private void handleNovaToVolConnection(TranslateTo translateTo, FileData nestedFileData,
196                                          NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
197
198
199     mdcDataDebugMessage.debugEntryMessage(null, null);
200
201     NovaToVolResourceConnection linker =
202         new NovaToVolResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
203             nodeType);
204     linker.connect();
205
206     mdcDataDebugMessage.debugExitMessage(null, null);
207   }
208
209   private void handleSecurityRulesToPortConnections(TranslateTo translateTo,
210                                                     FileData nestedFileData,
211                                                     NodeTemplate substitutionNodeTemplate,
212                                                     NodeType nodeType) {
213
214
215     mdcDataDebugMessage.debugEntryMessage(null, null);
216
217     SecurityRulesToPortResourceConnection linker =
218         new SecurityRulesToPortResourceConnection(this, translateTo, nestedFileData,
219             substitutionNodeTemplate, nodeType);
220     linker.connect();
221
222     mdcDataDebugMessage.debugExitMessage(null, null);
223   }
224
225   private void handlePortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
226                                           NodeTemplate substitutionNodeTemplate,
227                                           NodeType nodeType) {
228
229
230     mdcDataDebugMessage.debugEntryMessage(null, null);
231
232     PortToNetResourceConnection linker =
233         new PortToNetResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
234             nodeType);
235     linker.connect();
236
237     mdcDataDebugMessage.debugExitMessage(null, null);
238   }
239
240   private void handleContrailPortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
241                                                   NodeTemplate substitutionNodeTemplate,
242                                                   NodeType nodeType) {
243
244
245     mdcDataDebugMessage.debugEntryMessage(null, null);
246
247     ContrailPortToNetResourceConnection linker =
248         new ContrailPortToNetResourceConnection(this, translateTo, nestedFileData,
249             substitutionNodeTemplate, nodeType);
250     linker.connect();
251
252     mdcDataDebugMessage.debugExitMessage(null, null);
253   }
254
255 }