[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.common.utils.CommonUtil;
24 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
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
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Objects;
43 import java.util.Optional;
44 import java.util.function.Predicate;
45
46 public class ContrailV2VmInterfaceToNetResourceConnection
47     extends ResourceConnectionUsingRequirementHelper {
48
49   public ContrailV2VmInterfaceToNetResourceConnection(
50       ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
51       FileData nestedFileData, 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
81
82     mdcDataDebugMessage.debugEntryMessage(null, null);
83
84     List<String> networks = new ArrayList<>();
85     Object virtualNetworkRefs = heatResource.getProperties().get("virtual_network_refs");
86     if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
87         || ((List) virtualNetworkRefs).size() == 0) {
88       return Optional.empty();
89     }
90     if (((List) virtualNetworkRefs).size() > 1) {
91       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with nested heat file: '"
92           + translateTo.getResource().getType()
93           + "' has resource '" + heatResourceId + "' with type '"
94           + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
95           + "' which include 'virtual_network_refs' property with more than one network values, "
96           + "only the first network will be translated, all rest will be ignored in TOSCA "
97           + "translation.");
98     }
99     Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
100     Optional<AttachedResourceId> network = HeatToToscaUtil
101         .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
102             translateTo.getContext(), virtualNetworkRef);
103     if (network.isPresent() && network.get().isGetParam()
104         && network.get().getEntityId() instanceof String) {
105       networks.add((String) network.get().getEntityId());
106     }
107
108     mdcDataDebugMessage.debugExitMessage(null, null);
109     return Optional.of(networks);
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> supportedNetworkTypes =
131           Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
132               HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
133
134       addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
135           supportedNetworkTypes);
136     }
137
138     mdcDataDebugMessage.debugExitMessage(null, null);
139   }
140
141   @Override
142   boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
143                                                       final String nestedPropertyName,
144                                                       String connectionPointId,
145                                                       Resource connectedResource,
146                                                       List<String> supportedTypes) {
147
148
149     mdcDataDebugMessage.debugEntryMessage(null, null);
150
151     if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
152       logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
153           + "' is pointing to a resource with type '" + connectedResource.getType()
154           + "' which is not supported for requirement '" + connectionPointId
155           + "' that connect virtual machine interface to network. Supported types are: '"
156           + supportedTypes.toString()
157           + "', therefore, this TOSCA requirement will not be connected.");
158
159       mdcDataDebugMessage.debugExitMessage(null, null);
160       return false;
161     }
162
163     mdcDataDebugMessage.debugExitMessage(null, null);
164     return true;
165   }
166 }