2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
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.context.impl.MdcDataErrorMessage;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerErrorCode;
31 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
34 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
35 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
36 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
37 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
38 import org.openecomp.sdc.tosca.services.DataModelUtil;
39 import org.openecomp.sdc.tosca.services.ToscaConstants;
40 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
41 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
42 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
43 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
44 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
46 import java.util.Optional;
48 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
49 protected static Logger logger =
50 (Logger) LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
53 protected void translate(TranslateTo translateTo) {
56 mdcDataDebugMessage.debugEntryMessage(null, null);
58 String heatFileName = translateTo.getHeatFileName();
59 String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
60 if (translatedNetworkResourceId == null) {
61 mdcDataDebugMessage.debugExitMessage(null, null);
65 NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
66 if (policyNodeTemplate != null) {
68 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
69 createRequirementAssignment(translatedNetworkResourceId));
72 mdcDataDebugMessage.debugExitMessage(null, null);
76 protected String generateTranslatedId(TranslateTo translateTo) {
77 return extractAttachedResourceIdHandleMissing(translateTo, "network").getEntityId()
82 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
83 TranslateTo translateTo) {
84 return Optional.empty();
87 private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
88 String heatFileName) {
91 mdcDataDebugMessage.debugEntryMessage(null, null);
93 AttachedResourceId attachedPolicyResourceId =
94 extractAttachedResourceIdHandleMissing(translateTo, "policy");
95 NodeTemplate policyNodeTemplate = new NodeTemplate();
96 Optional<String> policyResourceId =
97 HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId);
98 if (policyResourceId.isPresent()) {
99 policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
101 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
102 + translateTo.getResource().getType()
103 + "' include 'policy' property without 'get_attr' of 'fq_name'/'get_resource' function,"
104 + " therefore this resource will be ignored in TOSCA translation.");
107 mdcDataDebugMessage.debugExitMessage(null, null);
108 return policyNodeTemplate;
111 private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
112 String policyResourceId) {
115 mdcDataDebugMessage.debugEntryMessage(null, null);
117 Resource policyResource = HeatToToscaUtil
118 .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
119 Optional<String> translatedPolicyResourceId =
120 ResourceTranslationFactory.getInstance(policyResource)
121 .translateResource(heatFileName, translateTo.getServiceTemplate(),
122 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
123 translateTo.getContext());
124 if (!translatedPolicyResourceId.isPresent()) {
125 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
126 + translateTo.getResource().getType()
127 + "' include unsupported policy resource, therefore this resource will be ignored in "
128 + "TOSCA translation. ");
130 mdcDataDebugMessage.debugExitMessage(null, null);
134 mdcDataDebugMessage.debugExitMessage(null, null);
136 .getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
139 private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
142 mdcDataDebugMessage.debugEntryMessage(null, null);
144 AttachedResourceId attachedNetworkResourceId =
145 extractAttachedResourceIdHandleMissing(translateTo, "network");
147 String translatedNetworkResourceId = null;
148 if (attachedNetworkResourceId.isGetResource()) {
149 translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
151 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
152 + translateTo.getResource().getType()
153 + "' include 'network' property without 'get_resource' function, therefore this "
154 + "resource will be ignored in TOSCA translation.");
157 mdcDataDebugMessage.debugExitMessage(null, null);
158 return translatedNetworkResourceId;
161 private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
164 mdcDataDebugMessage.debugEntryMessage(null, null);
166 RequirementAssignment requirement = new RequirementAssignment();
167 requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
168 requirement.setNode(translatedNetworkResourceId);
169 requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
170 mdcDataDebugMessage.debugExitMessage(null, null);
174 private AttachedResourceId extractAttachedResourceIdHandleMissing(
175 TranslateTo translateTo, String propertyName) {
178 mdcDataDebugMessage.debugEntryMessage(null, null);
180 Optional<AttachedResourceId> attachedResourceId =
181 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
183 if (!attachedResourceId.isPresent()) {
184 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
185 LoggerTragetServiceName.CREATE_REQUIREMENT_ASSIGNMENT, ErrorLevel.ERROR.name(),
186 LoggerErrorCode.DATA_ERROR.getErrorCode(),
187 LoggerErrorDescription.MISSING_MANDATORY_PROPERTY);
188 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
191 mdcDataDebugMessage.debugExitMessage(null, null);
192 return attachedResourceId.get();