75938d69f90298c5aeb116a2dbf86a931c32feba
[sdc.git] /
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_MULTIPLE_VIRTUAL_NETWORK_REFS_VALUES;
20 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_VMI_NETWORK_REQUIREMENT_CONNECTION;
21
22 import com.google.common.collect.ImmutableList;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.function.Predicate;
30
31 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
32 import org.onap.sdc.tosca.datatypes.model.NodeType;
33 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
34 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
35 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
36 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
37 import org.openecomp.sdc.heat.datatypes.model.Resource;
38 import org.openecomp.sdc.heat.services.HeatConstants;
39 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
40 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
41 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
42 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
43 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
44 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
45
46 public class ContrailV2VmInterfaceToNetResourceConnection
47         extends ResourceConnectionUsingRequirementHelper {
48
49     ContrailV2VmInterfaceToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
50                                                  TranslateTo translateTo, FileData nestedFileData,
51                                                  NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
52         super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
53     }
54
55     @Override
56     protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
57         return (nodeTemplate.getType()
58                 .equals(ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE)
59                 || nodeTemplate.getType()
60                 .equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE));
61     }
62
63     @Override
64     protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
65         ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
66         predicates.add(
67                 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE)
68                         && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
69                         && req.getRelationship()
70                         .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO));
71         return predicates;
72     }
73
74     @Override
75     protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
76                                                                    Resource heatResource,
77                                                                    HeatOrchestrationTemplate
78                                                                            nestedHeatOrchestrationTemplate,
79                                                                    String nestedHeatFileName) {
80         List<String> networks = new ArrayList<>();
81         Object virtualNetworkRefs = heatResource.getProperties().get(HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME);
82         if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
83                 || ((List) virtualNetworkRefs).isEmpty()) {
84             return Optional.empty();
85         }
86         if (((List) virtualNetworkRefs).size() > 1) {
87             logger.warn(LOG_MULTIPLE_VIRTUAL_NETWORK_REFS_VALUES, translateTo.getResourceId(),
88                     translateTo.getResource().getType(), heatResourceId,
89                     HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource());
90         }
91         Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
92         Optional<AttachedResourceId> network = HeatToToscaUtil
93                 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
94                         translateTo.getContext(), virtualNetworkRef);
95         if (network.isPresent() && network.get().isGetParam()
96                 && network.get().getEntityId() instanceof String) {
97             networks.add((String) network.get().getEntityId());
98         }
99         return Optional.of(networks);
100     }
101
102     @Override
103     protected String getDesiredResourceType() {
104         return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
105     }
106
107     @Override
108     protected void addRequirementToConnectResources(
109             Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
110             List<String> paramNames) {
111         if (paramNames == null || paramNames.isEmpty()) {
112             return;
113         }
114         for (String paramName : paramNames) {
115             Object paramValue = translateTo.getResource().getProperties().get(paramName);
116             List<String> supportedNetworkTypes =
117                     ImmutableList.of(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
118                             HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
119
120             addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
121                     supportedNetworkTypes);
122         }
123     }
124
125     @Override
126     boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
127                                                         final String nestedPropertyName,
128                                                         String connectionPointId,
129                                                         Resource connectedResource,
130                                                         List<String> supportedTypes) {
131         if (resourceTranslationBase.isUnsupportedResourceType(connectedResource, supportedTypes)) {
132             logger.warn(LOG_UNSUPPORTED_VMI_NETWORK_REQUIREMENT_CONNECTION,
133                     nestedResourceId, nestedPropertyName, connectedResource.getType(), connectionPointId,
134                     supportedTypes.toString());
135             return false;
136         }
137         return true;
138     }
139 }