7e2871043c22297a5edc3c18b1d4073d3e92dd15
[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.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;
41
42 import java.util.HashMap;
43 import java.util.Map;
44 import java.util.Objects;
45 import java.util.Optional;
46
47 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
48
49   static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
50     return ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + "heat." + serviceTemplateTranslatedId;
51   }
52
53   @Override
54   public void translate(TranslateTo translateTo) {
55     ServiceTemplate globalSubstitutionServiceTemplate =
56         getGlobalSubstitutionTypesServiceTemplate(translateTo);
57     addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
58     addComputeNodeType(translateTo, globalSubstitutionServiceTemplate, translateTo.getContext());
59   }
60
61   @Override
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());
66     }
67     return true;
68   }
69
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);
78     DataModelUtil
79         .addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
80   }
81
82   private void addSubstitutedNodeType(TranslateTo translateTo,
83                                       ServiceTemplate globalSubstitutionServiceTemplate) {
84     NodeType substitutedNodeType = new NodeType();
85     substitutedNodeType
86         .setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE);
87     DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
88         getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
89   }
90
91   @Override
92   protected Optional<ToscaTopologyTemplateElements> getTranslatedToscaTopologyElement(
93       TranslateTo translateTo) {
94     return Optional.empty();
95   }
96
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);
112     }
113     return globalSubstitutionServiceTemplate;
114   }
115
116 }