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.tosca.datatypes.ToscaCapabilityType;
28 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
29 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
30 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
31 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
32 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
35 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
41 import java.util.Objects;
42 import java.util.Optional;
43 import java.util.function.Predicate;
45 public class ContrailV2VmInterfaceToNetResourceConnection
46 extends ResourceConnectionUsingRequirementHelper {
48 public ContrailV2VmInterfaceToNetResourceConnection(
49 ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
50 FileData nestedFileData, NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
51 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
55 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
56 return (nodeTemplate.getType()
57 .equals(ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE)
58 || 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_LINKABLE)
67 && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
68 && req.getRelationship()
69 .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_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> networks = new ArrayList<>();
84 Object virtualNetworkRefs = heatResource.getProperties().get("virtual_network_refs");
85 if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
86 || ((List) virtualNetworkRefs).size() == 0) {
87 return Optional.empty();
89 if (((List) virtualNetworkRefs).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 'virtual_network_refs' property with more than one network values, "
95 + "only the first network will be translated, all rest will be ignored in TOSCA "
98 Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
99 Optional<AttachedResourceId> network = HeatToToscaUtil
100 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
101 translateTo.getContext(), virtualNetworkRef);
102 if (network.isPresent() && network.get().isGetParam()
103 && network.get().getEntityId() instanceof String) {
104 networks.add((String) network.get().getEntityId());
107 mdcDataDebugMessage.debugExitMessage(null, null);
108 return Optional.of(networks);
112 protected String getDesiredResourceType() {
113 return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
117 protected void addRequirementToConnectResources(
118 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
119 List<String> paramNames) {
122 mdcDataDebugMessage.debugEntryMessage(null, null);
124 if (paramNames == null || paramNames.isEmpty()) {
127 for (String paramName : paramNames) {
128 Object paramValue = translateTo.getResource().getProperties().get(paramName);
129 List<String> supportedNetworkTypes =
130 Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
131 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
133 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
134 supportedNetworkTypes);
137 mdcDataDebugMessage.debugExitMessage(null, null);
141 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
142 final String nestedPropertyName,
143 String connectionPointId,
144 Resource connectedResource,
145 List<String> supportedTypes) {
148 mdcDataDebugMessage.debugEntryMessage(null, null);
150 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
151 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
152 + "' is pointing to a resource with type '" + connectedResource.getType()
153 + "' which is not supported for requirement '" + connectionPointId
154 + "' that connect virtual machine interface to network. Supported types are: '"
155 + supportedTypes.toString()
156 + "', therefore, this TOSCA requirement will not be connected.");
158 mdcDataDebugMessage.debugExitMessage(null, null);
162 mdcDataDebugMessage.debugExitMessage(null, null);