0933174296a25cced8f8f325e1b13adce3a9fd68
[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.helper;
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.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;
39
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Objects;
44 import java.util.Optional;
45
46 public class ContrailV2VirtualMachineInterfaceHelper {
47   static Logger logger =
48       (Logger) LoggerFactory.getLogger(ContrailV2VirtualMachineInterfaceHelper.class);
49   /**
50    * Connect Virtual Machine Interface node template to network node template in TOSCA.
51    *
52    * @param resourceTranslationImpl resource translation implemetation
53    * @param translateTo             translated ro object
54    * @param vmiNodeTemplate         Virtual Machine Interface node template
55    */
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) {
63       return;
64     }
65     List<String> acceptableResourceTypes = Arrays
66         .asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
67             HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
68
69     if (((List) virtualNetworkRefs).size() > 1) {
70       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
71           + translateTo.getResource().getType()
72           + "' include '"
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.");
77     }
78     Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
79
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());
91
92         if (resourceTranslatedId.isPresent()) {
93           RequirementAssignment requirementAssignment = HeatToToscaUtil.addLinkReqFromPortToNetwork(
94               vmiNodeTemplate, resourceTranslatedId.get());
95
96           if (ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE
97               .equals(vmiNodeTemplate.getType())) {
98             ConsolidationDataUtil
99                 .updateNodesConnectedOut(translateTo, resourceTranslatedId.get(),
100                     ConsolidationEntityType.PORT,
101                     ToscaConstants.LINK_REQUIREMENT_ID, requirementAssignment);
102           }
103         }
104       } else {
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.");
112       }
113     } else {
114       Optional<AttachedResourceId> attachedResourceId = HeatToToscaUtil
115           .extractAttachedResourceId(translateTo.getHeatFileName(),
116               translateTo.getHeatOrchestrationTemplate(), translateTo.getContext(),
117               virtualNetworkRef);
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());
127
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);
134           }
135         }
136       }
137     }
138   }
139
140   /**
141    * Check if the input heat resource is Vlan sub interface resource
142    *
143    * @param resource heat resource to be checked
144    * @return true - if input resource is valn sub interface resource flase - otherwise.
145    */
146   public boolean isVlanSubInterfaceResource(Resource resource) {
147
148     if (resource.getType().equals(HeatResourcesTypes
149         .CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())
150         && getVlanTagPropertyValue(resource).isPresent()) {
151       return true;
152     }
153
154     return false;
155   }
156
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));
163     }
164     return Optional.empty();
165   }
166
167
168 }