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.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;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.Objects;
39 import java.util.Optional;
42 public class ResourceTranslationContrailV2VlanSubInterfaceImpl extends ResourceTranslationBase {
43 protected static Logger logger =
44 (Logger) LoggerFactory.getLogger(ResourceTranslationContrailV2VlanSubInterfaceImpl.class);
47 protected void translate(TranslateTo translateTo) {
48 NodeTemplate nodeTemplate = new NodeTemplate();
49 nodeTemplate.setType(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
51 nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
52 .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),translateTo.
53 getResourceId(),translateTo.getResource().getProperties(),
54 nodeTemplate.getProperties(), translateTo.getHeatFileName(),
55 translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
56 nodeTemplate, translateTo.getContext()));
58 new ContrailV2VirtualMachineInterfaceHelper()
59 .connectVmiToNetwork(this, translateTo, nodeTemplate);
60 connectSubInterfaceToInterface(translateTo, nodeTemplate);
61 DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
65 //connection to shared interface is not supported
66 private void connectSubInterfaceToInterface(TranslateTo translateTo,
67 NodeTemplate vlanSubInterfaceNodeTemplate) {
68 Object interfaceRefs =
69 translateTo.getResource().getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME);
70 if (Objects.isNull(interfaceRefs) || !(interfaceRefs instanceof List)
71 || ((List) interfaceRefs).size() == 0) {
74 List<String> acceptableResourceTypes = Arrays
75 .asList(HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE
77 HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource());
78 if (((List) interfaceRefs).size() > 1) {
79 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
80 + translateTo.getResource().getType()
81 + "' include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
82 + "' property with more than one interface values, only "
83 + "the first interface will be connected, "
84 + "all rest will be ignored in TOSCA translation.");
86 Object interfaceRef = ((List) interfaceRefs).get(0);
88 Optional<String> interfaceResourceId =
89 HeatToToscaUtil.extractContrailGetResourceAttachedHeatResourceId(interfaceRef);
90 if (interfaceResourceId.isPresent()) { // get_resource
91 Resource interfaceResource = HeatToToscaUtil
92 .getResource(translateTo.getHeatOrchestrationTemplate(), interfaceResourceId.get(),
93 translateTo.getHeatFileName());
95 if (acceptableResourceTypes.contains(interfaceResource.getType())
96 && !(new ContrailV2VirtualMachineInterfaceHelper()
97 .isVlanSubInterfaceResource(interfaceResource))) {
98 Optional<String> interfaceResourceTranslatedId =
99 getResourceTranslatedId(translateTo.getHeatFileName(),
100 translateTo.getHeatOrchestrationTemplate(), interfaceResourceId.get(),
101 translateTo.getContext());
102 interfaceResourceTranslatedId.ifPresent(interfaceResourceTranslatedIdVal -> HeatToToscaUtil
103 .addBindingReqFromSubInterfaceToInterface(vlanSubInterfaceNodeTemplate,
104 interfaceResourceTranslatedIdVal));
106 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
107 + translateTo.getResource().getType()
108 + "' include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
109 + "' property which is connect to unsupported/incorrect "
110 + (true == (new ContrailV2VirtualMachineInterfaceHelper()
111 .isVlanSubInterfaceResource(interfaceResource)) ? "Vlan Sub interface " : "")
112 + "resource '" + interfaceResourceId.get() + "' with type '"
113 + interfaceResource.getType()
114 + "', therefore, this connection will be ignored in TOSCA translation.");