Rename packages from openecomp to onap.
[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.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.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     List<String> networks = new ArrayList<>();
80     Object virtualNetworkRefs = heatResource.getProperties().get("virtual_network_refs");
81     if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
82         || ((List) virtualNetworkRefs).size() == 0) {
83       return Optional.empty();
84     }
85     if (((List) virtualNetworkRefs).size() > 1) {
86       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with nested heat file: '"
87           + translateTo.getResource().getType()
88           + "' has resource '" + heatResourceId + "' with type '"
89           + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
90           + "' which include 'virtual_network_refs' property with more than one network values, "
91           + "only the first network will be translated, all rest will be ignored in TOSCA "
92           + "translation.");
93     }
94     Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
95     Optional<AttachedResourceId> network = HeatToToscaUtil
96         .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
97             translateTo.getContext(), virtualNetworkRef);
98     if (network.isPresent() && network.get().isGetParam()
99         && network.get().getEntityId() instanceof String) {
100       networks.add((String) network.get().getEntityId());
101     }
102     return Optional.of(networks);
103   }
104
105   @Override
106   protected String getDesiredResourceType() {
107     return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
108   }
109
110   @Override
111   protected void addRequirementToConnectResources(
112       Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
113       List<String> paramNames) {
114     if (paramNames == null || paramNames.isEmpty()) {
115       return;
116     }
117     for (String paramName : paramNames) {
118       Object paramValue = translateTo.getResource().getProperties().get(paramName);
119       List<String> supportedNetworkTypes =
120           Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
121               HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
122
123       addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
124           supportedNetworkTypes);
125     }
126   }
127
128   @Override
129   boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
130                                                       final String nestedPropertyName,
131                                                       String connectionPointId,
132                                                       Resource connectedResource,
133                                                       List<String> supportedTypes) {
134     if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
135       logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
136           + "' is pointing to a resource with type '" + connectedResource.getType()
137           + "' which is not supported for requirement '" + connectionPointId
138           + "' that connect virtual machine interface to network. Supported types are: '"
139           + supportedTypes.toString()
140           + "', therefore, this TOSCA requirement will not be connected.");
141       return false;
142     }
143     return true;
144   }
145 }