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