49bcdd0d835f3d4ed80cd41a120c389c9220eee8
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
22
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;
42
43 import java.util.HashMap;
44 import java.util.Map;
45 import java.util.Objects;
46 import java.util.Optional;
47
48 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
49
50   static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
51     return ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat." + serviceTemplateTranslatedId;
52   }
53
54   @Override
55   public void translate(TranslateTo translateTo) {
56     ServiceTemplate globalSubstitutionServiceTemplate =
57         getGlobalSubstitutionTypesServiceTemplate(translateTo);
58     addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
59     addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
60   }
61
62   @Override
63   protected boolean isEssentialRequirementsValid(TranslateTo translateTo) {
64     Map<String, Object> properties = translateTo.getResource().getProperties();
65     if (Objects.isNull(properties) || Objects.isNull(properties.get("image_name"))) {
66       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
67           LoggerTragetServiceName.GENERATE_TRANSLATED_ID, ErrorLevel.ERROR.name(),
68           LoggerErrorCode.DATA_ERROR.getErrorCode(),
69           LoggerErrorDescription.MISSING_MANDATORY_PROPERTY);
70       throw new CoreException(new MissingMandatoryPropertyErrorBuilder("image_name").build());
71     }
72     return true;
73   }
74
75   private void addComputeNodeType(TranslateTo translateTo,
76                                   ServiceTemplate globalSubstitutionServiceTemplate,
77                                   TranslationContext context) {
78     NodeType computeNodeType = new NodeType();
79     computeNodeType.setDerived_from(ToscaNodeType.CONTRAIL_COMPUTE);
80     String computeNodeTypeId = new ContrailTranslationHelper()
81         .getComputeNodeTypeId(translateTo.getResource(), translateTo.getResourceId(),
82             translateTo.getTranslatedId(), context);
83     DataModelUtil
84         .addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
85   }
86
87   private void addSubstitutedNodeType(TranslateTo translateTo,
88                                       ServiceTemplate globalSubstitutionServiceTemplate) {
89     NodeType substitutedNodeType = new NodeType();
90     substitutedNodeType
91         .setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
92     DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
93         getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
94   }
95
96   @Override
97   protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
98       TranslateTo translateTo) {
99     return Optional.empty();
100   }
101
102   private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
103     ServiceTemplate globalSubstitutionServiceTemplate =
104         translateTo.getContext().getTranslatedServiceTemplates().get(
105             Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
106     if (globalSubstitutionServiceTemplate == null) {
107       globalSubstitutionServiceTemplate = new ServiceTemplate();
108       Map<String, String> templateMetadata = new HashMap<>();
109       templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
110       globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
111       globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
112       globalSubstitutionServiceTemplate
113           .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
114       translateTo.getContext().getTranslatedServiceTemplates()
115           .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME,
116               globalSubstitutionServiceTemplate);
117     }
118     return globalSubstitutionServiceTemplate;
119   }
120
121 }