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