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.NodeType;
20 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
21 import org.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
23 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
24 import org.openecomp.sdc.tosca.services.DataModelUtil;
25 import org.openecomp.sdc.tosca.services.ToscaConstants;
26 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
27 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
28 import org.openecomp.sdc.translator.services.heattotosca.Constants;
29 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
30 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
31 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailTranslationHelper;
33 import java.util.HashMap;
35 import java.util.Objects;
36 import java.util.Optional;
38 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
40 private static final String IMAGE_NAME = "image_name";
42 static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
43 return ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + ToscaConstants.HEAT_NODE_TYPE_SUFFIX
44 + serviceTemplateTranslatedId;
48 public void translate(TranslateTo translateTo) {
49 ServiceTemplate globalSubstitutionServiceTemplate = getGlobalSubstitutionTypesServiceTemplate(translateTo);
50 addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
51 addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
55 protected boolean isEssentialRequirementsValid(TranslateTo translateTo) {
56 Map<String, Object> properties = translateTo.getResource().getProperties();
57 if (Objects.isNull(properties) || Objects.isNull(properties.get(IMAGE_NAME))) {
58 throw new CoreException(new MissingMandatoryPropertyErrorBuilder(IMAGE_NAME).build());
63 private void addComputeNodeType(TranslateTo translateTo,
64 ServiceTemplate globalSubstitutionServiceTemplate,
65 TranslationContext context) {
66 NodeType computeNodeType = new NodeType();
67 computeNodeType.setDerived_from(ToscaNodeType.CONTRAIL_COMPUTE);
68 String computeNodeTypeId = new ContrailTranslationHelper().getComputeNodeTypeId(translateTo.getResource(),
69 translateTo.getResourceId(), translateTo.getTranslatedId(), context);
70 DataModelUtil.addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
73 private void addSubstitutedNodeType(TranslateTo translateTo,
74 ServiceTemplate globalSubstitutionServiceTemplate) {
75 NodeType substitutedNodeType = new NodeType();
76 substitutedNodeType.setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
77 DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
78 getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
82 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
83 TranslateTo translateTo) {
84 return Optional.empty();
87 private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
88 ServiceTemplate globalSubstitutionServiceTemplate = translateTo.getContext().getTranslatedServiceTemplates()
89 .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
90 if (globalSubstitutionServiceTemplate == null) {
91 globalSubstitutionServiceTemplate = new ServiceTemplate();
92 Map<String, String> templateMetadata = new HashMap<>();
93 templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants
94 .GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
95 globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
96 globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
97 globalSubstitutionServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
98 translateTo.getContext().getTranslatedServiceTemplates().put(Constants
99 .GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME, globalSubstitutionServiceTemplate);
101 return globalSubstitutionServiceTemplate;