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