2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
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;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
26 import java.util.Objects;
27 import java.util.Optional;
28 import java.util.function.Predicate;
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;
46 public class ContrailV2VlanToInterfaceResourceConnection
47 extends ResourceConnectionUsingRequirementHelper {
49 ContrailV2VlanToInterfaceResourceConnection(ResourceTranslationBase resourceTranslationBase,
50 TranslateTo translateTo, FileData nestedFileData,
51 NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
52 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
56 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
57 return nodeTemplate.getType().equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE);
61 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
62 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
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));
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();
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);
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());
97 return Optional.of(interfaces);
101 protected String getDesiredResourceType() {
102 return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
106 protected void addRequirementToConnectResources(
107 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
108 List<String> paramNames) {
109 if (paramNames == null || paramNames.isEmpty()) {
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
119 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
120 supportedInterfaceTypes);
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,
135 getLogMessageSuffixForConnectedResource(connectedResource), connectedResource.getType(),
136 connectionPointId, supportedTypes.toString());
142 private String getLogMessageSuffixForConnectedResource(Resource connectedResource) {
143 return new ContrailV2VirtualMachineInterfaceHelper()
144 .isVlanSubInterfaceResource(connectedResource) ? "Vlan Sub interface " : "";