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.helper;
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.datatypes.model.RequirementAssignment;
31 import org.openecomp.sdc.tosca.services.ToscaConstants;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslatedHeatResource;
35 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
36 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationEntityType;
37 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
38 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
40 import java.util.Arrays;
41 import java.util.List;
43 import java.util.Objects;
44 import java.util.Optional;
46 public class ContrailV2VirtualMachineInterfaceHelper {
47 static Logger logger =
48 (Logger) LoggerFactory.getLogger(ContrailV2VirtualMachineInterfaceHelper.class);
50 * Connect Virtual Machine Interface node template to network node template in TOSCA.
52 * @param resourceTranslationImpl resource translation implemetation
53 * @param translateTo translated ro object
54 * @param vmiNodeTemplate Virtual Machine Interface node template
56 public void connectVmiToNetwork(ResourceTranslationBase resourceTranslationImpl,
57 TranslateTo translateTo, NodeTemplate vmiNodeTemplate) {
58 Object virtualNetworkRefs =
59 translateTo.getResource().getProperties()
60 .get(HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME);
61 if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
62 || ((List) virtualNetworkRefs).size() == 0) {
65 List<String> acceptableResourceTypes = Arrays
66 .asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
67 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
69 if (((List) virtualNetworkRefs).size() > 1) {
70 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
71 + translateTo.getResource().getType()
73 + HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME
74 + "' property with more than one network values, only "
75 + "the first network will be connected, "
76 + "all rest will be ignored in TOSCA translation.");
78 Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
80 Optional<String> networkResourceId =
81 HeatToToscaUtil.extractContrailGetResourceAttachedHeatResourceId(virtualNetworkRef);
82 if (networkResourceId.isPresent()) { // get_resource
83 Resource networkResource = HeatToToscaUtil
84 .getResource(translateTo.getHeatOrchestrationTemplate(), networkResourceId.get(),
85 translateTo.getHeatFileName());
86 if (acceptableResourceTypes.contains(networkResource.getType())) {
87 Optional<String> resourceTranslatedId =
88 resourceTranslationImpl.getResourceTranslatedId(translateTo.getHeatFileName(),
89 translateTo.getHeatOrchestrationTemplate(), networkResourceId.get(),
90 translateTo.getContext());
92 if (resourceTranslatedId.isPresent()) {
93 RequirementAssignment requirementAssignment = HeatToToscaUtil.addLinkReqFromPortToNetwork(
94 vmiNodeTemplate, resourceTranslatedId.get());
96 if (ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE
97 .equals(vmiNodeTemplate.getType())) {
99 .updateNodesConnectedOut(translateTo, resourceTranslatedId.get(),
100 ConsolidationEntityType.PORT,
101 ToscaConstants.LINK_REQUIREMENT_ID, requirementAssignment);
105 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
106 + translateTo.getResource().getType()
107 + "' include '" + HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME
108 + "' property which is connect to "
109 + "unsupported/incorrect resource with type '"
110 + networkResource.getType()
111 + "', therefore, this connection will be ignored in TOSCA translation.");
114 Optional<AttachedResourceId> attachedResourceId = HeatToToscaUtil
115 .extractAttachedResourceId(translateTo.getHeatFileName(),
116 translateTo.getHeatOrchestrationTemplate(), translateTo.getContext(),
118 if (attachedResourceId.isPresent() && attachedResourceId.get().isGetParam()
119 && attachedResourceId.get().getEntityId() instanceof String) {
120 TranslatedHeatResource translatedSharedResourceId =
121 translateTo.getContext().getHeatSharedResourcesByParam()
122 .get(attachedResourceId.get().getEntityId());
123 if (Objects.nonNull(translatedSharedResourceId)
124 && !HeatToToscaUtil.isHeatFileNested(translateTo, translateTo.getHeatFileName())) {
125 RequirementAssignment requirementAssignment = HeatToToscaUtil.addLinkReqFromPortToNetwork(
126 vmiNodeTemplate, translatedSharedResourceId.getTranslatedId());
128 if (ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE
129 .equals(vmiNodeTemplate.getType())) {
130 ConsolidationDataUtil.updateNodesConnectedOut(translateTo, translatedSharedResourceId
131 .getTranslatedId(), ConsolidationEntityType.PORT,
132 ToscaConstants.LINK_REQUIREMENT_ID,
133 requirementAssignment);
141 * Check if the input heat resource is Vlan sub interface resource
143 * @param resource heat resource to be checked
144 * @return true - if input resource is valn sub interface resource flase - otherwise.
146 public boolean isVlanSubInterfaceResource(Resource resource) {
148 if (resource.getType().equals(HeatResourcesTypes
149 .CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())
150 && getVlanTagPropertyValue(resource).isPresent()) {
157 private Optional<Object> getVlanTagPropertyValue(Resource resource) {
158 Object vmiProperties = resource.getProperties()
159 .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME);
160 if (vmiProperties != null && vmiProperties instanceof Map) {
161 return Optional.ofNullable(((Map) vmiProperties)
162 .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME));
164 return Optional.empty();