aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/resourcetranslation/ResourceTranslationNestedImpl.java
blob: 843d9fabc61c479048c3ae7a736a76dd6c7db9cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;

import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.sdc.heat.datatypes.manifest.FileData;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
import org.openecomp.sdc.tosca.datatypes.model.NodeType;
import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.sdc.tosca.services.DataModelUtil;
import org.openecomp.sdc.tosca.services.ToscaUtil;
import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
import org.openecomp.sdc.translator.services.heattotosca.Constants;
import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
import org.openecomp.sdc.translator.services.heattotosca.TranslationService;

public class ResourceTranslationNestedImpl extends ResourceTranslationBase {

  protected static Logger logger =
      (Logger) LoggerFactory.getLogger(ResourceTranslationNestedImpl.class);

  @Override
  public void translate(TranslateTo translateTo) {
    mdcDataDebugMessage.debugEntryMessage(null, null);

    FileData nestedFileData =
        HeatToToscaUtil.getFileData(translateTo.getResource().getType(), translateTo.getContext());
    if (nestedFileData == null) {
      logger.warn("Nested File '" + translateTo.getResource().getType()
          + "' is not exist, therefore, the nested resource with the ID '"
          + translateTo.getResourceId() + "' will be ignored in TOSCA translation");
      return;
    }
    String templateName = FileUtils.getFileWithoutExtention(translateTo.getResource().getType());
    String substitutionNodeTypeKey = ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat."
        + templateName;

    if (!translateTo.getContext().getTranslatedServiceTemplates()
        .containsKey(translateTo.getResource().getType())) {

      //substitution service template
      ServiceTemplate nestedSubstitutionServiceTemplate =
          createSubstitutionServiceTemplate(translateTo, nestedFileData, templateName);

      //global substitution service template
      ServiceTemplate globalSubstitutionServiceTemplate = new HeatToToscaUtil()
          .fetchGlobalSubstitutionServiceTemplate(translateTo.getServiceTemplate(),
              translateTo.getContext());

      //substitution node type
      NodeType substitutionNodeType = new ToscaAnalyzerServiceImpl()
          .createInitSubstitutionNodeType(nestedSubstitutionServiceTemplate,
              ToscaNodeType.ABSTRACT_SUBSTITUTE);
      DataModelUtil.addNodeType(globalSubstitutionServiceTemplate, substitutionNodeTypeKey,
          substitutionNodeType);
      //substitution mapping
      HeatToToscaUtil
          .handleSubstitutionMapping(translateTo.getContext(), substitutionNodeTypeKey,
              nestedSubstitutionServiceTemplate, substitutionNodeType);

      //add new nested service template
      translateTo.getContext().getTranslatedServiceTemplates()
          .put(translateTo.getResource().getType(), nestedSubstitutionServiceTemplate);
    }

    NodeTemplate substitutionNodeTemplate =
        HeatToToscaUtil.createAbstractSubstitutionNodeTemplate(translateTo, templateName,
            substitutionNodeTypeKey);
    manageSubstitutionNodeTemplateConnectionPoint(translateTo, nestedFileData,
        substitutionNodeTemplate, substitutionNodeTypeKey);
    DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
        substitutionNodeTemplate);

    //Add nested node template id to consolidation data
    ConsolidationDataUtil.updateNestedNodeTemplateId(translateTo);

