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