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.context.impl.MdcDataErrorMessage;
26 import org.openecomp.sdc.logging.types.LoggerConstants;
27 import org.openecomp.sdc.logging.types.LoggerErrorCode;
28 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
31 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
32 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
33 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.sdc.tosca.services.DataModelUtil;
35 import org.openecomp.sdc.tosca.services.ToscaConstants;
36 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
37 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
38 import org.openecomp.sdc.translator.services.heattotosca.Constants;
39 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
40 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
41 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailTranslationHelper;
43 import java.util.HashMap;
45 import java.util.Objects;
46 import java.util.Optional;
48 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
50 static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
51 return ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat." + serviceTemplateTranslatedId;
55 public void translate(TranslateTo translateTo) {
58 mdcDataDebugMessage.debugEntryMessage(null, null);
60 ServiceTemplate globalSubstitutionServiceTemplate =
61 getGlobalSubstitutionTypesServiceTemplate(translateTo);
62 addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
63 addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
64 mdcDataDebugMessage.debugExitMessage(null, null);
68 protected boolean isEssentialRequirementsValid(TranslateTo translateTo) {
69 Map<String, Object> properties = translateTo.getResource().getProperties();
70 if (Objects.isNull(properties) || Objects.isNull(properties.get("image_name"))) {
71 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
72 LoggerTragetServiceName.GENERATE_TRANSLATED_ID, ErrorLevel.ERROR.name(),
73 LoggerErrorCode.DATA_ERROR.getErrorCode(),
74 LoggerErrorDescription.MISSING_MANDATORY_PROPERTY);
75 throw new CoreException(new MissingMandatoryPropertyErrorBuilder("image_name").build());
80 private void addComputeNodeType(TranslateTo translateTo,
81 ServiceTemplate globalSubstitutionServiceTemplate,
82 TranslationContext context) {
83 mdcDataDebugMessage.debugEntryMessage(null, null);
84 NodeType computeNodeType = new NodeType();
85 computeNodeType.setDerived_from(ToscaNodeType.CONTRAIL_COMPUTE);
86 String computeNodeTypeId = new ContrailTranslationHelper()
87 .getComputeNodeTypeId(translateTo.getResource(), translateTo.getResourceId(),
88 translateTo.getTranslatedId(), context);
90 .addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
92 mdcDataDebugMessage.debugExitMessage(null, null);
95 private void addSubstitutedNodeType(TranslateTo translateTo,
96 ServiceTemplate globalSubstitutionServiceTemplate) {
99 mdcDataDebugMessage.debugEntryMessage(null, null);
101 NodeType substitutedNodeType = new NodeType();
103 .setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
104 DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
105 getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
107 mdcDataDebugMessage.debugExitMessage(null, null);
111 protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
112 TranslateTo translateTo) {
113 return Optional.empty();
116 private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
119 mdcDataDebugMessage.debugEntryMessage(null, null);
121 ServiceTemplate globalSubstitutionServiceTemplate =
122 translateTo.getContext().getTranslatedServiceTemplates().get(
123 Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
124 if (globalSubstitutionServiceTemplate == null) {
125 globalSubstitutionServiceTemplate = new ServiceTemplate();
126 Map<String, String> templateMetadata = new HashMap<>();
127 templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
128 globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
129 globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
130 globalSubstitutionServiceTemplate
131 .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
132 translateTo.getContext().getTranslatedServiceTemplates()
133 .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME,
134 globalSubstitutionServiceTemplate);
137 mdcDataDebugMessage.debugExitMessage(null, null);
138 return globalSubstitutionServiceTemplate;