d906ff1401b5f64f75965e165c9bc9f1c2122484
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ContrailV2VlanToInterfaceResourceConnection.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.impl.resourcetranslation;
22
23 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
24 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
25 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
26 import org.openecomp.sdc.heat.datatypes.model.Resource;
27 import org.openecomp.sdc.heat.services.HeatConstants;
28 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
29 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
30 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
31 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
32 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
33 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailV2VirtualMachineInterfaceHelper;
38
39 import java.util.ArrayList;
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 import java.util.function.Predicate;
46
47 public class ContrailV2VlanToInterfaceResourceConnection
48     extends ResourceConnectionUsingRequirementHelper {
49
50   public ContrailV2VlanToInterfaceResourceConnection(
51       ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
52       FileData nestedFileData, NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
53     super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
54   }
55
56   @Override
57   protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
58     return nodeTemplate.getType()
59         .equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
60   }
61
62   @Override
63   protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
64     ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
65     predicates.add(
66         req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE)
67             && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NETWORK_PORT))
68             && req.getRelationship()
69             .equals(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO));
70     return predicates;
71   }
72
73   @Override
74   protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
75                                                                  Resource heatResource,
76                                                                  HeatOrchestrationTemplate
77                                                                    nestedHeatOrchestrationTemplate,
78                                                                  String nestedHeatFileName) {
79     List<String> interfaces = new ArrayList<>();
80     Object interfaceRefs = heatResource.getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME);
81     if (Objects.isNull(interfaceRefs) || !(interfaceRefs instanceof List)
82         || ((List) interfaceRefs).size() == 0) {
83       return Optional.empty();
84     }
85     if (((List) interfaceRefs).size() > 1) {
86       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with nested heat file: '"
87           + translateTo.getResource().getType()
88           + "' has resource '" + heatResourceId + "' with type '"
89           + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
90           + "' which include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
91           + "' property with more than one interface values, "
92           + "only the first interface will be connected, all rest will be ignored in TOSCA "
93           + "translation.");
94     }
95     Object interfaceRef = ((List) interfaceRefs).get(0);
96     Optional<AttachedResourceId> attachedInterfaceResource = HeatToToscaUtil
97         .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
98             translateTo.getContext(), interfaceRef);
99     if (attachedInterfaceResource.isPresent() && attachedInterfaceResource.get().isGetParam()
100         && attachedInterfaceResource.get().getEntityId() instanceof String) {
101       interfaces.add((String) attachedInterfaceResource.get().getEntityId());
102     }
103     return Optional.of(interfaces);
104   }
105
106   @Override
107   protected String getDesiredResourceType() {
108     return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
109   }
110
111   @Override
112   protected void addRequirementToConnectResources(
113       Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
114       List<String> paramNames) {
115     if (paramNames == null || paramNames.isEmpty()) {
116       return;
117     }
118     for (String paramName : paramNames) {
119       Object paramValue = translateTo.getResource().getProperties().get(paramName);
120       List<String> supportedInterfaceTypes =
121           Arrays.asList(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource(),
122               HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE
123                   .getHeatResource());
124
125       addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
126           supportedInterfaceTypes);
127     }
128   }
129
130   @Override
131   boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
132                                                       final String nestedPropertyName,
133                                                       String connectionPointId,
134                                                       Resource connectedResource,
135                                                       List<String> supportedTypes) {
136     if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)
137         || (new ContrailV2VirtualMachineInterfaceHelper()
138         .isVlanSubInterfaceResource(connectedResource))) {
139       logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
140           + "' is pointing to a " + (true == (new ContrailV2VirtualMachineInterfaceHelper()
141           .isVlanSubInterfaceResource(connectedResource)) ? "Vlan Sub interface " : "")
142           + "resource with type '" + connectedResource.getType() + "' which"
143           + " is not supported for requirement '" + connectionPointId
144           + "' that connect vmi vlan sub interface to interface. Supported types are: '"
145           + supportedTypes.toString() + "' (excluding Vlan), therefore, this TOSCA requirement will"
146           + " not be connected.");
147       return false;
148     }
149     return true;
150   }
151 }