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.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;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
43 import java.util.Objects;
44 import java.util.Optional;
45 import java.util.function.Predicate;
47 public class ContrailV2VlanToInterfaceResourceConnection
48 extends ResourceConnectionUsingRequirementHelper {
50 public ContrailV2VlanToInterfaceResourceConnection(
51 ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
52 FileData nestedFileData, NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
53 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
57 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
58 return nodeTemplate.getType()
59 .equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
63 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
64 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
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));
74 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
75 Resource heatResource,
76 HeatOrchestrationTemplate
77 nestedHeatOrchestrationTemplate,
78 String nestedHeatFileName) {
81 mdcDataDebugMessage.debugEntryMessage(null, null);
83 List<String> interfaces = new ArrayList<>();
84 Object interfaceRefs = heatResource.getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME);
85 if (Objects.isNull(interfaceRefs) || !(interfaceRefs instanceof List)
86 || ((List) interfaceRefs).size() == 0) {
87 return Optional.empty();
89 if (((List) interfaceRefs).size() > 1) {
90 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with nested heat file: '"
91 + translateTo.getResource().getType()
92 + "' has resource '" + heatResourceId + "' with type '"
93 + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
94 + "' which include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
95 + "' property with more than one interface values, "
96 + "only the first interface will be connected, all rest will be ignored in TOSCA "
99 Object interfaceRef = ((List) interfaceRefs).get(0);
100 Optional<AttachedResourceId> attachedInterfaceResource = HeatToToscaUtil
101 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
102 translateTo.getContext(), interfaceRef);
103 if (attachedInterfaceResource.isPresent() && attachedInterfaceResource.get().isGetParam()
104 && attachedInterfaceResource.get().getEntityId() instanceof String) {
105 interfaces.add((String) attachedInterfaceResource.get().getEntityId());
108 mdcDataDebugMessage.debugExitMessage(null, null);
109 return Optional.of(interfaces);
113 protected String getDesiredResourceType() {
114 return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
118 protected void addRequirementToConnectResources(
119 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
120 List<String> paramNames) {
123 mdcDataDebugMessage.debugEntryMessage(null, null);
125 if (paramNames == null || paramNames.isEmpty()) {
128 for (String paramName : paramNames) {
129 Object paramValue = translateTo.getResource().getProperties().get(paramName);
130 List<String> supportedInterfaceTypes =
131 Arrays.asList(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource(),
132 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE
135 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
136 supportedInterfaceTypes);
139 mdcDataDebugMessage.debugExitMessage(null, null);
143 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
144 final String nestedPropertyName,
145 String connectionPointId,
146 Resource connectedResource,
147 List<String> supportedTypes) {
150 mdcDataDebugMessage.debugEntryMessage(null, null);
152 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)
153 || (new ContrailV2VirtualMachineInterfaceHelper()
154 .isVlanSubInterfaceResource(connectedResource))) {
155 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
156 + "' is pointing to a " + (true == (new ContrailV2VirtualMachineInterfaceHelper()
157 .isVlanSubInterfaceResource(connectedResource)) ? "Vlan Sub interface " : "")
158 + "resource with type '" + connectedResource.getType() + "' which"
159 + " is not supported for requirement '" + connectionPointId
160 + "' that connect vmi vlan sub interface to interface. Supported types are: '"
161 + supportedTypes.toString() + "' (excluding Vlan), therefore, this TOSCA requirement will"
162 + " not be connected.");
164 mdcDataDebugMessage.debugExitMessage(null, null);
168 mdcDataDebugMessage.debugExitMessage(null, null);