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 java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.Optional;
24 import java.util.function.Predicate;
26 import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
27 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
28 import org.onap.sdc.tosca.datatypes.model.NodeType;
29 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
30 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
31 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
32 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
33 import org.openecomp.sdc.heat.datatypes.model.Resource;
34 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
35 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
36 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
37 import org.openecomp.sdc.tosca.services.ToscaConstants;
38 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
39 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
40 import org.openecomp.sdc.translator.services.heattotosca.Constants;
41 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
44 class SecurityRulesToPortResourceConnection extends ResourceConnectionUsingCapabilityHelper {
45 SecurityRulesToPortResourceConnection(ResourceTranslationNestedImpl resourceTranslationNested,
46 TranslateTo translateTo, FileData nestedFileData,
47 NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
48 super(resourceTranslationNested, translateTo, nestedFileData, substitutionNodeTemplate,
53 protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
54 return nodeTemplate.getType().equals(ToscaNodeType.NEUTRON_PORT);
58 protected List<Predicate<CapabilityDefinition>> getPredicatesListForConnectionPoints() {
59 ArrayList<Predicate<CapabilityDefinition>> predicates = new ArrayList<>(1);
60 predicates.add(cap -> cap.getType().equals(ToscaCapabilityType.NATIVE_ATTACHMENT));
65 protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
66 Resource heatResource,
67 HeatOrchestrationTemplate
68 nestedHeatOrchestrationTemplate,
69 String nestedHeatFileName) {
72 Object securityGroups =
73 heatResource.getProperties().get(Constants.SECURITY_GROUPS_PROPERTY_NAME);
74 List<String> paramsList = new ArrayList<>();
75 if (securityGroups instanceof List) {
76 ((List) securityGroups).forEach(group -> {
77 Optional<AttachedResourceId> attachedResourceId = HeatToToscaUtil
78 .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
79 translateTo.getContext(), group);
80 if (attachedResourceId.isPresent()
81 && attachedResourceId.get().isGetParam()
82 && attachedResourceId.get().getEntityId() instanceof String) {
83 paramsList.add((String) attachedResourceId.get().getEntityId());
87 return Optional.of(paramsList);
90 return Optional.empty();
94 protected String getDesiredResourceType() {
95 return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
99 void addRequirementToConnectResources(
100 Map.Entry<String, CapabilityDefinition> connectionPointEntry, List<String> paramNames) {
103 if (paramNames == null || paramNames.isEmpty()) {
106 List<String> supportedSecurityRulesTypes = Collections
107 .singletonList(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
109 for (String paramName : paramNames) {
110 addRequirementToConnectResource(connectionPointEntry, supportedSecurityRulesTypes, paramName);
116 boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
117 String nestedPropertyName,
118 String connectionPointId,
119 Resource connectedResource,
120 List<String> supportedTypes) {
123 if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
124 logger.warn("Nested resource '{}' property '{}' is pointing to resource with type '{}' which is not "
125 + "supported for capability '{}' connection, (security rules to port connection)."
126 + "Supported types are: '{}', therefore, this TOSCA capability will not be connected.",
127 nestedResourceId, nestedPropertyName, connectedResource.getType(),
128 connectionPointId, supportedTypes.toString());
137 Map.Entry<String, RequirementDefinition> createRequirementDefinition(String capabilityKey) {
140 RequirementDefinition definition = new RequirementDefinition();
141 definition.setCapability(capabilityKey);
142 definition.setRelationship(ToscaRelationshipType.ATTACHES_TO);
143 return new Map.Entry<String, RequirementDefinition>() {
145 public String getKey() {
146 return ToscaConstants.PORT_REQUIREMENT_ID;
150 public RequirementDefinition getValue() {
155 public RequirementDefinition setValue(RequirementDefinition value) {