[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 / helper / ContrailV2VirtualMachineInterfaceHelper.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.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.logging.context.impl.MdcDataDebugMessage;
29 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
30 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
31 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
32 import org.openecomp.sdc.tosca.services.ToscaConstants;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslatedHeatResource;
36 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
37 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationEntityType;
38 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase;
40
41 import java.util.Arrays;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Objects;
45 import java.util.Optional;
46
47 public class ContrailV2VirtualMachineInterfaceHelper {
48   static Logger logger =
49       (Logger) LoggerFactory.getLogger(ContrailV2VirtualMachineInterfaceHelper.class);
50   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
51
52   /**
53    * Connect Virtual Machine Interface node template to network node template in TOSCA.
54    *
55    * @param resourceTranslationImpl resource translation implemetation
56    * @param translateTo             translated ro object
57    * @param vmiNodeTemplate         Virtual Machine Interface node template
58    */
59   public void connectVmiToNetwork(ResourceTranslationBase resourceTranslationImpl,
60                                   TranslateTo translateTo, NodeTemplate vmiNodeTemplate) {
61
62
63     mdcDataDebugMessage.debugEntryMessage(null, null);
64
65     Object virtualNetworkRefs =
66         translateTo.getResource().getProperties()
67             .get(HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME);
68     if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
69         || ((List) virtualNetworkRefs).size() == 0) {
70       return;
71     }
72     List<String> acceptableResourceTypes = Arrays
73         .asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
74             HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
75
76     if (((List) virtualNetworkRefs).size() > 1) {
77       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
78           + translateTo.getResource().getType()
79           + "' include '"
80           + HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME
81           + "' property with more than one network values, only "
82           + "the first network will be connected, "
83           + "all rest will be ignored in TOSCA translation.");
84     }
85     Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
86
87     Optional<String> networkResourceId =
88         HeatToToscaUtil.extractContrailGetResourceAttachedHeatResourceId(virtualNetworkRef);
89     if (networkResourceId.isPresent()) { // get_resource
90       Resource networkResource = HeatToToscaUtil
91           .getResource(translateTo.getHeatOrchestrationTemplate(), networkResourceId.get(),
92               translateTo.getHeatFileName());
93       if (acceptableResourceTypes.contains(networkResource.getType())) {
94         Optional<String> resourceTranslatedId =
95             resourceTranslationImpl.getResourceTranslatedId(translateTo.getHeatFileName(),
96                 translateTo.getHeatOrchestrationTemplate(), networkResourceId.get(),
97                 translateTo.getContext());
98
99         if (resourceTranslatedId.isPresent()) {
100           RequirementAssignment requirementAssignment = HeatToToscaUtil.addLinkReqFromPortToNetwork(
101               vmiNodeTemplate, resourceTranslatedId.get());
102
103           if (ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE
104               .equals(vmiNodeTemplate.getType())) {
105             ConsolidationDataUtil
106                 .updateNodesConnectedOut(translateTo, resourceTranslatedId.get(),
107                     ConsolidationEntityType.PORT,
108                     ToscaConstants.LINK_REQUIREMENT_ID, requirementAssignment);
109           }
110         }
111       } else {
112         logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
113             + translateTo.getResource().getType()
114             + "' include '" + HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME
115             + "' property which is connect to "
116             + "unsupported/incorrect resource with type '"
117             + networkResource.getType()
118             + "', therefore, this connection will be ignored in TOSCA translation.");
119       }
120     } else {
121       Optional<AttachedResourceId> attachedResourceId = HeatToToscaUtil
122           .extractAttachedResourceId(translateTo.getHeatFileName(),
123               translateTo.getHeatOrchestrationTemplate(), translateTo.getContext(),
124               virtualNetworkRef);
125       if (attachedResourceId.isPresent() && attachedResourceId.get().isGetParam()
126           && attachedResourceId.get().getEntityId() instanceof String) {
127         TranslatedHeatResource translatedSharedResourceId =
128             translateTo.getContext().getHeatSharedResourcesByParam()
129                 .get(attachedResourceId.get().getEntityId());
130         if (Objects.nonNull(translatedSharedResourceId)
131             && !HeatToToscaUtil.isHeatFileNested(translateTo, translateTo.getHeatFileName())) {
132           RequirementAssignment requirementAssignment = HeatToToscaUtil.addLinkReqFromPortToNetwork(
133               vmiNodeTemplate, translatedSharedResourceId.getTranslatedId());
134
135           if (ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE
136               .equals(vmiNodeTemplate.getType())) {
137             ConsolidationDataUtil.updateNodesConnectedOut(translateTo, translatedSharedResourceId
138                     .getTranslatedId(), ConsolidationEntityType.PORT,
139                 ToscaConstants.LINK_REQUIREMENT_ID,
140                 requirementAssignment);
141           }
142         }
143       }
144     }
145
146     mdcDataDebugMessage.debugExitMessage(null, null);
147   }
148
149   /**
150    * Check if the input heat resource is Vlan sub interface resource
151    *
152    * @param resource heat resource to be checked
153    * @return true - if input resource is valn sub interface resource flase - otherwise.
154    */
155   public boolean isVlanSubInterfaceResource(Resource resource) {
156
157     if (resource.getType().equals(HeatResourcesTypes
158         .CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())
159         && getVlanTagPropertyValue(resource).isPresent()) {
160       return true;
161     }
162
163     return false;
164   }
165
166   private Optional<Object> getVlanTagPropertyValue(Resource resource) {
167
168
169     mdcDataDebugMessage.debugEntryMessage(null, null);
170
171     Object vmiProperties = resource.getProperties()
172         .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME);
173     if (vmiProperties != null && vmiProperties instanceof Map) {
174       mdcDataDebugMessage.debugExitMessage(null, null);
175       return Optional.ofNullable(((Map) vmiProperties)
176           .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME));
177     }
178
179     mdcDataDebugMessage.debugExitMessage(null, null);
180     return Optional.empty();
181   }
182
183
184 }