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.heat.datatypes.manifest.FileData;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.heat.services.HeatConstants;
29 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
30 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
31 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
32 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
33 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
34 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
35 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
36 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
37 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
38 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
39 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
40 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
41 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.List;
47 import java.util.Optional;
48 import java.util.function.Predicate;
50 public class ContrailPortToNetResourceConnection extends ResourceConnectionUsingRequirementHelper {
51 private MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
53 public ContrailPortToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
54 TranslateTo translateTo, FileData nestedFileData,
55 NodeTemplate substitutionNodeTemplate,
57 super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
61 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
62 ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
63 ToscaServiceModel toscaServiceModel =
64 HeatToToscaUtil.getToscaServiceModel(translateTo.getContext());
65 return toscaAnalyzerService
66 .isTypeOf(nodeTemplate, ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE,
67 translateTo.getContext().getTranslatedServiceTemplates()
68 .get(translateTo.getResource().getType()), toscaServiceModel);
72 protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
73 ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
75 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE)
76 && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
77 && req.getRelationship()
78 .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO));
83 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
84 Resource heatResource,
85 HeatOrchestrationTemplate
86 nestedHeatOrchestrationTemplate,
87 String nestedHeatFileName) {
89 mdcDataDebugMessage.debugEntryMessage(null, null);
91 Object interfaceListProperty =
92 heatResource.getProperties().get(HeatConstants.INTERFACE_LIST_PROPERTY_NAME);
93 if (interfaceListProperty == null) {
94 return Optional.empty();
96 List<String> paramsList = new ArrayList<>();
97 if (interfaceListProperty instanceof List) {
98 for (int index = 0; index < ((List) interfaceListProperty).size(); index++) {
99 Object interfaceEntry = ((List) interfaceListProperty).get(index);
100 if (interfaceEntry instanceof Map) {
101 Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil
102 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
103 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());
112 mdcDataDebugMessage.debugExitMessage(null, null);
113 return Optional.of(paramsList);
114 } else if (interfaceListProperty instanceof Map) {
115 Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil
116 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
117 translateTo.getContext(),
118 ((Map) interfaceListProperty).get(HeatConstants.VIRTUAL_NETWORK_PROPERTY_NAME));
119 if (attachedVirtualNetwork.isPresent() && attachedVirtualNetwork.get().isGetParam()
120 && attachedVirtualNetwork.get().getEntityId() instanceof String) {
121 paramsList.add((String) attachedVirtualNetwork.get().getEntityId());
122 return Optional.of(paramsList);
126 mdcDataDebugMessage.debugExitMessage(null, null);
127 return Optional.empty();
131 protected String getDesiredResourceType() {
132 return HeatResourcesTypes.CONTRAIL_SERVICE_INSTANCE.getHeatResource();
136 protected void addRequirementToConnectResources(
137 Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
138 List<String> paramNames) {
141 mdcDataDebugMessage.debugEntryMessage(null, null);
143 if (paramNames == null || paramNames.isEmpty()) {
146 Integer index = Integer.valueOf(
147 requirementDefinitionEntry.getKey().substring("link_port_".length()).substring(0, 1));
149 String paramName = paramNames.get(
150 index); // port can connect to one network only and we are
151 // expecting to get only one param(unlike security rules to port)
152 Object paramValue = translateTo.getResource().getProperties().get(paramName);
153 List<String> supportedNetworkTypes =
154 Arrays.asList(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
155 HeatResourcesTypes.CONTRAIL_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
157 addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
158 supportedNetworkTypes);
160 mdcDataDebugMessage.debugExitMessage(null, null);
164 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
165 final String nestedPropertyName,
166 String connectionPointId,
167 Resource connectedResource,
168 List<String> supportedTypes) {
171 mdcDataDebugMessage.debugEntryMessage(null, null);
173 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
174 logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
175 + "' is pointing to a resource with type '" + connectedResource.getType()
176 + "' which is not supported for requirement '" + connectionPointId
177 + "' that connect contrail port to network. Supported types are: '"
178 + supportedTypes.toString()
179 + "', therefore, this TOSCA requirement will not be connected.");
181 mdcDataDebugMessage.debugExitMessage(null, null);
185 mdcDataDebugMessage.debugExitMessage(null, null);