[sdc] docker file fix for cassandra
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / 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;
22
23 import org.openecomp.sdc.common.errors.CoreException;
24 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
25 import org.openecomp.sdc.tosca.datatypes.model.Metadata;
26 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.tosca.services.DataModelUtil;
29 import org.openecomp.sdc.tosca.services.ToscaConstants;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
31 import org.openecomp.sdc.translator.services.heattotosca.Constants;
32 import org.openecomp.sdc.translator.services.heattotosca.errors.MissingMandatoryPropertyErrorBuilder;
33 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
34 import org.openecomp.sdc.translator.services.heattotosca.helper.ContrailTranslationHelper;
35
36 import java.util.Map;
37 import java.util.Objects;
38
39 public class ResourceTranslationContrailServiceTemplateImpl extends ResourceTranslationBase {
40
41   static String getContrailSubstitutedNodeTypeId(String serviceTemplateTranslatedId) {
42     return ToscaConstants.NODES_SUBSTITUTION_PREFIX + serviceTemplateTranslatedId;
43   }
44
45   @Override
46   protected boolean isEssentialRequirementsValid(TranslateTo translateTo) {
47     Map<String, Object> properties = translateTo.getResource().getProperties();
48     if (Objects.isNull(properties) || Objects.isNull(properties.get("image_name"))) {
49       throw new CoreException(new MissingMandatoryPropertyErrorBuilder("image_name").build());
50     }
51     return true;
52   }
53
54   @Override
55   public void translate(TranslateTo translateTo) {
56
57     ServiceTemplate globalSubstitutionServiceTemplate =
58         getGlobalSubstitutionTypesServiceTemplate(translateTo);
59     addSubstitutedNodeType(translateTo, globalSubstitutionServiceTemplate);
60     addComputeNodeType(translateTo, globalSubstitutionServiceTemplate);
61   }
62
63   private void addComputeNodeType(TranslateTo translateTo,
64                                   ServiceTemplate globalSubstitutionServiceTemplate) {
65     NodeType computeNodeType = new NodeType();
66     computeNodeType.setDerived_from(ToscaNodeType.CONTRAIL_COMPUTE.getDisplayName());
67     String computeNodeTypeId = new ContrailTranslationHelper()
68         .getComputeNodeTypeId(translateTo.getTranslatedId(), translateTo.getResource());
69     DataModelUtil
70         .addNodeType(globalSubstitutionServiceTemplate, computeNodeTypeId, computeNodeType);
71   }
72
73   private void addSubstitutedNodeType(TranslateTo translateTo,
74                                       ServiceTemplate globalSubstitutionServiceTemplate) {
75     NodeType substitutedNodeType = new NodeType();
76     substitutedNodeType
77         .setDerived_from(ToscaNodeType.CONTRAIL_ABSTRACT_SUBSTITUTE.getDisplayName());
78     DataModelUtil.addNodeType(globalSubstitutionServiceTemplate,
79         getContrailSubstitutedNodeTypeId(translateTo.getTranslatedId()), substitutedNodeType);
80   }
81
82   private ServiceTemplate getGlobalSubstitutionTypesServiceTemplate(TranslateTo translateTo) {
83     ServiceTemplate globalSubstitutionServiceTemplate =
84         translateTo.getContext().getTranslatedServiceTemplates()
85             .get(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
86     if (globalSubstitutionServiceTemplate == null) {
87       globalSubstitutionServiceTemplate = new ServiceTemplate();
88       Metadata templateMetadata = new Metadata();
89       templateMetadata.setTemplate_name(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME);
90       globalSubstitutionServiceTemplate.setMetadata(templateMetadata);
91       globalSubstitutionServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
92       globalSubstitutionServiceTemplate
93           .setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
94       translateTo.getContext().getTranslatedServiceTemplates()
95           .put(Constants.GLOBAL_SUBSTITUTION_TYPES_TEMPLATE_NAME,
96               globalSubstitutionServiceTemplate);
97     }
98     return globalSubstitutionServiceTemplate;
99   }
100 }