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.impl.resourcetranslation;
23 import org.openecomp.sdc.common.utils.CommonUtil;
24 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.heat.services.HeatConstants;
29 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
30 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
31 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
32 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
33 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
34 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
37 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
38 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailV2VirtualMachineInterfaceHelper;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
44 import java.util.Objects;
45 import java.util.Optional;
46 import java.util.function.Predicate;
48 public class ContrailV2VlanToInterfaceResourceConnection
49 extends ResourceConnectionUsingRequirementHelper {
51 public ContrailV2VlanToInterfaceResourceConnection(
52 ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
53 FileData nestedFileData, NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
54 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
58 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
59 return nodeTemplate.getType()
60 .equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
64 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
65 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
67 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE)
68 && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NETWORK_PORT))
69 && req.getRelationship()
70 .equals(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO));
75 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
76 Resource heatResource,
77 HeatOrchestrationTemplate
78 nestedHeatOrchestrationTemplate,
79 String nestedHeatFileName) {
82 mdcDataDebugMessage.debugEntryMessage(null, null);
84 List<String> interfaces = new ArrayList<>();
85 Object interfaceRefs = heatResource.getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME);
86 if (Objects.isNull(interfaceRefs) || !(interfaceRefs instanceof List)
87 || ((List) interfaceRefs).size() == 0) {
88 return Optional.empty();
90 if (((List) interfaceRefs).size() > 1) {
91 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with nested heat file: '"
92 + translateTo.getResource().getType()
93 + "' has resource '" + heatResourceId + "' with type '"
94 + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
95 + "' which include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
96 + "' property with more than one interface values, "
97 + "only the first interface will be connected, all rest will be ignored in TOSCA "
100 Object interfaceRef = ((List) interfaceRefs).get(0);
101 Optional<AttachedResourceId> attachedInterfaceResource = HeatToToscaUtil
102 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
103 translateTo.getContext(), interfaceRef);
104 if (attachedInterfaceResource.isPresent() && attachedInterfaceResource.get().isGetParam()
105 && attachedInterfaceResource.get().getEntityId() instanceof String) {
106 interfaces.add((String) attachedInterfaceResource.get().getEntityId());
109 mdcDataDebugMessage.debugExitMessage(null, null);
110 return Optional.of(interfaces);
114 protected String getDesiredResourceType() {
115 return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
119 protected void addRequirementToConnectResources(
120 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
121 List<String> paramNames) {
124 mdcDataDebugMessage.debugEntryMessage(null, null);
126 if (paramNames == null || paramNames.isEmpty()) {
129 for (String paramName : paramNames) {
130 Object paramValue = translateTo.getResource().getProperties().get(paramName);
131 List<String> supportedInterfaceTypes =
132 Arrays.asList(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource(),
133 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE
136 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
137 supportedInterfaceTypes);
140 mdcDataDebugMessage.debugExitMessage(null, null);
144 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
145 final String nestedPropertyName,
146 String connectionPointId,
147 Resource connectedResource,
148 List<String> supportedTypes) {
151 mdcDataDebugMessage.debugEntryMessage(null, null);
153 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)
154 || (new ContrailV2VirtualMachineInterfaceHelper()
155 .isVlanSubInterfaceResource(connectedResource))) {
156 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
157 + "' is pointing to a " + (true == (new ContrailV2VirtualMachineInterfaceHelper()
158 .isVlanSubInterfaceResource(connectedResource)) ? "Vlan Sub interface " : "")
159 + "resource with type '" + connectedResource.getType() + "' which"
160 + " is not supported for requirement '" + connectionPointId
161 + "' that connect vmi vlan sub interface to interface. Supported types are: '"
162 + supportedTypes.toString() + "' (excluding Vlan), therefore, this TOSCA requirement will"
163 + " not be connected.");
165 mdcDataDebugMessage.debugExitMessage(null, null);
169 mdcDataDebugMessage.debugExitMessage(null, null);