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;
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.tosca.datatypes.model.ServiceTemplate;
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.datatypes.heattotosca.to.TranslatedHeatResource;
37 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.Collections;
42 import java.util.List;
44 import java.util.Objects;
45 import java.util.Optional;
46 import java.util.function.Predicate;
48 public class PortToNetResourceConnection extends NovaAndPortResourceConnectionHelper {
50 public PortToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
51 TranslateTo translateTo, FileData nestedFileData,
52 NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
53 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
57 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
58 return nodeTemplate.getType().equals(ToscaNodeType.NEUTRON_PORT.getDisplayName());
62 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
63 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
65 req -> req.getCapability().equals(ToscaCapabilityType.NETWORK_LINKABLE.getDisplayName())
66 && req.getNode().equals(ToscaNodeType.ROOT.getDisplayName())
67 && req.getRelationship().equals(
68 ToscaRelationshipType.NETWORK_LINK_TO.getDisplayName()));
73 protected Optional<List<String>> getConnectorParamName(String heatResourceId,
74 Resource heatResource,
75 HeatOrchestrationTemplate
76 nestedHeatOrchestrationTemplate) {
77 Optional<AttachedResourceId> network = HeatToToscaUtil
78 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
79 translateTo.getContext(), heatResource.getProperties().get("network"));
80 if (network.isPresent() && network.get().isGetParam()) {
81 return Optional.of(Collections.singletonList((String) network.get().getEntityId()));
83 network = HeatToToscaUtil
84 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
85 translateTo.getContext(), heatResource.getProperties().get("network_id"));
86 if (network.isPresent() && network.get().isGetParam()) {
87 return Optional.of(Collections.singletonList((String) network.get().getEntityId()));
89 return Optional.empty();
95 protected String getDesiredResourceType() {
96 return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
100 protected String getTranslatedResourceIdFromSubstitutionMapping(
101 ServiceTemplate nestedServiceTemplate, Map.Entry<String, RequirementDefinition> entry) {
102 List<String> substitutionMapping =
103 nestedServiceTemplate.getTopology_template().getSubstitution_mappings().getRequirements()
104 .get(entry.getKey());
105 return substitutionMapping.get(0);
109 protected void addRequirementToConnectResources(Map.Entry<String, RequirementDefinition> entry,
110 List<String> paramNames) {
111 String paramName = paramNames.get(
112 0); // port can connect to one network only and
113 // we are expecting to have only one param(unlike security rules to port)
114 Object paramValue = translateTo.getResource().getProperties().get(paramName);
115 if (paramValue == null) {
117 "Nested resource '" + translateTo.getResourceId() + "' is not including property '"
118 + paramName + "' with value for the nested heat file, therefore, '" + entry.getKey()
119 + "' TOSCA requirement will not be connected.");
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 Optional<String> targetTranslatedNodeId =
128 getConnectionTargetNodeUsingGetResourceFunc(entry, paramName, paramValue,
129 supportedNetworkTypes);
130 if (targetTranslatedNodeId.isPresent()) {
131 createRequirementAssignment(entry, targetTranslatedNodeId.get(), substitutionNodeTemplate);
133 targetTranslatedNodeId =
134 getConnectionTargetNodeUsingGetParam(entry, paramName, supportedNetworkTypes);
135 if (targetTranslatedNodeId.isPresent()) {
136 createRequirementAssignment(entry, targetTranslatedNodeId.get(), substitutionNodeTemplate);
141 private boolean validateResourceTypeSupportedForReqCreation(String sourceResourceId,
142 final String sourcePropertyName,
144 Resource targetResource,
145 List<String> supportedTypes) {
146 if (!resourceTranslationBase.isResourceTypeSupported(targetResource, supportedTypes)) {
147 logger.warn("Nested resource '" + sourceResourceId + "' property '" + sourcePropertyName
148 + "' is pointing to a resource with type '" + targetResource.getType()
149 + "' which is not supported for requirement '" + sourceReqId
150 + "' that connect port to network. \nSupported types are: '" + supportedTypes.toString()
151 + "', therefore, this TOSCA requirement will not be connected.");
157 private Optional<String> getConnectionTargetNodeUsingGetParam(
158 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry, String paramName,
159 List<String> supportedTargetNodeTypes) {
160 Optional<AttachedResourceId> attachedResourceId =
161 HeatToToscaUtil.extractAttachedResourceId(translateTo, paramName);
162 if (!attachedResourceId.isPresent()) {
163 return Optional.empty();
165 AttachedResourceId resourceId = attachedResourceId.get();
166 if (resourceId.isGetParam()) {
167 TranslatedHeatResource shareResource =
168 translateTo.getContext().getHeatSharedResourcesByParam().get(resourceId.getEntityId());
169 if (Objects.nonNull(shareResource)
170 && !HeatToToscaUtil.isHeatFileNested(translateTo, translateTo.getHeatFileName())) {
171 if (validateResourceTypeSupportedForReqCreation(translateTo.getResourceId(), paramName,
172 requirementDefinitionEntry.getKey(), shareResource.getHeatResource(),
173 supportedTargetNodeTypes)) {
174 return Optional.of(shareResource.getTranslatedId());
179 return Optional.empty();
182 private Optional<String> getConnectionTargetNodeUsingGetResourceFunc(
183 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry, String paramName,
184 Object paramValue, List<String> supportedTargetNodeTypes) {
185 String getResourceAttachedResourceId =
186 HeatToToscaUtil.extractContrailGetResourceAttachedHeatResourceId(paramValue);
187 if (getResourceAttachedResourceId != null) { // get resource
188 Resource resource = translateTo.getHeatOrchestrationTemplate().getResources()
189 .get(getResourceAttachedResourceId);
190 if (validateResourceTypeSupportedForReqCreation(translateTo.getResourceId(), paramName,
191 requirementDefinitionEntry.getKey(), resource, supportedTargetNodeTypes)) {
192 return ResourceTranslationBase.getResourceTranslatedId(translateTo.getHeatFileName(),
193 translateTo.getHeatOrchestrationTemplate(), getResourceAttachedResourceId,
194 translateTo.getContext());
198 return Optional.empty();