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.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;
40 import java.util.Optional;
42 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
44 protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
47 protected void translate(TranslateTo translateTo) {
48 String heatFileName = translateTo.getHeatFileName();
49 String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
50 if (translatedNetworkResourceId == null) {
54 NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
55 if (policyNodeTemplate != null) {
57 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
58 createRequirementAssignment(translatedNetworkResourceId));
63 protected String generateTranslatedId(TranslateTo translateTo) {
64 return extractAttachedResourceIdHandleMissing(translateTo, "network").getEntityId()
69 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
70 TranslateTo translateTo) {
71 return Optional.empty();
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());
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());
88 return policyNodeTemplate;
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());
107 .getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
110 private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
111 AttachedResourceId attachedNetworkResourceId =
112 extractAttachedResourceIdHandleMissing(translateTo, "network");
114 String translatedNetworkResourceId = null;
115 if (attachedNetworkResourceId.isGetResource()) {
116 translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
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());
122 return translatedNetworkResourceId;
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);
133 private AttachedResourceId extractAttachedResourceIdHandleMissing(
134 TranslateTo translateTo, String propertyName) {
135 Optional<AttachedResourceId> attachedResourceId =
136 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
138 if (!attachedResourceId.isPresent()) {
139 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
141 return attachedResourceId.get();