76396f77bc0de105213c496f5cb6da4fe4d1e489
[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.sdc.heat.datatypes.model.HeatResourcesTypes;
24 import org.openecomp.sdc.heat.datatypes.model.Resource;
25 import org.openecomp.sdc.heat.services.HeatConstants;
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.services.DataModelUtil;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
32 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
33 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailV2VirtualMachineInterfaceHelper;
34 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
35
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Objects;
39 import java.util.Optional;
40
41
42 public class ResourceTranslationContrailV2VlanSubInterfaceImpl extends ResourceTranslationBase {
43   protected static Logger logger =
44       (Logger) LoggerFactory.getLogger(ResourceTranslationContrailV2VlanSubInterfaceImpl.class);
45
46   @Override
47   protected void translate(TranslateTo translateTo) {
48
49
50     mdcDataDebugMessage.debugEntryMessage(null, null);
51
52     NodeTemplate nodeTemplate = new NodeTemplate();
53     nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
54
55     nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
56         .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),translateTo.
57             getResourceId(),translateTo.getResource().getProperties(),
58             nodeTemplate.getProperties(), translateTo.getHeatFileName(),
59             translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
60             nodeTemplate, translateTo.getContext()));
61
62     new ContrailV2VirtualMachineInterfaceHelper()
63         .connectVmiToNetwork(this, translateTo, nodeTemplate);
64     connectSubInterfaceToInterface(translateTo, nodeTemplate);
65     DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
66         nodeTemplate);
67
68     mdcDataDebugMessage.debugExitMessage(null, null);
69   }
70
71   //connection to shared interface is not supported
72   private void connectSubInterfaceToInterface(TranslateTo translateTo,
73                                               NodeTemplate vlanSubInterfaceNodeTemplate) {
74
75
76     mdcDataDebugMessage.debugEntryMessage(null, null);
77
78     Object interfaceRefs =
79         translateTo.getResource().getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME);
80     if (Objects.isNull(interfaceRefs) || !(interfaceRefs instanceof List)
81         || ((List) interfaceRefs).size() == 0) {
82       return;
83     }
84     List<String> acceptableResourceTypes = Arrays
85         .asList(HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE
86                 .getHeatResource(),
87             HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
88     if (((List) interfaceRefs).size() > 1) {
89       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
90           + translateTo.getResource().getType()
91           + "' include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
92           + "' property with more than one interface values, only "
93           + "the first interface will be connected, "
94           + "all rest will be ignored in TOSCA translation.");
95     }
96     Object interfaceRef = ((List) interfaceRefs).get(0);
97
98     Optional<String> interfaceResourceId =
99         HeatToToscaUtil.extractContrailGetResourceAttachedHeatResourceId(interfaceRef);
100     if (interfaceResourceId.isPresent()) { // get_resource
101       Resource interfaceResource = HeatToToscaUtil
102           .getResource(translateTo.getHeatOrchestrationTemplate(), interfaceResourceId.get(),
103               translateTo.getHeatFileName());
104
105       if (acceptableResourceTypes.contains(interfaceResource.getType())
106           && !(new ContrailV2VirtualMachineInterfaceHelper()
107           .isVlanSubInterfaceResource(interfaceResource))) {
108         Optional<String> interfaceResourceTranslatedId =
109             getResourceTranslatedId(translateTo.getHeatFileName(),
110                 translateTo.getHeatOrchestrationTemplate(), interfaceResourceId.get(),
111                 translateTo.getContext());
112         interfaceResourceTranslatedId.ifPresent(interfaceResourceTranslatedIdVal -> HeatToToscaUtil
113             .addBindingReqFromSubInterfaceToInterface(vlanSubInterfaceNodeTemplate,
114                 interfaceResourceTranslatedIdVal));
115       } else {
116         logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
117             + translateTo.getResource().getType()
118             + "' include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
119             + "' property which is connect to unsupported/incorrect "
120             + (true == (new ContrailV2VirtualMachineInterfaceHelper()
121             .isVlanSubInterfaceResource(interfaceResource)) ? "Vlan Sub interface " : "")
122             + "resource '" + interfaceResourceId.get() + "' with type '"
123             + interfaceResource.getType()
124             + "', therefore, this connection will be ignored in TOSCA translation.");
125       }
126     }
127
128     mdcDataDebugMessage.debugExitMessage(null, null);
129   }
130
131 }