    mdcDataDebugMessage.debugExitMessage(null, null);
  }

  private ServiceTemplate createSubstitutionServiceTemplate(TranslateTo translateTo,
                                                            FileData nestedFileData,
                                                            String templateName) {
    ServiceTemplate nestedSubstitutionServiceTemplate =
        HeatToToscaUtil.createInitSubstitutionServiceTemplate(templateName);
    translateTo.getContext()
        .addNestedHeatFileName(ToscaUtil.getServiceTemplateFileName(templateName),
            translateTo.getResource().getType());
    new TranslationService().translateHeatFile(nestedSubstitutionServiceTemplate, nestedFileData,
        translateTo.getContext());
    return nestedSubstitutionServiceTemplate;
  }


  private void manageSubstitutionNodeTemplateConnectionPoint(TranslateTo translateTo,
                                                             FileData nestedFileData,
                                                             NodeTemplate substitutionNodeTemplate,
                                                             String substitutionNodeTypeId) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    ServiceTemplate globalSubstitutionTemplate =
        translateTo.getContext().getTranslatedServiceTemplates()
            .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
    NodeType nodeType = globalSubstitutionTemplate.getNode_types().get(substitutionNodeTypeId);
    handlePortToNetConnections(translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
    handleSecurityRulesToPortConnections(translateTo, nestedFileData, substitutionNodeTemplate,
        nodeType);
    handleNovaToVolConnection(translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
    handleContrailV2VmInterfaceToNetworkConnection(translateTo, nestedFileData,
        substitutionNodeTemplate, nodeType);
    handleContrailPortToNetConnections(translateTo, nestedFileData, substitutionNodeTemplate,
        nodeType);
    handleVlanSubInterfaceToInterfaceConnections(translateTo, nestedFileData,
        substitutionNodeTemplate, nodeType);

    mdcDataDebugMessage.debugExitMessage(null, null);
  }

  private void handleVlanSubInterfaceToInterfaceConnections(TranslateTo translateTo,
                                                            FileData nestedFileData,
                                                            NodeTemplate substitutionNodeTemplate,
                                                            NodeType nodeType) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    ContrailV2VlanToInterfaceResourceConnection linker =
        new ContrailV2VlanToInterfaceResourceConnection(this, translateTo, nestedFileData,
            substitutionNodeTemplate, nodeType);
    linker.connect();

    mdcDataDebugMessage.debugExitMessage(null, null);
  }


  private void handleContrailV2VmInterfaceToNetworkConnection(TranslateTo translateTo,
                                                              FileData nestedFileData,
                                                              NodeTemplate substitutionNodeTemplate,
                                                              NodeType nodeType) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    ContrailV2VmInterfaceToNetResourceConnection linker =
        new ContrailV2VmInterfaceToNetResourceConnection(this, translateTo, nestedFileData,
            substitutionNodeTemplate, nodeType);
    linker.connect();

    mdcDataDebugMessage.debugExitMessage(null, null);
  }

  private void handleNovaToVolConnection(TranslateTo translateTo, FileData nestedFileData,
                                         NodeTemplate substitutionNodeTemplate, NodeType nodeType) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    NovaToVolResourceConnection linker =
        new NovaToVolResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
            nodeType);
    linker.connect();

    mdcDataDebugMessage.debugExitMessage(null, null);
  }

  private void handleSecurityRulesToPortConnections(TranslateTo translateTo,
                                                    FileData nestedFileData,
                                                    NodeTemplate substitutionNodeTemplate,
                                                    NodeType nodeType) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    SecurityRulesToPortResourceConnection linker =
        new SecurityRulesToPortResourceConnection(this, translateTo, nestedFileData,
            substitutionNodeTemplate, nodeType);
    linker.connect();

    mdcDataDebugMessage.debugExitMessage(null, null);
  }

  private void handlePortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
                                          NodeTemplate substitutionNodeTemplate,
                                          NodeType nodeType) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    PortToNetResourceConnection linker =
        new PortToNetResourceConnection(this, translateTo, nestedFileData, substitutionNodeTemplate,
            nodeType);
    linker.connect();

    mdcDataDebugMessage.debugExitMessage(null, null);
  }

  private void handleContrailPortToNetConnections(TranslateTo translateTo, FileData nestedFileData,
                                                  NodeTemplate substitutionNodeTemplate,
                                                  NodeType nodeType) {


    mdcDataDebugMessage.debugEntryMessage(null, null);

    ContrailPortToNetResourceConnection linker =
        new ContrailPortToNetResourceConnection(this, translateTo, nestedFileData,
            substitutionNodeTemplate, nodeType);
    linker.connect();

    mdcDataDebugMessage.debugExitMessage(null, null);
  }

}