Refactor Onboarding Translator code
[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  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Optional;
24 import java.util.function.Predicate;
25
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;
42
43
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,
49                 nodeType);
50     }
51
52     @Override
53     protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
54         return nodeTemplate.getType().equals(ToscaNodeType.NEUTRON_PORT);
55     }
56
57     @Override
58     protected List<Predicate<CapabilityDefinition>> getPredicatesListForConnectionPoints() {
59         ArrayList<Predicate<CapabilityDefinition>> predicates = new ArrayList<>(1);
60         predicates.add(cap -> cap.getType().equals(ToscaCapabilityType.NATIVE_ATTACHMENT));
61         return predicates;
62     }
63
64     @Override
65     protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
66                                                                    Resource heatResource,
67                                                                    HeatOrchestrationTemplate
68                                                                            nestedHeatOrchestrationTemplate,
69                                                                    String nestedHeatFileName) {
70
71
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());
84                 }
85             });
86
87             return Optional.of(paramsList);
88         }
89
90         return Optional.empty();
91     }
92
93     @Override
94     protected String getDesiredResourceType() {
95         return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
96     }
97
98     @Override
99     void addRequirementToConnectResources(
100             Map.Entry<String, CapabilityDefinition> connectionPointEntry, List<String> paramNames) {
101
102
103         if (paramNames == null || paramNames.isEmpty()) {
104             return;
105         }
106         List<String> supportedSecurityRulesTypes = Collections
107                 .singletonList(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
108
109         for (String paramName : paramNames) {
110             addRequirementToConnectResource(connectionPointEntry, supportedSecurityRulesTypes, paramName);
111         }
112
113     }
114
115     @Override
116     boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
117                                                         String nestedPropertyName,
118                                                         String connectionPointId,
119                                                         Resource connectedResource,
120                                                         List<String> supportedTypes) {
121
122
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());
129
130             return false;
131         }
132
133         return true;
134     }
135
136     @Override
137     Map.Entry<String, RequirementDefinition> createRequirementDefinition(String capabilityKey) {
138
139
140         RequirementDefinition definition = new RequirementDefinition();
141         definition.setCapability(capabilityKey);
142         definition.setRelationship(ToscaRelationshipType.ATTACHES_TO);
143         return new Map.Entry<String, RequirementDefinition>() {
144             @Override
145             public String getKey() {
146                 return ToscaConstants.PORT_REQUIREMENT_ID;
147             }
148
149             @Override
150             public RequirementDefinition getValue() {
151                 return definition;
152             }
153
154             @Override
155             public RequirementDefinition setValue(RequirementDefinition value) {
156                 return null;
157             }
158         };
159     }
160
161
162 }