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 org.onap.sdc.tosca.datatypes.model.NodeTemplate;
20 import org.openecomp.sdc.common.errors.CoreException;
21 import org.openecomp.sdc.heat.datatypes.model.Resource;
22 import org.openecomp.sdc.logging.api.Logger;
23 import org.openecomp.sdc.logging.api.LoggerFactory;
24 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
25 import org.openecomp.sdc.tosca.services.DataModelUtil;
26 import org.openecomp.sdc.tosca.services.ToscaConstants;
27 import org.openecomp.sdc.translator.datatypes.heattotosca.AttachedResourceId;
28 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
29 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
30 import org.openecomp.sdc.translator.services.heattotosca.ResourceTranslationFactory;
31 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
33 import java.util.Optional;
35 import static org.openecomp.sdc.heat.services.HeatConstants.NETWORK_PROPERTY_NAME;
36 import static org.openecomp.sdc.tosca.services.DataModelUtil.createAttachmentRequirementAssignment;
37 import static org.openecomp.sdc.translator.services.heattotosca.HeatToToscaLogConstants.*;
39 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
41 protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
44 protected void translate(TranslateTo translateTo) {
45 String heatFileName = translateTo.getHeatFileName();
46 String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
47 if (translatedNetworkResourceId == null) {
51 NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
52 if (policyNodeTemplate != null) {
53 DataModelUtil.addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
54 createAttachmentRequirementAssignment(translatedNetworkResourceId));
59 protected String generateTranslatedId(TranslateTo translateTo) {
60 return extractAttachedResourceIdHandleMissing(translateTo, NETWORK_PROPERTY_NAME).getEntityId().toString();
64 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
65 TranslateTo translateTo) {
66 return Optional.empty();
69 private NodeTemplate getTranslatedPolicyNodeTemplate(TranslateTo translateTo,
70 String heatFileName) {
71 AttachedResourceId attachedPolicyResourceId = extractAttachedResourceIdHandleMissing(translateTo, "policy");
72 NodeTemplate policyNodeTemplate = new NodeTemplate();
73 Optional<String> policyResourceId =
74 HeatToToscaUtil.getContrailAttachedHeatResourceId(attachedPolicyResourceId);
75 if (policyResourceId.isPresent()) {
76 policyNodeTemplate = getPolicyNodeTemplate(translateTo, heatFileName, policyResourceId.get());
78 logger.warn(LOG_UNSUPPORTED_POLICY_PROPERTY_GET_ATTR, translateTo.getResourceId(),
79 translateTo.getResource().getType());
81 return policyNodeTemplate;
84 private NodeTemplate getPolicyNodeTemplate(TranslateTo translateTo, String heatFileName,
85 String policyResourceId) {
86 Resource policyResource = HeatToToscaUtil
87 .getResource(translateTo.getHeatOrchestrationTemplate(), policyResourceId, heatFileName);
88 Optional<String> translatedPolicyResourceId =
89 ResourceTranslationFactory.getInstance(policyResource)
90 .translateResource(heatFileName, translateTo.getServiceTemplate(),
91 translateTo.getHeatOrchestrationTemplate(), policyResource, policyResourceId,
92 translateTo.getContext());
93 if (!translatedPolicyResourceId.isPresent()) {
94 logger.warn(LOG_UNSUPPORTED_POLICY_RESOURCE, translateTo.getResourceId(), translateTo.getResource().getType());
97 return DataModelUtil.getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
100 private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
101 AttachedResourceId attachedNetworkResourceId = extractAttachedResourceIdHandleMissing(translateTo,
102 NETWORK_PROPERTY_NAME);
104 String translatedNetworkResourceId = null;
105 if (attachedNetworkResourceId.isGetResource()) {
106 translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
108 logger.warn(LOG_UNSUPPORTED_POLICY_NETWORK_PROPERTY, translateTo.getResourceId(),
109 translateTo.getResource().getType());
111 return translatedNetworkResourceId;
114 private AttachedResourceId extractAttachedResourceIdHandleMissing(
115 TranslateTo translateTo, String propertyName) {
116 Optional<AttachedResourceId> attachedResourceId =
117 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
119 if (!attachedResourceId.isPresent()) {
120 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
122 return attachedResourceId.get();