re base code
[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  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation;
18
19 import org.onap.sdc.tosca.datatypes.model.NodeType;
20 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
21 import org.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
23 import org.openecomp.sdc.tosca.datatypes.ToscaTopologyTemplateElements;
24 import org.openecomp.sdc.tosca.services.DataModelUtil;
25 import org.openecomp.sdc.tosca.services.ToscaConstants;
26 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
27 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
28 import org.openecomp.sdc.translator.services.heattotosca.Constants;
29 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
30 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
31 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailTranslationHelper;
32
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Objects;
36 import java.util.Optional;
37
38 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
39
40     private static final String IMAGE_NAME = "image_name";
41
42     static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
43         return ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + ToscaConstants.HEAT_NODE_TYPE_SUFFIX
44                 + serviceTemplateTranslatedId;
45     }
46
47     @Override
48     public void translate(TranslateTo translateTo) {
49         ServiceTemplate globalSubstitutionServiceTemplate = getGlobalSubstitutionTypesServiceTemplate(translateTo);
50         addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
51         addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
52     }
53
54     @Override
55     protected boolean isEssentialRequirementsValid(TranslateTo translateTo) {
56         Map<String, Object> properties = translateTo.getResource().getProperties();
57         if (Objects.isNull(properties) || Objects.isNull(properties.get(IMAGE_NAME))) {
58             throw new CoreException(new MissingMandatoryPropertyErrorBuilder(IMAGE_NAME).build());
59         }
60         return true;
61     }
62
63     private void addComputeNodeType(TranslateTo translateTo,
64                                     ServiceTemplate globalSubstitutionServiceTemplate,
65                                     TranslationContext context) {
66         NodeType computeNodeType = new NodeType();
67         computeNodeType.setDerived_from(ToscaNodeType.CONTRAIL_COMPUTE);
68         String computeNodeTypeId = new ContrailTranslationHelper().getComputeNodeTypeId(translateTo.getResource(),
69                 translateTo.getResourceId(), translateTo.getTranslatedId(), context);
70         DataModelUtil.addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
71     }
72
73     private void addSubstitutedNodeType(TranslateTo translateTo,
74                                         ServiceTemplate globalSubstitutionServiceTemplate) {
75         NodeType substitutedNodeType = new NodeType();
76         substitutedNodeType.setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
77         DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
78                 getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
79     }
80
81     @Override
82     protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
83             TranslateTo translateTo) {
84         return Optional.empty();
85     }
86
87     private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
88         ServiceTemplate globalSubstitutionServiceTemplate = translateTo.getContext().getTranslatedServiceTemplates()
89                 .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
90         if (globalSubstitutionServiceTemplate == null) {
91             globalSubstitutionServiceTemplate = new ServiceTemplate();
92             Map<String, String> templateMetadata = new HashMap<>();
93             templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, Constants
94                     .GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
95             globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
96             globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
97             globalSubstitutionServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
98             translateTo.getContext().getTranslatedServiceTemplates().put(Constants
99                             .GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME, globalSubstitutionServiceTemplate);
100         }
101         return globalSubstitutionServiceTemplate;
102     }
103
104 }