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) {
75 Optional<AttachedResourceId> network = HeatToToscaUtil
76 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
77 translateTo.getContext(), heatResource.getProperties().get("network"));
78 if (network.isPresent() && network.get().isGetParam()
79 && network.get().getEntityId() instanceof String) {
80 return Optional.of(Collections.singletonList((String) network.get().getEntityId()));
82 network = HeatToToscaUtil
83 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
84 translateTo.getContext(), heatResource.getProperties().get("network_id"));
85 if (network.isPresent()
86 && network.get().isGetParam()
87 && network.get().getEntityId() instanceof String) {
88 return Optional.of(Collections.singletonList((String) network.get().getEntityId()));
90 return Optional.empty();
96 protected String getDesiredResourceType() {
97 return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
101 protected void addRequirementToConnectResources(
102 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
103 List<String> paramNames) {
104 if (paramNames == null || paramNames.isEmpty()) {
107 String paramName = paramNames.get(
108 0); // port can connect to one network only and we are
109 // expecting to have only one param(unlike security rules to port)
110 Object paramValue = translateTo.getResource().getProperties().get(paramName);
111 List<String> supportedNetworkTypes =
112 Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
113 HeatResourcesTypes.CONTRAIL_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource(),
114 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
116 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
117 supportedNetworkTypes);
121 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
122 final String nestedPropertyName,
123 String connectionPointId,
124 Resource connectedResource,
125 List<String> supportedTypes) {
126 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
127 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
128 + "' is pointing to a resource with type '" + connectedResource.getType()
129 + "' which is not supported for requirement '" + connectionPointId
130 + "' that connect port to network. Supported types are: '" + supportedTypes.toString()
131 + "', therefore, this TOSCA requirement will not be connected.");