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.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;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
42 import java.util.Objects;
43 import java.util.Optional;
44 import java.util.function.Predicate;
46 public class ContrailV2VmInterfaceToNetResourceConnection
47 extends ResourceConnectionUsingRequirementHelper {
49 public ContrailV2VmInterfaceToNetResourceConnection(
50 ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
51 FileData nestedFileData, NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
52 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
56 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
57 return (nodeTemplate.getType()
58 .equals(ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE)
59 || 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_LINKABLE)
68 && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
69 && req.getRelationship()
70 .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_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> networks = new ArrayList<>();
85 Object virtualNetworkRefs = heatResource.getProperties().get("virtual_network_refs");
86 if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
87 || ((List) virtualNetworkRefs).size() == 0) {
88 return Optional.empty();
90 if (((List) virtualNetworkRefs).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 'virtual_network_refs' property with more than one network values, "
96 + "only the first network will be translated, all rest will be ignored in TOSCA "
99 Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
100 Optional<AttachedResourceId> network = HeatToToscaUtil
101 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
102 translateTo.getContext(), virtualNetworkRef);
103 if (network.isPresent() && network.get().isGetParam()
104 && network.get().getEntityId() instanceof String) {
105 networks.add((String) network.get().getEntityId());
108 mdcDataDebugMessage.debugExitMessage(null, null);
109 return Optional.of(networks);
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> supportedNetworkTypes =
131 Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
132 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
134 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
135 supportedNetworkTypes);
138 mdcDataDebugMessage.debugExitMessage(null, null);
142 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
143 final String nestedPropertyName,
144 String connectionPointId,
145 Resource connectedResource,
146 List<String> supportedTypes) {
149 mdcDataDebugMessage.debugEntryMessage(null, null);
151 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
152 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
153 + "' is pointing to a resource with type '" + connectedResource.getType()
154 + "' which is not supported for requirement '" + connectionPointId
155 + "' that connect virtual machine interface to network. Supported types are: '"
156 + supportedTypes.toString()
157 + "', therefore, this TOSCA requirement will not be connected.");
159 mdcDataDebugMessage.debugExitMessage(null, null);
163 mdcDataDebugMessage.debugExitMessage(null, null);