622f3c1be3b7192322d4965eb2eed54c83b427e0
[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  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 package org.openecomp.sdc.translator.services.heattotosca.helper;
19
20 import org.apache.commons.collections4.CollectionUtils;
21 import org.apache.commons.collections4.MapUtils;
22 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
23 import org.openecomp.sdc.heat.datatypes.model.Resource;
24 import org.openecomp.sdc.heat.datatypes.model.ResourceReferenceFunctions;
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.TranslationContext;
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.ArrayList;
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Objects;
47 import java.util.Optional;
48 import java.util.Set;
49
50 public class ContrailV2VirtualMachineInterfaceHelper {
51   static Logger logger =
52       (Logger) LoggerFactory.getLogger(ContrailV2VirtualMachineInterfaceHelper.class);
53   /**
54    * Connect Virtual Machine Interface node template to network node template in TOSCA.
55    *
56    * @param resourceTranslationImpl resource translation implemetation
57    * @param translateTo             translated ro object
58    * @param vmiNodeTemplate         Virtual Machine Interface node template
59    */
60   public void connectVmiToNetwork(ResourceTranslationBase resourceTranslationImpl,
61                                   TranslateTo translateTo, NodeTemplate vmiNodeTemplate) {
62     Object virtualNetworkRefs =
63         translateTo.getResource().getProperties()
64             .get(HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME);
65     if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
66         || ((List) virtualNetworkRefs).size() == 0) {
67       return;
68     }
69     List<String> acceptableResourceTypes = Arrays
70         .asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
71             HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
72
73     if (((List) virtualNetworkRefs).size() > 1) {
74       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
75           + translateTo.getResource().getType()
76           + "' include '"
77           + HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME
78           + "' property with more than one network values, only "
79           + "the first network will be connected, "
80           + "all rest will be ignored in TOSCA translation.");
81     }
82     Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
83
84     Optional<String> networkResourceId =
85         HeatToToscaUtil.extractContrailGetResourceAttachedHeatResourceId(virtualNetworkRef);
86     if (networkResourceId.isPresent()) { // get_resource
87       Resource networkResource = HeatToToscaUtil
88           .getResource(translateTo.getHeatOrchestrationTemplate(), networkResourceId.get(),
89               translateTo.getHeatFileName());
90       if (acceptableResourceTypes.contains(networkResource.getType())) {
91         Optional<String> resourceTranslatedId =
92             resourceTranslationImpl.getResourceTranslatedId(translateTo.getHeatFileName(),
93                 translateTo.getHeatOrchestrationTemplate(), networkResourceId.get(),
94                 translateTo.getContext());
95
96         if (resourceTranslatedId.isPresent()) {
97           RequirementAssignment requirementAssignment = HeatToToscaUtil.addLinkReqFromPortToNetwork(
98               vmiNodeTemplate, resourceTranslatedId.get());
99
100           if (ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE
101               .equals(vmiNodeTemplate.getType())) {
102             ConsolidationDataUtil
103                 .updateNodesConnectedOut(translateTo, resourceTranslatedId.get(),
104                     ConsolidationEntityType.PORT,
105                     ToscaConstants.LINK_REQUIREMENT_ID, requirementAssignment);
106           }
107         }
108       } else {
109         logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
110             + translateTo.getResource().getType()
111             + "' include '" + HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME
112             + "' property which is connect to "
113             + "unsupported/incorrect resource with type '"
114             + networkResource.getType()
115             + "', therefore, this connection will be ignored in TOSCA translation.");
116       }
117     } else {
118       Optional<AttachedResourceId> attachedResourceId = HeatToToscaUtil
119           .extractAttachedResourceId(translateTo.getHeatFileName(),
120               translateTo.getHeatOrchestrationTemplate(), translateTo.getContext(),
121               virtualNetworkRef);
122       if (attachedResourceId.isPresent() && attachedResourceId.get().isGetParam()
123           && attachedResourceId.get().getEntityId() instanceof String) {
124         TranslatedHeatResource translatedSharedResourceId =
125             translateTo.getContext().getHeatSharedResourcesByParam()
126                 .get(attachedResourceId.get().getEntityId());
127         if (Objects.nonNull(translatedSharedResourceId)
128             && !HeatToToscaUtil.isHeatFileNested(translateTo, translateTo.getHeatFileName())) {
129           RequirementAssignment requirementAssignment = HeatToToscaUtil.addLinkReqFromPortToNetwork(
130               vmiNodeTemplate, translatedSharedResourceId.getTranslatedId());
131
132           if (ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE
133               .equals(vmiNodeTemplate.getType())) {
134             ConsolidationDataUtil.updateNodesConnectedOut(translateTo, translatedSharedResourceId
135                     .getTranslatedId(), ConsolidationEntityType.PORT,
136                 ToscaConstants.LINK_REQUIREMENT_ID,
137                 requirementAssignment);
138           }
139         }
140       }
141     }
142   }
143
144   /**
145    * Check if the input heat resource is Vlan sub interface resource.
146    *
147    * @param resource heat resource to be checked
148    * @return true - if input resource is valn sub interface resource flase - otherwise.
149    */
150   public boolean isVlanSubInterfaceResource(Resource resource) {
151
152     if (resource.getType().equals(HeatResourcesTypes
153         .CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource())
154         && getVlanTagPropertyValue(resource).isPresent()) {
155       return true;
156     }
157
158     return false;
159   }
160
161   public boolean isVlanSubInterfaceConnectedToPortIndirectly(TranslateTo translateTo) {
162     Resource resource = translateTo.getResource();
163     TranslationContext context = translateTo.getContext();
164     Set<String> nestedHeatsFiles = context.getNestedHeatsFiles();
165     Map<String, Object> properties = resource.getProperties();
166
167     if (MapUtils.isNotEmpty(properties)
168         && properties.containsKey(HeatConstants.VMI_REFS_PROPERTY_NAME)) {
169       Map<String, Object> portReference = getPortReference(properties);
170
171       return CollectionUtils.isNotEmpty(nestedHeatsFiles)
172           && nestedHeatsFiles.contains(translateTo.getHeatFileName())
173           && portReference.containsKey(ResourceReferenceFunctions.GET_PARAM.getFunction());
174     }
175
176     return false;
177   }
178
179   private Map<String, Object> getPortReference(Map<String, Object> properties) {
180     Object portReferenceObj = properties.get(HeatConstants.VMI_REFS_PROPERTY_NAME);
181     List<Object> portReference =
182         portReferenceObj instanceof List ? (List<Object>) portReferenceObj : new ArrayList<>();
183     return CollectionUtils.isEmpty(portReference) ? new HashMap<>()
184         : (Map<String, Object>) portReference.get(0);
185   }
186
187   private Optional<Object> getVlanTagPropertyValue(Resource resource) {
188     Object vmiProperties = resource.getProperties()
189         .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME);
190     if (vmiProperties != null && vmiProperties instanceof Map) {
191       return Optional.ofNullable(((Map) vmiProperties)
192           .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME));
193     }
194     return Optional.empty();
195   }
196
197
198 }