c6719afa9269566d6466c697f2e9aedc031fc0f4
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ResourceTranslationContrailAttachPolicyImpl.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.errors.CoreException;
24 import org.openecomp.sdc.heat.datatypes.model.Resource;
25 import org.openecomp.sdc.logging.api.Logger;
26 import org.openecomp.sdc.logging.api.LoggerFactory;
27 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
28 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
29 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
30 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
31 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
32 import org.openecomp.sdc.tosca.services.DataModelUtil;
33 import org.openecomp.sdc.tosca.services.ToscaConstants;
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 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
38 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
39
40 import java.util.Optional;
41
42 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
43
44   protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
45
46   @Override
47   protected void translate(TranslateTo translateTo) {
48     String heatFileName = translateTo.getHeatFileName();
49     String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
50     if (translatedNetworkResourceId == null) {
51       return;
52     }
53
54     NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
55     if (policyNodeTemplate != null) {
56       DataModelUtil
57           .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
58               createRequirementAssignment(translatedNetworkResourceId));
59     }
60   }
61
62   @Override
63   protected String generateTranslatedId(TranslateTo translateTo) {
64     return extractAttachedResourceIdHandleMissing(translateTo, "network").getEntityId()
65         .toString();
66   }
67
68   @Override
69   protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
70       TranslateTo translateTo) {
71     return Optional.empty();
72   }
73
74   private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
75                                                        String heatFileName) {
76     AttachedResourceId attachedPolicyResourceId =
77         extractAttachedResourceIdHandleMissing(translateTo, "policy");
78     NodeTemplate policyNodeTemplate = new NodeTemplate();
79     Optional<String> policyResourceId =
80         HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId);
81     if (policyResourceId.isPresent()) {
82       policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
83     } else {
84       logger.warn("Heat resource: '{}' with type: '{}' include 'policy' property without 'get_attr' of " +
85               "'fq_name'/'get_resource' function,  therefore this resource will be ignored in TOSCA translation.",
86               translateTo.getResourceId(), translateTo.getResource().getType());
87     }
88     return policyNodeTemplate;
89   }
90
91   private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
92                                              String policyResourceId) {
93     Resource policyResource = HeatToToscaUtil
94         .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
95     Optional<String> translatedPolicyResourceId =
96         ResourceTranslationFactory.getInstance(policyResource)
97             .translateResource(heatFileName, translateTo.getServiceTemplate(),
98                 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
99                 translateTo.getContext());
100     if (!translatedPolicyResourceId.isPresent()) {
101       logger.warn("Heat resource: '{}' with type: '{}' include unsupported policy resource, " +
102               "therefore this resource will be ignored in TOSCA translation.",
103               translateTo.getResourceId(), translateTo.getResource().getType());
104       return null;
105     }
106     return DataModelUtil
107         .getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
108   }
109
110   private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
111     AttachedResourceId attachedNetworkResourceId =
112         extractAttachedResourceIdHandleMissing(translateTo, "network");
113
114     String translatedNetworkResourceId = null;
115     if (attachedNetworkResourceId.isGetResource()) {
116       translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
117     } else {
118       logger.warn("Heat resource: '{}' with type: '{}' include 'network' property without 'get_resource' " +
119               "function, therefore this resource will be ignored in TOSCA translation.",
120               translateTo.getResourceId(), translateTo.getResource().getType());
121     }
122     return translatedNetworkResourceId;
123   }
124
125   private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
126     RequirementAssignment requirement = new RequirementAssignment();
127     requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
128     requirement.setNode(translatedNetworkResourceId);
129     requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
130     return requirement;
131   }
132
133   private AttachedResourceId extractAttachedResourceIdHandleMissing(
134       TranslateTo translateTo, String propertyName) {
135     Optional<AttachedResourceId> attachedResourceId =
136         HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
137
138     if (!attachedResourceId.isPresent()) {
139       throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
140     }
141     return attachedResourceId.get();
142   }
143 }