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.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.logging.types.LoggerConstants;
26 import org.openecomp.sdc.logging.types.LoggerErrorCode;
27 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
28 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
29 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
30 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
31 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
32 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
33 import org.openecomp.sdc.tosca.services.DataModelUtil;
34 import org.openecomp.sdc.tosca.services.ToscaConstants;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
37 import org.openecomp.sdc.translator.services.heattotosca.Constants;
38 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
39 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
40 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailTranslationHelper;
42 import java.util.HashMap;
44 import java.util.Objects;
45 import java.util.Optional;
47 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
49 static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
50 return ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat." + serviceTemplateTranslatedId;
54 public void translate(TranslateTo translateTo) {
55 ServiceTemplate globalSubstitutionServiceTemplate =
56 getGlobalSubstitutionTypesServiceTemplate(translateTo);
57 addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
58 addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
62 protected boolean isEssentialRequirementsValid(TranslateTo translateTo) {
63 Map<String, Object> properties = translateTo.getResource().getProperties();
64 if (Objects.isNull(properties) || Objects.isNull(properties.get("image_name"))) {
65 throw new CoreException(new MissingMandatoryPropertyErrorBuilder("image_name").build());
70 private void addComputeNodeType(TranslateTo translateTo,
71 ServiceTemplate globalSubstitutionServiceTemplate,
72 TranslationContext context) {
73 NodeType computeNodeType = new NodeType();
74 computeNodeType.setDerived_from(ToscaNodeType.CONTRAIL_COMPUTE);
75 String computeNodeTypeId = new ContrailTranslationHelper()
76 .getComputeNodeTypeId(translateTo.getResource(), translateTo.getResourceId(),
77 translateTo.getTranslatedId(), context);
79 .addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
82 private void addSubstitutedNodeType(TranslateTo translateTo,
83 ServiceTemplate globalSubstitutionServiceTemplate) {
84 NodeType substitutedNodeType = new NodeType();
86 .setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
87 DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
88 getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
92 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
93 TranslateTo translateTo) {
94 return Optional.empty();
97 private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
98 ServiceTemplate globalSubstitutionServiceTemplate =
99 translateTo.getContext().getTranslatedServiceTemplates().get(
100 Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
101 if (globalSubstitutionServiceTemplate == null) {
102 globalSubstitutionServiceTemplate = new ServiceTemplate();
103 Map<String, String> templateMetadata = new HashMap<>();
104 templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
105 globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
106 globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
107 globalSubstitutionServiceTemplate
108 .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
109 translateTo.getContext().getTranslatedServiceTemplates()
110 .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME,
111 globalSubstitutionServiceTemplate);
113 return globalSubstitutionServiceTemplate;