Fixed SONAR issues
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ContrailV2VlanToInterfaceResourceConnection.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.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.tosca.datatypes.model.NodeTemplate;
32 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
33 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailV2VirtualMachineInterfaceHelper;
38
39 import java.util.ArrayList;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Objects;
44 import java.util.Optional;
45 import java.util.function.Predicate;
46
47 public class ContrailV2VlanToInterfaceResourceConnection
48     extends ResourceConnectionUsingRequirementHelper {
49
50   public ContrailV2VlanToInterfaceResourceConnection(
51       ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
52       FileData nestedFileData, NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
53     super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
54   }
55
56   @Override
57   protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
58     return nodeTemplate.getType()
59         .equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
60   }
61
62   @Override
63   protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
64     ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
65     predicates.add(
66         req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_BINDABLE)
67             && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NETWORK_PORT))
68             && req.getRelationship()
69             .equals(ToscaRelationshipType.NATIVE_NETWORK_BINDS_TO));
70     return predicates;
71   }
72
73   @Override
74   protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
75                                                                  Resource heatResource,
76                                                                  HeatOrchestrationTemplate
77                                                                    nestedHeatOrchestrationTemplate,
78                                                                  String nestedHeatFileName) {
79
80
81     mdcDataDebugMessage.debugEntryMessage(null, null);
82
83     List<String> interfaces = new ArrayList<>();
84     Object interfaceRefs = heatResource.getProperties().get(HeatConstants.VMI_REFS_PROPERTY_NAME);
85     if (Objects.isNull(interfaceRefs) || !(interfaceRefs instanceof List)
86         || ((List) interfaceRefs).size() == 0) {
87       return Optional.empty();
88     }
89     if (((List) interfaceRefs).size() > 1) {
90       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with nested heat file: '"
91           + translateTo.getResource().getType()
92           + "' has resource '" + heatResourceId + "' with type '"
93           + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
94           + "' which include '" + HeatConstants.VMI_REFS_PROPERTY_NAME
95           + "' property with more than one interface values, "
96           + "only the first interface will be connected, all rest will be ignored in TOSCA "
97           + "translation.");
98     }
99     Object interfaceRef = ((List) interfaceRefs).get(0);
100     Optional<AttachedResourceId> attachedInterfaceResource = HeatToToscaUtil
101         .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
102             translateTo.getContext(), interfaceRef);
103     if (attachedInterfaceResource.isPresent() && attachedInterfaceResource.get().isGetParam()
104         && attachedInterfaceResource.get().getEntityId() instanceof String) {
105       interfaces.add((String) attachedInterfaceResource.get().getEntityId());
106     }
107
108     mdcDataDebugMessage.debugExitMessage(null, null);
109     return Optional.of(interfaces);
110   }
111
112   @Override
113   protected String getDesiredResourceType() {
114     return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
115   }
116
117   @Override
118   protected void addRequirementToConnectResources(
119       Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
120       List<String> paramNames) {
121
122
123     mdcDataDebugMessage.debugEntryMessage(null, null);
124
125     if (paramNames == null || paramNames.isEmpty()) {
126       return;
127     }
128     for (String paramName : paramNames) {
129       Object paramValue = translateTo.getResource().getProperties().get(paramName);
130       List<String> supportedInterfaceTypes =
131           Arrays.asList(HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource(),
132               HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE
133                   .getHeatResource());
134
135       addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
136           supportedInterfaceTypes);
137     }
138
139     mdcDataDebugMessage.debugExitMessage(null, null);
140   }
141
142   @Override
143   boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
144                                                       final String nestedPropertyName,
145                                                       String connectionPointId,
146                                                       Resource connectedResource,
147                                                       List<String> supportedTypes) {
148
149
150     mdcDataDebugMessage.debugEntryMessage(null, null);
151
152     if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)
153         || (new ContrailV2VirtualMachineInterfaceHelper()
154         .isVlanSubInterfaceResource(connectedResource))) {
155       logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
156           + "' is pointing to a " + (true == (new ContrailV2VirtualMachineInterfaceHelper()
157           .isVlanSubInterfaceResource(connectedResource)) ? "Vlan Sub interface " : "")
158           + "resource with type '" + connectedResource.getType() + "' which"
159           + " is not supported for requirement '" + connectionPointId
160           + "' that connect vmi vlan sub interface to interface. Supported types are: '"
161           + supportedTypes.toString() + "' (excluding Vlan), therefore, this TOSCA requirement will"
162           + " not be connected.");
163
164       mdcDataDebugMessage.debugExitMessage(null, null);
165       return false;
166     }
167
168     mdcDataDebugMessage.debugExitMessage(null, null);
169     return true;
170   }
171 }