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