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 / ResourceTranslationCinderVolumeImpl.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.NodeTemplate;
20 import org.openecomp.sdc.heat.datatypes.HeatBoolean;
21 import org.openecomp.sdc.heat.services.HeatConstants;
22 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
23 import org.openecomp.sdc.tosca.services.DataModelUtil;
24 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
25 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
26 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
27
28 import java.util.Map;
29
30
31 public class ResourceTranslationCinderVolumeImpl extends ResourceTranslationBase {
32
33     private static final String VOLUME_SIZE_PROPERTY_NAME = "size";
34
35     @Override
36     public void translate(TranslateTo translateTo) {
37         NodeTemplate nodeTemplate = new NodeTemplate();
38         nodeTemplate.setType(ToscaNodeType.CINDER_VOLUME);
39         nodeTemplate.setProperties(TranslatorHeatToToscaPropertyConverter
40                 .getToscaPropertiesSimpleConversion(translateTo.getServiceTemplate(),
41                         translateTo.getResourceId(), translateTo.getResource().getProperties(),
42                         nodeTemplate.getProperties(), translateTo.getHeatFileName(),
43                         translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
44                         nodeTemplate, translateTo.getContext()));
45         handleSizeProperty(nodeTemplate.getProperties());
46         String toscaReadOnlyPropName =
47                 HeatToToscaUtil.getToscaPropertyName(translateTo, HeatConstants.READ_ONLY_PROPERTY_NAME);
48         Object readOnlyPropVal = nodeTemplate.getProperties().get(toscaReadOnlyPropName);
49         if (readOnlyPropVal != null && !(readOnlyPropVal instanceof Map)) {
50             nodeTemplate.getProperties().put(toscaReadOnlyPropName, HeatBoolean.eval(readOnlyPropVal));
51         }
52         DataModelUtil.addNodeTemplate(translateTo.getServiceTemplate(), translateTo.getTranslatedId(),
53                 nodeTemplate);
54     }
55
56
57     private void handleSizeProperty(Map<String, Object> nodeTemplateProperties) {
58         Object size = nodeTemplateProperties.get(VOLUME_SIZE_PROPERTY_NAME);
59         if (size == null) {
60             return;
61         }
62         if (size instanceof Map) {
63             Map<String, Object> propMap = (Map) size;
64             Map.Entry<String, Object> entry = propMap.entrySet().iterator().next();
65             String val = "(" + entry.getKey() + " : " + entry.getValue() + ") * 1024";
66             nodeTemplateProperties.put(VOLUME_SIZE_PROPERTY_NAME, val);
67         } else {
68             nodeTemplateProperties.put(VOLUME_SIZE_PROPERTY_NAME, size + "*1024");
69         }
70     }
71 }