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 / ContrailV2VmInterfaceToNetResourceConnection.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 com.google.common.collect.ImmutableList;
20 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
21 import org.onap.sdc.tosca.datatypes.model.NodeType;
22 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
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.heat.services.HeatConstants;
28 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
29 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
30 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
33 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
34
35 import java.util.*;
36 import java.util.function.Predicate;
37
38 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_MULTIPLE_VIRTUAL_NETWORK_REFS_VALUES;
39 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_VMI_NETWORK_REQUIREMENT_CONNECTION;
40
41 public class ContrailV2VmInterfaceToNetResourceConnection
42         extends ResourceConnectionUsingRequirementHelper {
43
44     ContrailV2VmInterfaceToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
45                                                  TranslateTo translateTo, FileData nestedFileData,
46                                                  NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
47         super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
48     }
49
50     @Override
51     protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
52         return (nodeTemplate.getType()
53                 .equals(ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE)
54                 || nodeTemplate.getType()
55                 .equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE));
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()
65                         .equals(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         List<String> networks = new ArrayList<>();
76         Object virtualNetworkRefs = heatResource.getProperties().get(HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME);
77         if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
78                 || ((List) virtualNetworkRefs).isEmpty()) {
79             return Optional.empty();
80         }
81         if (((List) virtualNetworkRefs).size() > 1) {
82             logger.warn(LOG_MULTIPLE_VIRTUAL_NETWORK_REFS_VALUES, translateTo.getResourceId(),
83                     translateTo.getResource().getType(), heatResourceId,
84                     HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource());
85         }
86         Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
87         Optional<AttachedResourceId> network = HeatToToscaUtil
88                 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
89                         translateTo.getContext(), virtualNetworkRef);
90         if (network.isPresent() && network.get().isGetParam()
91                 && network.get().getEntityId() instanceof String) {
92             networks.add((String) network.get().getEntityId());
93         }
94         return Optional.of(networks);
95     }
96
97     @Override
98     protected String getDesiredResourceType() {
99         return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
100     }
101
102     @Override
103     protected void addRequirementToConnectResources(
104             Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
105             List<String> paramNames) {
106         if (paramNames == null || paramNames.isEmpty()) {
107             return;
108         }
109         for (String paramName : paramNames) {
110             Object paramValue = translateTo.getResource().getProperties().get(paramName);
111             List<String> supportedNetworkTypes =
112                     ImmutableList.of(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
113                             HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
114
115             addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
116                     supportedNetworkTypes);
117         }
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.isUnsupportedResourceType(connectedResource, supportedTypes)) {
127             logger.warn(LOG_UNSUPPORTED_VMI_NETWORK_REQUIREMENT_CONNECTION,
128                     nestedResourceId, nestedPropertyName, connectedResource.getType(), connectionPointId,
129                     supportedTypes.toString());
130             return false;
131         }
132         return true;
133     }
134 }