Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / SecurityRulesToPortResourceConnection.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
22
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.tosca.datatypes.ToscaCapabilityType;
28 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
29 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
30 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
31 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
32 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
33 import org.openecomp.sdc.tosca.datatypes.model.RequirementDefinition;
34 import org.openecomp.sdc.tosca.services.ToscaConstants;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
37 import org.openecomp.sdc.translator.services.heattotosca.Constants;
38 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Optional;
45 import java.util.function.Predicate;
46
47
48 class SecurityRulesToPortResourceConnection extends ResourceConnectionUsingCapabilityHelper {
49   SecurityRulesToPortResourceConnection(ResourceTranslationNestedImpl resourceTranslationNested,
50                                         TranslateTo translateTo, FileData nestedFileData,
51                                         NodeTemplate substitutionNodeTemplate, NodeType nodeType) {
52     super(resourceTranslationNested, translateTo, nestedFileData, substitutionNodeTemplate,
53         nodeType);
54   }
55
56   @Override
57   protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
58     return nodeTemplate.getType().equals(ToscaNodeType.NEUTRON_PORT);
59   }
60
61   @Override
62   protected List<Predicate<CapabilityDefinition>> getPredicatesListForConnectionPoints() {
63     ArrayList<Predicate<CapabilityDefinition>> predicates = new ArrayList<>();
64     predicates.add(cap -> cap.getType().equals(ToscaCapabilityType.NATIVE_ATTACHMENT));
65     return predicates;
66   }
67
68   @Override
69   protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
70                                                                  Resource heatResource,
71                                                                  HeatOrchestrationTemplate
72                                                                  nestedHeatOrchestrationTemplate,
73                                                                  String nestedHeatFileName) {
74
75
76     mdcDataDebugMessage.debugEntryMessage(null, null);
77
78     Object securityGroups =
79         heatResource.getProperties().get(Constants.SECURITY_GROUPS_PROPERTY_NAME);
80     List<String> paramsList = new ArrayList<>();
81     if (securityGroups instanceof List) {
82       ((List) securityGroups).forEach(group -> {
83         Optional<AttachedResourceId> attachedResourceId = HeatToToscaUtil
84             .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
85                 translateTo.getContext(), group);
86         if (attachedResourceId.isPresent()
87             && attachedResourceId.get().isGetParam()
88             && attachedResourceId.get().getEntityId() instanceof String) {
89           paramsList.add((String) attachedResourceId.get().getEntityId());
90         }
91       });
92
93       mdcDataDebugMessage.debugExitMessage(null, null);
94       return Optional.of(paramsList);
95     }
96
97     mdcDataDebugMessage.debugExitMessage(null, null);
98     return Optional.empty();
99   }
100
101   @Override
102   protected String getDesiredResourceType() {
103     return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
104   }
105
106   @Override
107   void addRequirementToConnectResources(
108       Map.Entry<String, CapabilityDefinition> connectionPointEntry, List<String> paramNames) {
109
110
111     mdcDataDebugMessage.debugEntryMessage(null, null);
112
113     if (paramNames == null || paramNames.isEmpty()) {
114       return;
115     }
116     List<String> supportedSecurityRulesTypes = Collections
117         .singletonList(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
118
119     for (String paramName : paramNames) {
120       addRequirementToConnectResource(connectionPointEntry, supportedSecurityRulesTypes, paramName);
121     }
122
123     mdcDataDebugMessage.debugExitMessage(null, null);
124   }
125
126   @Override
127   boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
128                                                       String nestedPropertyName,
129                                                       String connectionPointId,
130                                                       Resource connectedResource,
131                                                       List<String> supportedTypes) {
132
133
134     mdcDataDebugMessage.debugEntryMessage(null, null);
135
136     if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
137       logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
138           + "' is pointing to resource with type '" + connectedResource.getType()
139           + "' which is not supported for capability '" + connectionPointId
140           + "' connection, (security rules to port connection). Supported types are: '"
141           + supportedTypes.toString() + "', therefore, this TOSCA capability will not be "
142           + "connected.");
143
144       mdcDataDebugMessage.debugExitMessage(null, null);
145       return false;
146     }
147
148     mdcDataDebugMessage.debugExitMessage(null, null);
149     return true;
150   }
151
152   @Override
153   Map.Entry<String, RequirementDefinition> createRequirementDefinition(String capabilityKey) {
154
155
156     mdcDataDebugMessage.debugEntryMessage(null, null);
157
158     RequirementDefinition definition = new RequirementDefinition();
159     definition.setCapability(capabilityKey);
160     definition.setRelationship(ToscaRelationshipType.ATTACHES_TO);
161     mdcDataDebugMessage.debugExitMessage(null, null);
162     return new Map.Entry<String, RequirementDefinition>() {
163       @Override
164       public String getKey() {
165         return ToscaConstants.PORT_REQUIREMENT_ID;
166       }
167
168       @Override
169       public RequirementDefinition getValue() {
170         return definition;
171       }
172
173       @Override
174       public RequirementDefinition setValue(RequirementDefinition value) {
175         return null;
176       }
177     };
178   }
179
180
181 }