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