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