24ff55de6027c8aaee21ed28dadb31a548ccc7f3
[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_INTERFACE_VALUES_NESTED;
20 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_VMI_VLAN_SUB_INTERFACE_REQUIREMENT_CONNECTION;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.Optional;
28 import java.util.function.Predicate;
29
30 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
31 import org.onap.sdc.tosca.datatypes.model.NodeType;
32 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
33 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
34 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
35 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
36 import org.openecomp.sdc.heat.datatypes.model.Resource;
37 import org.openecomp.sdc.heat.services.HeatConstants;
38 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
39 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
40 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
41 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
42 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
43 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
44 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailV2VirtualMachineInterfaceHelper;
45
46 public class ContrailV2VlanToInterfaceResourceConnection
47         extends ResourceConnectionUsingRequirementHelper {
48
49     ContrailV2VlanToInterfaceResourceConnection(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().equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
58     }
59
60     @Override
61     protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
62         ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
63         predicates.add(
64                 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE)
65                         && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NETWORK_PORT))
66                         && req.getRelationship()
67                         .equals(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO));
68         return predicates;
69     }
70
71     @Override
72     protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
73                                                                    Resource heatResource,
74                                                                    HeatOrchestrationTemplate
75                                                                            nestedHeatOrchestrationTemplate,
76                                                                    String nestedHeatFileName) {
77         List<String> interfaces = new ArrayList<>();
78         Object interfaceRefs = heatResource.getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME);
79         if (Objects.isNull(interfaceRefs) || !(interfaceRefs instanceof List)
80                 || ((List) interfaceRefs).isEmpty()) {
81             return Optional.empty();
82         }
83         if (((List) interfaceRefs).size() > 1) {
84             logger.warn(LOG_MULTIPLE_INTERFACE_VALUES_NESTED,
85                     translateTo.getResourceId(), translateTo.getResource().getType(), heatResourceId,
86                     HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource(),
87                     HeatConstants.VMI_REFS_PROPERTY_NAME);
88         }
89         Object interfaceRef = ((List) interfaceRefs).get(0);
90         Optional<AttachedResourceId> attachedInterfaceResource = HeatToToscaUtil
91                 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
92                         translateTo.getContext(), interfaceRef);
93         if (attachedInterfaceResource.isPresent() && attachedInterfaceResource.get().isGetParam()
94                 && attachedInterfaceResource.get().getEntityId() instanceof String) {
95             interfaces.add((String) attachedInterfaceResource.get().getEntityId());
96         }
97         return Optional.of(interfaces);
98     }
99
100     @Override
101     protected String getDesiredResourceType() {
102         return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
103     }
104
105     @Override
106     protected void addRequirementToConnectResources(
107             Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
108             List<String> paramNames) {
109         if (paramNames == null || paramNames.isEmpty()) {
110             return;
111         }
112         for (String paramName : paramNames) {
113             Object paramValue = translateTo.getResource().getProperties().get(paramName);
114             List<String> supportedInterfaceTypes =
115                     Arrays.asList(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource(),
116                             HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE
117                                     .getHeatResource());
118
119             addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
120                     supportedInterfaceTypes);
121         }
122     }
123
124     @Override
125     boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
126                                                         final String nestedPropertyName,
127                                                         String connectionPointId,
128                                                         Resource connectedResource,
129                                                         List<String> supportedTypes) {
130         if (resourceTranslationBase.isUnsupportedResourceType(connectedResource, supportedTypes)
131                 || (new ContrailV2VirtualMachineInterfaceHelper()
132                 .isVlanSubInterfaceResource(connectedResource))) {
133             logger.warn(LOG_UNSUPPORTED_VMI_VLAN_SUB_INTERFACE_REQUIREMENT_CONNECTION, nestedResourceId,
134                     nestedPropertyName,
135                     getLogMessageSuffixForConnectedResource(connectedResource), connectedResource.getType(),
136                     connectionPointId, supportedTypes.toString());
137             return false;
138         }
139         return true;
140     }
141
142     private String getLogMessageSuffixForConnectedResource(Resource connectedResource) {
143         return new ContrailV2VirtualMachineInterfaceHelper()
144                 .isVlanSubInterfaceResource(connectedResource) ? "Vlan Sub interface " : "";
145     }
146 }