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.Collections;
41 import java.util.List;
43 import java.util.Optional;
44 import java.util.function.Predicate;
46 public class PortToNetResourceConnection extends ResourceConnectionUsingRequirementHelper {
48 public PortToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
49 TranslateTo translateTo, FileData nestedFileData,
50 NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
51 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
55 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
56 return nodeTemplate.getType().equals(ToscaNodeType.NEUTRON_PORT);
60 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
61 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
63 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE)
64 && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
65 && req.getRelationship().equals(
66 ToscaRelationshipType.NATIVE_NETWORK_LINK_TO));
71 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
72 Resource heatResource,
73 HeatOrchestrationTemplate
74 nestedHeatOrchestrationTemplate,
75 String nestedHeatFileName) {
78 mdcDataDebugMessage.debugEntryMessage(null, null);
80 Optional<AttachedResourceId> network = HeatToToscaUtil
81 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
82 translateTo.getContext(), heatResource.getProperties().get("network"));
83 if (network.isPresent() && network.get().isGetParam()
84 && network.get().getEntityId() instanceof String) {
85 return Optional.of(Collections.singletonList((String) network.get().getEntityId()));
87 network = HeatToToscaUtil
88 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
89 translateTo.getContext(), heatResource.getProperties().get("network_id"));
90 if (network.isPresent()
91 && network.get().isGetParam()
92 && network.get().getEntityId() instanceof String) {
93 mdcDataDebugMessage.debugExitMessage(null, null);
94 return Optional.of(Collections.singletonList((String) network.get().getEntityId()));
96 mdcDataDebugMessage.debugExitMessage(null, null);
97 return Optional.empty();
103 protected String getDesiredResourceType() {
104 return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
108 protected void addRequirementToConnectResources(
109 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
110 List<String> paramNames) {
113 mdcDataDebugMessage.debugEntryMessage(null, null);
115 if (paramNames == null || paramNames.isEmpty()) {
118 String paramName = paramNames.get(
119 0); // port can connect to one network only and we are
120 // expecting to have only one param(unlike security rules to port)
121 Object paramValue = translateTo.getResource().getProperties().get(paramName);
122 List<String> supportedNetworkTypes =
123 Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
124 HeatResourcesTypes.CONTRAIL_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource(),
125 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
127 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
128 supportedNetworkTypes);
130 mdcDataDebugMessage.debugExitMessage(null, null);
134 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
135 final String nestedPropertyName,
136 String connectionPointId,
137 Resource connectedResource,
138 List<String> supportedTypes) {
141 mdcDataDebugMessage.debugEntryMessage(null, null);
143 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
144 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
145 + "' is pointing to a resource with type '" + connectedResource.getType()
146 + "' which is not supported for requirement '" + connectionPointId
147 + "' that connect port to network. Supported types are: '" + supportedTypes.toString()
148 + "', therefore, this TOSCA requirement will not be connected.");
150 mdcDataDebugMessage.debugExitMessage(null, null);
154 mdcDataDebugMessage.debugExitMessage(null, null);