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