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) {
54 String heatFileName = translateTo.getHeatFileName();
55 String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
56 if (translatedNetworkResourceId == null) {
60 NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
61 if (policyNodeTemplate != null) {
63 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
64 createRequirementAssignment(translatedNetworkResourceId));
69 protected String generateTranslatedId(TranslateTo translateTo) {
70 return extractAttachedResourceIdHandleMissing(translateTo, "network").getEntityId()
75 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
76 TranslateTo translateTo) {
77 return Optional.empty();
80 private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
81 String heatFileName) {
82 AttachedResourceId attachedPolicyResourceId =
83 extractAttachedResourceIdHandleMissing(translateTo, "policy");
84 NodeTemplate policyNodeTemplate = new NodeTemplate();
85 Optional<String> policyResourceId =
86 HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId);
87 if (policyResourceId.isPresent()) {
88 policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
90 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
91 + translateTo.getResource().getType()
92 + "' include 'policy' property without 'get_attr' of 'fq_name'/'get_resource' function,"
93 + " therefore this resource will be ignored in TOSCA translation.");
95 return policyNodeTemplate;
98 private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
99 String policyResourceId) {
100 Resource policyResource = HeatToToscaUtil
101 .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
102 Optional<String> translatedPolicyResourceId =
103 ResourceTranslationFactory.getInstance(policyResource)
104 .translateResource(heatFileName, translateTo.getServiceTemplate(),
105 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
106 translateTo.getContext());
107 if (!translatedPolicyResourceId.isPresent()) {
108 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
109 + translateTo.getResource().getType()
110 + "' include unsupported policy resource, therefore this resource will be ignored in "
111 + "TOSCA translation. ");
115 .getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
118 private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
119 AttachedResourceId attachedNetworkResourceId =
120 extractAttachedResourceIdHandleMissing(translateTo, "network");
122 String translatedNetworkResourceId = null;
123 if (attachedNetworkResourceId.isGetResource()) {
124 translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
126 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
127 + translateTo.getResource().getType()
128 + "' include 'network' property without 'get_resource' function, therefore this "
129 + "resource will be ignored in TOSCA translation.");
131 return translatedNetworkResourceId;
134 private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
135 RequirementAssignment requirement = new RequirementAssignment();
136 requirement.setCapability(ToscaCapabilityType.NATIVE_ATTACHMENT);
137 requirement.setNode(translatedNetworkResourceId);
138 requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO);
142 private AttachedResourceId extractAttachedResourceIdHandleMissing(
143 TranslateTo translateTo, String propertyName) {
144 Optional<AttachedResourceId> attachedResourceId =
145 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
147 if (!attachedResourceId.isPresent()) {
148 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
149 LoggerTragetServiceName.CREATE_REQUIREMENT_ASSIGNMENT, ErrorLevel.ERROR.name(),
150 LoggerErrorCode.DATA_ERROR.getErrorCode(),
151 LoggerErrorDescription.MISSING_MANDATORY_PROPERTY);
152 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
154 return attachedResourceId.get();