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 com.google.common.collect.ImmutableList;
20 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
21 import org.onap.sdc.tosca.datatypes.model.NodeType;
22 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
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.heat.services.HeatConstants;
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.ToscaServiceModel;
32 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
33 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
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;
38 import java.util.ArrayList;
39 import java.util.List;
41 import java.util.Optional;
42 import java.util.function.Predicate;
44 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_CONTRAIL_PORT_NETWORK_REQUIREMENT_CONNECTION;
46 public class ContrailPortToNetResourceConnection extends ResourceConnectionUsingRequirementHelper {
47 ContrailPortToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
48 TranslateTo translateTo, FileData nestedFileData,
49 NodeTemplate substitutionNodeTemplate,
51 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
55 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
56 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
57 ToscaServiceModel toscaServiceModel =
58 HeatToToscaUtil.getToscaServiceModel(translateTo.getContext());
59 return toscaAnalyzerService
60 .isTypeOf(nodeTemplate, ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE,
61 translateTo.getContext().getTranslatedServiceTemplates()
62 .get(translateTo.getResource().getType()), toscaServiceModel);
66 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
67 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
69 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE)
70 && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
71 && req.getRelationship()
72 .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO));
77 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
78 Resource heatResource,
79 HeatOrchestrationTemplate
80 nestedHeatOrchestrationTemplate,
81 String nestedHeatFileName) {
82 Object interfaceListProperty = heatResource.getProperties().get(HeatConstants.INTERFACE_LIST_PROPERTY_NAME);
83 if (interfaceListProperty == null) {
84 return Optional.empty();
86 List<String> paramsList = new ArrayList<>();
87 if (interfaceListProperty instanceof List) {
88 return getConnectorPropertyParamNameFromList(nestedHeatOrchestrationTemplate, nestedHeatFileName,
89 (List) interfaceListProperty, paramsList);
90 } else if (interfaceListProperty instanceof Map) {
91 return getConnectorPropertyParamNameFromMap(nestedHeatOrchestrationTemplate, nestedHeatFileName,
92 (Map) interfaceListProperty, paramsList);
94 return Optional.empty();
97 private Optional<List<String>> getConnectorPropertyParamNameFromList(
98 HeatOrchestrationTemplate nestedHeatOrchestrationTemplate, String nestedHeatFileName,
99 List interfaceListProperty, List<String> paramsList) {
100 for (Object interfaceEntry : interfaceListProperty) {
101 if (interfaceEntry instanceof Map) {
102 Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil.extractAttachedResourceId(
103 nestedHeatFileName, nestedHeatOrchestrationTemplate, translateTo.getContext(),
104 ((Map) interfaceEntry).get(HeatConstants.VIRTUAL_NETWORK_PROPERTY_NAME));
105 if (attachedVirtualNetwork.isPresent() && attachedVirtualNetwork.get().isGetParam()
106 && attachedVirtualNetwork.get().getEntityId() instanceof String) {
107 paramsList.add((String) attachedVirtualNetwork.get().getEntityId());
111 return Optional.of(paramsList);
114 private Optional<List<String>> getConnectorPropertyParamNameFromMap(HeatOrchestrationTemplate
115 nestedHeatOrchestrationTemplate,
116 String nestedHeatFileName,
117 Map interfaceListProperty,
118 List<String> paramsList) {
119 Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil
120 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
121 translateTo.getContext(),
122 interfaceListProperty.get(HeatConstants.VIRTUAL_NETWORK_PROPERTY_NAME));
123 if (attachedVirtualNetwork.isPresent() && attachedVirtualNetwork.get().isGetParam()
124 && attachedVirtualNetwork.get().getEntityId() instanceof String) {
125 paramsList.add((String) attachedVirtualNetwork.get().getEntityId());
126 return Optional.of(paramsList);
128 return Optional.empty();
132 protected String getDesiredResourceType() {
133 return HeatResourcesTypes.CONTRAIL_SERVICE_INSTANCE.getHeatResource();
137 protected void addRequirementToConnectResources(
138 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
139 List<String> paramNames) {
140 if (paramNames == null || paramNames.isEmpty()) {
143 Integer index = Integer.valueOf(
144 requirementDefinitionEntry.getKey().substring("link_port_".length()).substring(0, 1));
146 String paramName = paramNames.get(
147 index); // port can connect to one network only and we are
148 // expecting to get only one param(unlike security rules to port)
149 Object paramValue = translateTo.getResource().getProperties().get(paramName);
150 List<String> supportedNetworkTypes =
151 ImmutableList.of(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
152 HeatResourcesTypes.CONTRAIL_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
154 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
155 supportedNetworkTypes);
159 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
160 final String nestedPropertyName,
161 String connectionPointId,
162 Resource connectedResource,
163 List<String> supportedTypes) {
164 if (resourceTranslationBase.isUnsupportedResourceType(connectedResource, supportedTypes)) {
165 logger.warn(LOG_UNSUPPORTED_CONTRAIL_PORT_NETWORK_REQUIREMENT_CONNECTION, nestedResourceId,
166 nestedPropertyName, connectedResource.getType(), connectionPointId, supportedTypes.toString());