[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / resourcetranslation / ResourceTranslationContrailServiceTemplateImpl.java
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
57
58     mdcDataDebugMessage.debugEntryMessage(null, null);
59
60     ServiceTemplate globalSubstitutionServiceTemplate =
61         getGlobalSubstitutionTypesServiceTemplate(translateTo);
62     addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
63     addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
64     mdcDataDebugMessage.debugExitMessage(null, null);
65   }
66
67   @Override
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());
76     }
77     return true;
78   }
79
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);
89     DataModelUtil
90         .addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
91
92     mdcDataDebugMessage.debugExitMessage(null, null);
93   }
94
95   private void addSubstitutedNodeType(TranslateTo translateTo,
96                                       ServiceTemplate globalSubstitutionServiceTemplate) {
97
98
99     mdcDataDebugMessage.debugEntryMessage(null, null);
100
101     NodeType substitutedNodeType = new NodeType();
102     substitutedNodeType
103         .setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
104     DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
105         getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
106
107     mdcDataDebugMessage.debugExitMessage(null, null);
108   }
109
110   @Override
111   protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
112       TranslateTo translateTo) {
113     return Optional.empty();
114   }
115
116   private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
117
118
119     mdcDataDebugMessage.debugEntryMessage(null, null);
120
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);
135     }
136
137     mdcDataDebugMessage.debugExitMessage(null, null);
138     return globalSubstitutionServiceTemplate;
139   }
140
141 }