2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
24 import org.openecomp.sdc.common.utils.CommonUtil;
25 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
26 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
27 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.heat.services.HeatConstants;
30 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
32 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
33 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
34 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
35 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
36 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
37 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
38 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
39 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
40 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
41 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
42 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.List;
48 import java.util.Optional;
49 import java.util.function.Predicate;
51 public class ContrailPortToNetResourceConnection extends ResourceConnectionUsingRequirementHelper {
52 private MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
54 public ContrailPortToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
55 TranslateTo translateTo, FileData nestedFileData,
56 NodeTemplate substitutionNodeTemplate,
58 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
62 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
63 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
64 ToscaServiceModel toscaServiceModel =
65 HeatToToscaUtil.getToscaServiceModel(translateTo.getContext());
66 return toscaAnalyzerService
67 .isTypeOf(nodeTemplate, ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE,
68 translateTo.getContext().getTranslatedServiceTemplates()
69 .get(translateTo.getResource().getType()), toscaServiceModel);
73 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
74 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
76 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE)
77 && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
78 && req.getRelationship()
79 .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO));
84 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
85 Resource heatResource,
86 HeatOrchestrationTemplate
87 nestedHeatOrchestrationTemplate,
88 String nestedHeatFileName) {
90 mdcDataDebugMessage.debugEntryMessage(null, null);
92 Object interfaceListProperty =
93 heatResource.getProperties().get(HeatConstants.INTERFACE_LIST_PROPERTY_NAME);
94 if (interfaceListProperty == null) {
95 return Optional.empty();
97 List<String> paramsList = new ArrayList<>();
98 if (interfaceListProperty instanceof List) {
99 for (int index = 0; index < ((List) interfaceListProperty).size(); index++) {
100 Object interfaceEntry = ((List) interfaceListProperty).get(index);
101 if (interfaceEntry instanceof Map) {
102 Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil
103 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
104 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());
113 mdcDataDebugMessage.debugExitMessage(null, null);
114 return Optional.of(paramsList);
115 } else if (interfaceListProperty instanceof Map) {
116 Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil
117 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
118 translateTo.getContext(),
119 ((Map) interfaceListProperty).get(HeatConstants.VIRTUAL_NETWORK_PROPERTY_NAME));
120 if (attachedVirtualNetwork.isPresent() && attachedVirtualNetwork.get().isGetParam()
121 && attachedVirtualNetwork.get().getEntityId() instanceof String) {
122 paramsList.add((String) attachedVirtualNetwork.get().getEntityId());
123 return Optional.of(paramsList);
127 mdcDataDebugMessage.debugExitMessage(null, null);
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) {
142 mdcDataDebugMessage.debugEntryMessage(null, null);
144 if (paramNames == null || paramNames.isEmpty()) {
147 Integer index = Integer.valueOf(
148 requirementDefinitionEntry.getKey().substring("link_port_".length()).substring(0, 1));
150 String paramName = paramNames.get(
151 index); // port can connect to one network only and we are
152 // expecting to get only one param(unlike security rules to port)
153 Object paramValue = translateTo.getResource().getProperties().get(paramName);
154 List<String> supportedNetworkTypes =
155 Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
156 HeatResourcesTypes.CONTRAIL_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
158 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
159 supportedNetworkTypes);
161 mdcDataDebugMessage.debugExitMessage(null, null);
165 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
166 final String nestedPropertyName,
167 String connectionPointId,
168 Resource connectedResource,
169 List<String> supportedTypes) {
172 mdcDataDebugMessage.debugEntryMessage(null, null);
174 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
175 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
176 + "' is pointing to a resource with type '" + connectedResource.getType()
177 + "' which is not supported for requirement '" + connectionPointId
178 + "' that connect contrail port to network. Supported types are: '"
179 + supportedTypes.toString()
180 + "', therefore, this TOSCA requirement will not be connected.");
182 mdcDataDebugMessage.debugExitMessage(null, null);
186 mdcDataDebugMessage.debugExitMessage(null, null);