3792f0eb54f9d70a81cc57d92ec56e4f9ef5daa2
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
18
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;
24
25 import java.util.Optional;
26
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;
40
41 public class ResourceTranslationContrailAttachPolicyImpl extends ResourceTranslationBase {
42
43     protected static Logger logger = LoggerFactory.getLogger(ResourceTranslationContrailAttachPolicyImpl.class);
44
45     @Override
46     protected void translate(TranslateTo translateTo) {
47         String heatFileName = translateTo.getHeatFileName();
48         String translatedNetworkResourceId = getTranslatedNetworkResourceId(translateTo);
49         if (translatedNetworkResourceId == null) {
50             return;
51         }
52
53         NodeTemplate policyNodeTemplate = getTranslatedPolicyNodeTemplate(translateTo, heatFileName);
54         if (policyNodeTemplate != null) {
55             DataModelUtil.addRequirementAssignment(policyNodeTemplate, ToscaConstants.NETWORK_REQUIREMENT_ID,
56                     createAttachmentRequirementAssignment(translatedNetworkResourceId));
57         }
58     }
59
60     @Override
61     protected String generateTranslatedId(TranslateTo translateTo) {
62         return extractAttachedResourceIdHandleMissing(translateTo, NETWORK_PROPERTY_NAME).getEntityId().toString();
63     }
64
65     @Override
66     protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
67             TranslateTo translateTo) {
68         return Optional.empty();
69     }
70
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());
79         } else {
80             logger.warn(LOG_UNSUPPORTED_POLICY_PROPERTY_GET_ATTR, translateTo.getResourceId(),
81                     translateTo.getResource().getType());
82         }
83         return policyNodeTemplate;
84     }
85
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());
97             return null;
98         }
99         return DataModelUtil.getNodeTemplate(translateTo.getServiceTemplate(), translatedPolicyResourceId.get());
100     }
101
102     private String getTranslatedNetworkResourceId(TranslateTo translateTo) {
103         AttachedResourceId attachedNetworkResourceId = extractAttachedResourceIdHandleMissing(translateTo,
104                 NETWORK_PROPERTY_NAME);
105
106         String translatedNetworkResourceId = null;
107         if (attachedNetworkResourceId.isGetResource()) {
108             translatedNetworkResourceId = (String) attachedNetworkResourceId.getTranslatedId();
109         } else {
110             logger.warn(LOG_UNSUPPORTED_POLICY_NETWORK_PROPERTY, translateTo.getResourceId(),
111                     translateTo.getResource().getType());
112         }
113         return translatedNetworkResourceId;
114     }
115
116     private AttachedResourceId extractAttachedResourceIdHandleMissing(
117             TranslateTo translateTo, String propertyName) {
118         Optional<AttachedResourceId> attachedResourceId =
119                 HeatToToscaUtil.extractAttachedResourceId(translateTo, propertyName);
120
121         if (!attachedResourceId.isPresent()) {
122             throw new CoreException(new MissingMandatoryPropertyErrorBuilder(propertyName).build());
123         }
124         return attachedResourceId.get();
125     }
126 }