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.resourcetranslation;
23 import org.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
25 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
26 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.tosca.services.DataModelUtil;
29 import org.openecomp.sdc.tosca.services.ToscaConstants;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
32 import org.openecomp.sdc.translator.services.heattotosca.Constants;
33 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
34 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
35 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailTranslationHelper;
37 import java.util.HashMap;
39 import java.util.Objects;
40 import java.util.Optional;
42 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
44 static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
45 return ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat." + serviceTemplateTranslatedId;
49 public void translate(TranslateTo translateTo) {
50 ServiceTemplate globalSubstitutionServiceTemplate =
51 getGlobalSubstitutionTypesServiceTemplate(translateTo);
52 addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
53 addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
57 protected boolean isEssentialRequirementsValid(TranslateTo translateTo) {
58 Map<String, Object> properties = translateTo.getResource().getProperties();
59 if (Objects.isNull(properties) || Objects.isNull(properties.get("image_name"))) {
60 throw new CoreException(new MissingMandatoryPropertyErrorBuilder("image_name").build());
65 private void addComputeNodeType(TranslateTo translateTo,
66 ServiceTemplate globalSubstitutionServiceTemplate,
67 TranslationContext context) {
68 NodeType computeNodeType = new NodeType();
69 computeNodeType.setDerived_from(ToscaNodeType.CONTRAIL_COMPUTE);
70 String computeNodeTypeId = new ContrailTranslationHelper()
71 .getComputeNodeTypeId(translateTo.getResource(), translateTo.getResourceId(),
72 translateTo.getTranslatedId(), context);
74 .addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
77 private void addSubstitutedNodeType(TranslateTo translateTo,
78 ServiceTemplate globalSubstitutionServiceTemplate) {
79 NodeType substitutedNodeType = new NodeType();
81 .setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
82 DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
83 getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
87 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
88 TranslateTo translateTo) {
89 return Optional.empty();
92 private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
93 ServiceTemplate globalSubstitutionServiceTemplate =
94 translateTo.getContext().getTranslatedServiceTemplates().get(
95 Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
96 if (globalSubstitutionServiceTemplate == null) {
97 globalSubstitutionServiceTemplate = new ServiceTemplate();
98 Map<String, String> templateMetadata = new HashMap<>();
99 templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
100 globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
101 globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
102 globalSubstitutionServiceTemplate
103 .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
104 translateTo.getContext().getTranslatedServiceTemplates()
105 .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME,
106 globalSubstitutionServiceTemplate);
108 return globalSubstitutionServiceTemplate;