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