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_VIRTUAL_NETWORK_REFS_VALUES;
20 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_VMI_NETWORK_REQUIREMENT_CONNECTION;
22 import com.google.common.collect.ImmutableList;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.function.Predicate;
31 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
32 import org.onap.sdc.tosca.datatypes.model.NodeType;
33 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
34 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
35 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
36 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
37 import org.openecomp.sdc.heat.datatypes.model.Resource;
38 import org.openecomp.sdc.heat.services.HeatConstants;
39 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
40 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
41 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
42 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
43 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
44 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
46 public class ContrailV2VmInterfaceToNetResourceConnection
47 extends ResourceConnectionUsingRequirementHelper {
49 ContrailV2VmInterfaceToNetResourceConnection(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()
58 .equals(ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE)
59 || nodeTemplate.getType()
60 .equals(ToscaNodeType.CONTRAILV2_VLAN_SUB_INTERFACE));
64 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
65 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
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));
75 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
76 Resource heatResource,
77 HeatOrchestrationTemplate
78 nestedHeatOrchestrationTemplate,
79 String nestedHeatFileName) {
80 List<String> networks = new ArrayList<>();
81 Object virtualNetworkRefs = heatResource.getProperties().get(HeatConstants.VIRTUAL_NETWORK_REFS_PROPERTY_NAME);
82 if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
83 || ((List) virtualNetworkRefs).isEmpty()) {
84 return Optional.empty();
86 if (((List) virtualNetworkRefs).size() > 1) {
87 logger.warn(LOG_MULTIPLE_VIRTUAL_NETWORK_REFS_VALUES, translateTo.getResourceId(),
88 translateTo.getResource().getType(), heatResourceId,
89 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource());
91 Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
92 Optional<AttachedResourceId> network = HeatToToscaUtil
93 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
94 translateTo.getContext(), virtualNetworkRef);
95 if (network.isPresent() && network.get().isGetParam()
96 && network.get().getEntityId() instanceof String) {
97 networks.add((String) network.get().getEntityId());
99 return Optional.of(networks);
103 protected String getDesiredResourceType() {
104 return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
108 protected void addRequirementToConnectResources(
109 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
110 List<String> paramNames) {
111 if (paramNames == null || paramNames.isEmpty()) {
114 for (String paramName : paramNames) {
115 Object paramValue = translateTo.getResource().getProperties().get(paramName);
116 List<String> supportedNetworkTypes =
117 ImmutableList.of(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
118 HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
120 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
121 supportedNetworkTypes);
126 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
127 final String nestedPropertyName,
128 String connectionPointId,
129 Resource connectedResource,
130 List<String> supportedTypes) {
131 if (resourceTranslationBase.isUnsupportedResourceType(connectedResource, supportedTypes)) {
132 logger.warn(LOG_UNSUPPORTED_VMI_NETWORK_REQUIREMENT_CONNECTION,
133 nestedResourceId, nestedPropertyName, connectedResource.getType(), connectionPointId,
134 supportedTypes.toString());