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