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.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;
45 import java.util.Optional;
47 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
48 protected static Logger logger =
49 (Logger) LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
52 protected void translate(TranslateTo translateTo) {
53 String heatFileName = translateTo.getHeatFileName();
54 String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
55 if (translatedNetworkResourceId == null) {
59 NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
60 if (policyNodeTemplate != null) {
62 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
63 createRequirementAssignment(translatedNetworkResourceId));
68 protected String generateTranslatedId(TranslateTo translateTo) {
69 return extractAttachedResourceIdHandleMissing(translateTo, "network").getEntityId()
74 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
75 TranslateTo translateTo) {
76 return Optional.empty();
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());
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.");
94 return policyNodeTemplate;
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. ");
114 .getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
117 private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
118 AttachedResourceId attachedNetworkResourceId =
119 extractAttachedResourceIdHandleMissing(translateTo, "network");
121 String translatedNetworkResourceId = null;
122 if (attachedNetworkResourceId.isGetResource()) {
123 translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
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.");
130 return translatedNetworkResourceId;
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);
141 private AttachedResourceId extractAttachedResourceIdHandleMissing(
142 TranslateTo translateTo, String propertyName) {
143 Optional<AttachedResourceId> attachedResourceId =
144 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
146 if (!attachedResourceId.isPresent()) {
147 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
149 return attachedResourceId.get();