re base 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 / ContrailPortToNetResourceConnection.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 com.google.common.collect.ImmutableList;
20 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
21 import org.onap.sdc.tosca.datatypes.model.NodeType;
22 import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
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.heat.services.HeatConstants;
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.ToscaServiceModel;
32 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
33 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Optional;
42 import java.util.function.Predicate;
43
44 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_CONTRAIL_PORT_NETWORK_REQUIREMENT_CONNECTION;
45
46 public class ContrailPortToNetResourceConnection extends ResourceConnectionUsingRequirementHelper {
47     ContrailPortToNetResourceConnection(ResourceTranslationBase resourceTranslationBase,
48                                         TranslateTo translateTo, FileData nestedFileData,
49                                         NodeTemplate substitutionNodeTemplate,
50                                         NodeType nodeType) {
51         super(resourceTranslationBase, translateTo, nestedFileData, substitutionNodeTemplate, nodeType);
52     }
53
54     @Override
55     protected boolean isDesiredNodeTemplateType(NodeTemplate nodeTemplate) {
56         ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
57         ToscaServiceModel toscaServiceModel =
58                 HeatToToscaUtil.getToscaServiceModel(translateTo.getContext());
59         return toscaAnalyzerService
60                 .isTypeOf(nodeTemplate, ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE,
61                         translateTo.getContext().getTranslatedServiceTemplates()
62                                 .get(translateTo.getResource().getType()), toscaServiceModel);
63     }
64
65     @Override
66     protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
67         ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
68         predicates.add(
69                 req -> req.getCapability().equals(ToscaCapabilityType.NATIVE_NETWORK_LINKABLE)
70                         && (req.getNode() == null || req.getNode().equals(ToscaNodeType.NATIVE_ROOT))
71                         && req.getRelationship()
72                         .equals(ToscaRelationshipType.NATIVE_NETWORK_LINK_TO));
73         return predicates;
74     }
75
76     @Override
77     protected Optional<List<String>> getConnectorPropertyParamName(String heatResourceId,
78                                                                    Resource heatResource,
79                                                                    HeatOrchestrationTemplate
80                                                                            nestedHeatOrchestrationTemplate,
81                                                                    String nestedHeatFileName) {
82         Object interfaceListProperty = heatResource.getProperties().get(HeatConstants.INTERFACE_LIST_PROPERTY_NAME);
83         if (interfaceListProperty == null) {
84             return Optional.empty();
85         }
86         List<String> paramsList = new ArrayList<>();
87         if (interfaceListProperty instanceof List) {
88             return getConnectorPropertyParamNameFromList(nestedHeatOrchestrationTemplate, nestedHeatFileName,
89                     (List) interfaceListProperty, paramsList);
90         } else if (interfaceListProperty instanceof Map) {
91             return getConnectorPropertyParamNameFromMap(nestedHeatOrchestrationTemplate, nestedHeatFileName,
92                     (Map) interfaceListProperty, paramsList);
93         }
94         return Optional.empty();
95     }
96
97     private Optional<List<String>> getConnectorPropertyParamNameFromList(
98             HeatOrchestrationTemplate nestedHeatOrchestrationTemplate, String nestedHeatFileName,
99             List interfaceListProperty, List<String> paramsList) {
100         for (Object interfaceEntry : interfaceListProperty) {
101             if (interfaceEntry instanceof Map) {
102                 Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil.extractAttachedResourceId(
103                         nestedHeatFileName, nestedHeatOrchestrationTemplate, translateTo.getContext(),
104                         ((Map) interfaceEntry).get(HeatConstants.VIRTUAL_NETWORK_PROPERTY_NAME));
105                 if (attachedVirtualNetwork.isPresent() && attachedVirtualNetwork.get().isGetParam()
106                         && attachedVirtualNetwork.get().getEntityId() instanceof String) {
107                     paramsList.add((String) attachedVirtualNetwork.get().getEntityId());
108                 }
109             }
110         }
111         return Optional.of(paramsList);
112     }
113
114     private Optional<List<String>> getConnectorPropertyParamNameFromMap(HeatOrchestrationTemplate
115                                                                                 nestedHeatOrchestrationTemplate,
116                                                                         String nestedHeatFileName,
117                                                                         Map interfaceListProperty,
118                                                                         List<String> paramsList) {
119         Optional<AttachedResourceId> attachedVirtualNetwork = HeatToToscaUtil
120                 .extractAttachedResourceId(nestedHeatFileName, nestedHeatOrchestrationTemplate,
121                         translateTo.getContext(),
122                         interfaceListProperty.get(HeatConstants.VIRTUAL_NETWORK_PROPERTY_NAME));
123         if (attachedVirtualNetwork.isPresent() && attachedVirtualNetwork.get().isGetParam()
124                 && attachedVirtualNetwork.get().getEntityId() instanceof String) {
125             paramsList.add((String) attachedVirtualNetwork.get().getEntityId());
126             return Optional.of(paramsList);
127         }
128         return Optional.empty();
129     }
130
131     @Override
132     protected String getDesiredResourceType() {
133         return HeatResourcesTypes.CONTRAIL_SERVICE_INSTANCE.getHeatResource();
134     }
135
136     @Override
137     protected void addRequirementToConnectResources(
138             Map.Entry<String, RequirementDefinition> requirementDefinitionEntry,
139             List<String> paramNames) {
140         if (paramNames == null || paramNames.isEmpty()) {
141             return;
142         }
143         Integer index = Integer.valueOf(
144                 requirementDefinitionEntry.getKey().substring("link_port_".length()).substring(0, 1));
145
146         String paramName = paramNames.get(
147                 index); // port can connect to one network only and we are
148         // expecting to get only one param(unlike security rules to port)
149         Object paramValue = translateTo.getResource().getProperties().get(paramName);
150         List<String> supportedNetworkTypes =
151                 ImmutableList.of(HeatResourcesTypes.NEUTRON_NET_RESOURCE_TYPE.getHeatResource(),
152                         HeatResourcesTypes.CONTRAIL_VIRTUAL_NETWORK_RESOURCE_TYPE.getHeatResource());
153
154         addRequirementToConnectResource(requirementDefinitionEntry, paramName, paramValue,
155                 supportedNetworkTypes);
156     }
157
158     @Override
159     boolean validateResourceTypeSupportedForReqCreation(String nestedResourceId,
160                                                         final String nestedPropertyName,
161                                                         String connectionPointId,
162                                                         Resource connectedResource,
163                                                         List<String> supportedTypes) {
164         if (resourceTranslationBase.isUnsupportedResourceType(connectedResource, supportedTypes)) {
165             logger.warn(LOG_UNSUPPORTED_CONTRAIL_PORT_NETWORK_REQUIREMENT_CONNECTION, nestedResourceId,
166                     nestedPropertyName, connectedResource.getType(), connectionPointId, supportedTypes.toString());
167             return false;
168         }
169         return true;
170     }
171
172
173 }