Rename packages from openecomp to onap.
[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.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
31 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
32 import org.onap.sdc.tosca.datatypes.model.NodeType;
33 import org.onap.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
77     Object securityGroups =
78         heatResource.getProperties().get(Constants.SECURITY_GROUPS_PROPERTY_NAME);
79     List<String> paramsList = new ArrayList<>();
80     if (securityGroups instanceof List) {
81       ((List) securityGroups).forEach(group -> {
82         Optional<AttachedResourceId> attachedResourceId = HeatToToscaUtil
83             .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
84                 translateTo.getContext(), group);
85         if (attachedResourceId.isPresent()
86             && attachedResourceId.get().isGetParam()
87             && attachedResourceId.get().getEntityId() instanceof String) {
88           paramsList.add((String) attachedResourceId.get().getEntityId());
89         }
90       });
91
92       return Optional.of(paramsList);
93     }
94
95     return Optional.empty();
96   }
97
98   @Override
99   protected String getDesiredResourceType() {
100     return HeatResourcesTypes.NEUTRON_PORT_RESOURCE_TYPE.getHeatResource();
101   }
102
103   @Override
104   void addRequirementToConnectResources(
105       Map.Entry<String, CapabilityDefinition> connectionPointEntry, List<String> paramNames) {
106
107
108
109     if (paramNames == null || paramNames.isEmpty()) {
110       return;
111     }
112     List<String> supportedSecurityRulesTypes = Collections
113         .singletonList(HeatResourcesTypes.NEUTRON_SECURITY_GROUP_RESOURCE_TYPE.getHeatResource());
114
115     for (String paramName : paramNames) {
116       addRequirementToConnectResource(connectionPointEntry, supportedSecurityRulesTypes, paramName);
117     }
118
119   }
120
121   @Override
122   boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
123                                                       String nestedPropertyName,
124                                                       String connectionPointId,
125                                                       Resource connectedResource,
126                                                       List<String> supportedTypes) {
127
128
129
130     if (!resourceTranslationBase.isResourceTypeSupported(connectedResource, supportedTypes)) {
131       logger.warn("Nested resource '" + nestedResourceId + "' property '" + nestedPropertyName
132           + "' is pointing to resource with type '" + connectedResource.getType()
133           + "' which is not supported for capability '" + connectionPointId
134           + "' connection, (security rules to port connection). Supported types are: '"
135           + supportedTypes.toString() + "', therefore, this TOSCA capability will not be "
136           + "connected.");
137
138       return false;
139     }
140
141     return true;
142   }
143
144   @Override
145   Map.Entry<String, RequirementDefinition> createRequirementDefinition(String capabilityKey) {
146
147
148
149     RequirementDefinition definition = new RequirementDefinition();
150     definition.setCapability(capabilityKey);
151     definition.setRelationship(ToscaRelationshipType.ATTACHES_TO);
152     return new Map.Entry<String, RequirementDefinition>() {
153       @Override
154       public String getKey() {
155         return ToscaConstants.PORT_REQUIREMENT_ID;
156       }
157
158       @Override
159       public RequirementDefinition getValue() {
160         return definition;
161       }
162
163       @Override
164       public RequirementDefinition setValue(RequirementDefinition value) {
165         return null;
166       }
167     };
168   }
169
170
171 }