re base code
[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  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
18
19 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_PORT_NETWORK_REQUIREMENT_CONNECTION;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.function.Predicate;
27
28 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
29 import org.onap.sdc.tosca.datatypes.model.NodeType;
30 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
31 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
32 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
33 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
34 import org.openecomp.sdc.heat.datatypes.model.Resource;
35 import org.openecomp.sdc.heat.services.HeatConstants;
36 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
37 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
38 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
39 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
40 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
41 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
42
43 import com.google.common.collect.ImmutableList;
44
45 public class PortToNetResourceConnection extends ResourceConnectionUsingRequirementHelper {
46
47     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, translateTo
77                         .getContext(), heatResource.getProperties().get(HeatConstants.NETWORK_PROPERTY_NAME));
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, translateTo
84                             .getContext(), heatResource.getProperties().get(HeatConstants.NETWORK_ID_PROPERTY_NAME));
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(0); // port can connect to one network only and we are
108         // expecting to have only one param(unlike security rules to port)
109         Object paramValue = translateTo.getResource().getProperties().get(paramName);
110         List<String> supportedNetworkTypes = ImmutableList.of(
111                 HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
112                 HeatResourcesTypes.CONTRAIL_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource(),
113                 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
114
115         addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
116                 supportedNetworkTypes);
117     }
118
119     @Override
120     boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
121                                                         final String nestedPropertyName,
122                                                         String connectionPointId,
123                                                         Resource connectedResource,
124                                                         List<String> supportedTypes) {
125         if (resourceTranslationBase.isUnsupportedResourceType(connectedResource, supportedTypes)) {
126             logger.warn(LOG_UNSUPPORTED_PORT_NETWORK_REQUIREMENT_CONNECTION, nestedResourceId,
127                     nestedPropertyName, connectedResource.getType(), connectionPointId, supportedTypes.toString());
128             return false;
129         }
130         return true;
131     }
132
133
134 }