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;
23 import org.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.heat.datatypes.model.Resource;
25 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
26 import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
27 import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
28 import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
29 import org.openecomp.sdc.tosca.services.DataModelUtil;
30 import org.openecomp.sdc.tosca.services.ToscaConstants;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
33 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
34 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
35 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 import java.util.Optional;
41 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
42 protected static Logger logger =
43 LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
46 protected void translate(TranslateTo translateTo) {
47 String heatFileName = translateTo.getHeatFileName();
48 String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
49 if (translatedNetworkResourceId == null) {
53 NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
54 if (policyNodeTemplate != null) {
56 .addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
57 createRequirementAssignment(translatedNetworkResourceId));
61 private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
62 String heatFileName) {
63 Optional<AttachedResourceId> attachedPolicyResourceId =
64 extractAttachedResourceIdHandleMissing(translateTo, "policy");
65 NodeTemplate policyNodeTemplate = new NodeTemplate();
66 Optional<String> policyResourceId =
67 HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId.get());
68 if (policyResourceId.isPresent()) {
69 policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
71 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
72 + translateTo.getResource().getType()
73 + "' include 'policy' property without 'get_attr' of 'fq_name'/'get_resource'"
74 + " function, therefore this resource will be ignored in TOSCA translation.");
76 return policyNodeTemplate;
79 private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
80 String policyResourceId) {
81 Resource policyResource = HeatToToscaUtil
82 .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
83 Optional<String> translatedPolicyResourceId =
84 ResourceTranslationFactory.getInstance(policyResource)
85 .translateResource(heatFileName, translateTo.getServiceTemplate(),
86 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
87 translateTo.getContext());
88 if (!translatedPolicyResourceId.isPresent()) {
89 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
90 + translateTo.getResource().getType()
91 + "' include unsupported policy resource, therefore this "
92 + "resource will be ignored in TOSCA translation. ");
96 .getNodeTemplate(translateTo.getServiceTemplate(),
97 translatedPolicyResourceId.get());
100 private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
101 Optional<AttachedResourceId> attachedNetworkResourceId =
102 extractAttachedResourceIdHandleMissing(translateTo, "network");
104 String translatedNetworkResourceId = null;
105 if (attachedNetworkResourceId.get().isGetResource()) {
106 translatedNetworkResourceId = (String) attachedNetworkResourceId.get().getTranslatedId();
108 logger.warn("Heat resource: '" + translateTo.getResourceId() + "' with type: '"
109 + translateTo.getResource().getType()
110 + "' include 'network' property without 'get_resource' function, "
111 + "therefore this resource will be ignored in TOSCA translation.");
113 return translatedNetworkResourceId;
116 private RequirementAssignment createRequirementAssignment(String translatedNetworkResourceId) {
117 RequirementAssignment requirement = new RequirementAssignment();
118 requirement.setCapability(ToscaCapabilityType.ATTACHMENT.getDisplayName());
119 requirement.setNode(translatedNetworkResourceId);
120 requirement.setRelationship(ToscaRelationshipType.ATTACHES_TO.getDisplayName());
124 private Optional<AttachedResourceId> extractAttachedResourceIdHandleMissing(
125 TranslateTo translateTo, String propertyName) {
126 Optional<AttachedResourceId> attachedResourceId =
127 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
129 if (!attachedResourceId.isPresent()) {
130 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
132 return attachedResourceId;
137 protected String generateTranslatedId(TranslateTo translateTo) {
138 return extractAttachedResourceIdHandleMissing(translateTo, "network").get().getEntityId()