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