06d338d6d6b11b7131fa244cfe7f913233b5f4d9
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / PortToNetResourceConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
22
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;
36
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Optional;
43 import java.util.function.Predicate;
44
45 public class PortToNetResourceConnection extends ResourceConnectionUsingRequirementHelper {
46
47   public PortToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
48                                      TranslateTo translateTo, FileData nestedFileData,
49                                      NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
50     super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
51   }
52
53   @Override
54   protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
55     return nodeTemplate.getType().equals(ToscaNodeType.NEUTRON_PORT);
56   }
57
58   @Override
59   protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
60     ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
61     predicates.add(
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));
66     return predicates;
67   }
68
69   @Override
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()));
81     } else {
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()));
89       } else {
90         return Optional.empty();
91       }
92     }
93   }
94
95   @Override
96   protected String getDesiredResourceType() {
97     return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
98   }
99
100   @Override
101   protected void addRequirementToConnectResources(
102       Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
103       List<String> paramNames) {
104     if (paramNames == null || paramNames.isEmpty()) {
105       return;
106     }
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());
115
116     addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
117         supportedNetworkTypes);
118   }
119
120   @Override
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.");
132       return false;
133     }
134     return true;
135   }
136
137
138 }