push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / ContrailV2VmInterfaceToNetResourceConnection.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;
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.datatypes.heattotosca.to.TranslatedHeatResource;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.Optional;
43 import java.util.function.Predicate;
44
45 public class ContrailV2VmInterfaceToNetResourceConnection extends PortToNetResourceConnection {
46
47   public ContrailV2VmInterfaceToNetResourceConnection(
48       ResourceTranslationBase resourceTranslationBase, TranslateTo translateTo,
49       FileData nestedFileData, 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()
56         .equals(ToscaNodeType.CONTRAILV2_VIRTUAL_MACHINE_INTERFACE.getDisplayName());
57   }
58
59   @Override
60   protected Optional<List<String>> getConnectorParamName(String heatResourceId,
61                                     Resource heatResource,
62                                     HeatOrchestrationTemplate nestedHeatOrchestrationTemplate) {
63     List<String> networks = new ArrayList<>();
64     Object virtualNetworkRefs = heatResource.getProperties().get("virtual_network_refs");
65     if (Objects.isNull(virtualNetworkRefs) || !(virtualNetworkRefs instanceof List)
66         || ((List) virtualNetworkRefs).size() == 0) {
67       return Optional.empty();
68     }
69     if (((List) virtualNetworkRefs).size() > 1) {
70       logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with nested heat file: '"
71           + translateTo.getResource().getType()
72           + "' has resource '" + heatResourceId + "' with type '"
73           + HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource()
74           + "' which include 'virtual_network_refs' property with more than one network values, "
75           + "only the first network will be translated, all rest will be ignored in TOSCA "
76           + "translation.");
77     }
78     Object virtualNetworkRef = ((List) virtualNetworkRefs).get(0);
79     Optional<AttachedResourceId> network = HeatToToscaUtil
80         .extractAttachedResourceId(nestedFileData.getFile(), nestedHeatOrchestrationTemplate,
81             translateTo.getContext(), virtualNetworkRef);
82     if (network.isPresent() && network.get().isGetParam()) {
83       networks.add((String) network.get().getEntityId());
84     }
85     return Optional.of(networks);
86   }
87
88   @Override
89   protected String getDesiredResourceType() {
90     return HeatResourcesTypes.CONTRAIL_V2_VIRTUAL_MACHINE_INTERFACE_RESOURCE_TYPE.getHeatResource();
91   }
92
93   @Override
94   protected void addRequirementToConnectResources(Map.Entry<String, RequirementDefinition> entry,
95                                                   List<String> paramNames) {
96     for (String paramName : paramNames) {
97       Object paramValue = translateTo.getResource().getProperties().get(paramName);
98       String contrailAttachedResourceId =
99           HeatToToscaUtil.extractContrailGetResourceAttachedHeatResourceId(paramValue);
100       Optional<String> node;
101       if (contrailAttachedResourceId != null) { // contrail get resource
102         node = ResourceTranslationBase.getResourceTranslatedId(translateTo.getHeatFileName(),
103             translateTo.getHeatOrchestrationTemplate(), contrailAttachedResourceId,
104             translateTo.getContext());
105         if (node.isPresent()) {
106           createRequirementAssignment(entry, node.get(), substitutionNodeTemplate);
107         }
108       } else {
109         Optional<AttachedResourceId> attachedResourceId =
110             HeatToToscaUtil.extractAttachedResourceId(translateTo, paramName);
111         if (!attachedResourceId.isPresent()) {
112           return;
113         }
114         AttachedResourceId resourceId = attachedResourceId.get();
115         if (resourceId.isGetParam()) {
116           TranslatedHeatResource shareResource =
117               translateTo.getContext().getHeatSharedResourcesByParam()
118                   .get(resourceId.getEntityId());
119           if (Objects.nonNull(shareResource)
120               && !HeatToToscaUtil.isHeatFileNested(translateTo, translateTo.getHeatFileName())) {
121             createRequirementAssignment(entry, shareResource.getTranslatedId(),
122                 substitutionNodeTemplate);
123           }
124         }
125       }
126     }
127   }
128
129   @Override
130   protected List<Predicate<RequirementDefinition>> getPredicatesListForConnectionPoints() {
131     ArrayList<Predicate<RequirementDefinition>> predicates = new ArrayList<>();
132     predicates.add(
133         req -> req.getCapability().equals(ToscaCapabilityType.NETWORK_LINKABLE.getDisplayName())
134             && req.getNode().equals(ToscaNodeType.ROOT.getDisplayName())
135             && req.getRelationship().equals(ToscaRelationshipType.NETWORK_LINK_TO.getDisplayName())
136     );
137     return predicates;
138   }
139 }