2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
19 import static org.openecomp.sdc.heat.services.HeatConstants.NETWORK_PROPERTY_NAME;
20 import static org.openecomp.sdc.tosca.services.DataModelUtil.createAttachmentRequirementAssignment;
21 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_POLICY_NETWORK_PROPERTY;
22 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_POLICY_PROPERTY_GET_ATTR;
23 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.LOG_UNSUPPORTED_POLICY_RESOURCE;
25 import java.util.Optional;
27 import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.heat.datatypes.model.Resource;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
33 import org.openecomp.sdc.tosca.services.DataModelUtil;
34 import org.openecomp.sdc.tosca.services.ToscaConstants;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
37 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
38 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
39 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
41 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
43 protected static Logger logger = 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) {
55 DataModelUtil.addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
56 createAttachmentRequirementAssignment(translatedNetworkResourceId));
61 protected String generateTranslatedId(TranslateTo translateTo) {
62 return extractAttachedResourceIdHandleMissing(translateTo, NETWORK_PROPERTY_NAME).getEntityId().toString();
66 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
67 TranslateTo translateTo) {
68 return Optional.empty();
71 private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
72 String heatFileName) {
73 AttachedResourceId attachedPolicyResourceId = extractAttachedResourceIdHandleMissing(translateTo, "policy");
74 NodeTemplate policyNodeTemplate = new NodeTemplate();
75 Optional<String> policyResourceId =
76 HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId);
77 if (policyResourceId.isPresent()) {
78 policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
80 logger.warn(LOG_UNSUPPORTED_POLICY_PROPERTY_GET_ATTR, translateTo.getResourceId(),
81 translateTo.getResource().getType());
83 return policyNodeTemplate;
86 private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
87 String policyResourceId) {
88 Resource policyResource = HeatToToscaUtil
89 .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
90 Optional<String> translatedPolicyResourceId =
91 ResourceTranslationFactory.getInstance(policyResource)
92 .translateResource(heatFileName, translateTo.getServiceTemplate(),
93 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
94 translateTo.getContext());
95 if (!translatedPolicyResourceId.isPresent()) {
96 logger.warn(LOG_UNSUPPORTED_POLICY_RESOURCE, translateTo.getResourceId(), translateTo.getResource().getType());
99 return DataModelUtil.getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
102 private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
103 AttachedResourceId attachedNetworkResourceId = extractAttachedResourceIdHandleMissing(translateTo,
104 NETWORK_PROPERTY_NAME);
106 String translatedNetworkResourceId = null;
107 if (attachedNetworkResourceId.isGetResource()) {
108 translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
110 logger.warn(LOG_UNSUPPORTED_POLICY_NETWORK_PROPERTY, translateTo.getResourceId(),
111 translateTo.getResource().getType());
113 return translatedNetworkResourceId;
116 private AttachedResourceId extractAttachedResourceIdHandleMissing(
117 TranslateTo translateTo, String propertyName) {
118 Optional<AttachedResourceId> attachedResourceId =
119 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
121 if (!attachedResourceId.isPresent()) {
122 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
124 return attachedResourceId.get();