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 / ContrailV2VmInterfaceToNetResourceConnection.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.tosca.datatypes.ToscaCapabilityType;
28 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
29 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
30 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
31 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
32 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
35 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
36
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.Optional;
43 import java.util.function.Predicate;
44
45 public class ContrailV2VmInterfaceToNetResourceConnection
46     extends ResourceConnectionUsingRequirementHelper {
47
48   public ContrailV2VmInterfaceToNetResourceConnection(
49       ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
50       FileData nestedFileData, NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
51     super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
52   }
53
54   @Override
55   protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
56     return (nodeTemplate.getType()
57         .equals(ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE)
58         || 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_LINKABLE)
67             && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
68             && req.getRelationship()
69             .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_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> networks = new ArrayList<>();
84     Object virtualNetworkRefs = heatResource.getProperties().get("virtual_network_refs");
85     if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
86         || ((List) virtualNetworkRefs).size() == 0) {
87       return Optional.empty();
88     }
89     if (((List) virtualNetworkRefs).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 'virtual_network_refs' property with more than one network values, "
95           + "only the first network will be translated, all rest will be ignored in TOSCA "
96           + "translation.");
97     }
98     Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
99     Optional<AttachedResourceId> network = HeatToToscaUtil
100         .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
101             translateTo.getContext(), virtualNetworkRef);
102     if (network.isPresent() && network.get().isGetParam()
103         && network.get().getEntityId() instanceof String) {
104       networks.add((String) network.get().getEntityId());
105     }
106
107     mdcDataDebugMessage.debugExitMessage(null, null);
108     return Optional.of(networks);
109   }
110
111   @Override
112   protected String getDesiredResourceType() {
113     return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
114   }
115
116   @Override
117   protected void addRequirementToConnectResources(
118       Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
119       List<String> paramNames) {
120
121
122     mdcDataDebugMessage.debugEntryMessage(null, null);
123
124     if (paramNames == null || paramNames.isEmpty()) {
125       return;
126     }
127     for (String paramName : paramNames) {
128       Object paramValue = translateTo.getResource().getProperties().get(paramName);
129       List<String> supportedNetworkTypes =
130           Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
131               HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
132
133       addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
134           supportedNetworkTypes);
135     }
136
137     mdcDataDebugMessage.debugExitMessage(null, null);
138   }
139
140   @Override
141   boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
142                                                       final String nestedPropertyName,
143                                                       String connectionPointId,
144                                                       Resource connectedResource,
145                                                       List<String> supportedTypes) {
146
147
148     mdcDataDebugMessage.debugEntryMessage(null, null);
149
150     if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
151       logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
152           + "' is pointing to a resource with type '" + connectedResource.getType()
153           + "' which is not supported for requirement '" + connectionPointId
154           + "' that connect virtual machine interface to network. Supported types are: '"
155           + supportedTypes.toString()
156           + "', therefore, this TOSCA requirement will not be connected.");
157
158       mdcDataDebugMessage.debugExitMessage(null, null);
159       return false;
160     }
161
162     mdcDataDebugMessage.debugExitMessage(null, null);
163     return true;
164   }
165 }