Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / vdu / mapper / VfModuleCustomizationToVduMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 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.onap.so.adapters.vdu.mapper;
22
23 import java.util.List;
24 import org.onap.so.adapters.vdu.VduModelInfo;
25 import org.onap.so.adapters.vdu.VduArtifact;
26 import org.onap.so.adapters.vdu.VduArtifact.ArtifactType;
27 import org.onap.so.db.catalog.beans.HeatEnvironment;
28 import org.onap.so.db.catalog.beans.HeatFiles;
29 import org.onap.so.db.catalog.beans.HeatTemplate;
30 import org.onap.so.db.catalog.beans.VfModuleCustomization;
31 import org.springframework.stereotype.Component;
32
33 @Component
34 public class VfModuleCustomizationToVduMapper {
35
36     public VduModelInfo mapVfModuleCustomizationToVdu(VfModuleCustomization vfModuleCustom) {
37         VduModelInfo vduModel = new VduModelInfo();
38         vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUUID());
39         vduModel.setModelUUID(vfModuleCustom.getVfModule().getModelUUID());
40         vduModel.setModelInvariantUUID(vfModuleCustom.getVfModule().getModelInvariantUUID());
41
42         // Map the cloud templates, attached files, and environment file
43         mapCloudTemplates(vfModuleCustom.getVfModule().getModuleHeatTemplate(), vduModel);
44         mapCloudFiles(vfModuleCustom, vduModel);
45         mapEnvironment(vfModuleCustom.getHeatEnvironment(), vduModel);
46
47         return vduModel;
48     }
49
50     public VduModelInfo mapVfModuleCustVolumeToVdu(VfModuleCustomization vfModuleCustom) {
51         VduModelInfo vduModel = new VduModelInfo();
52         vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUUID());
53         vduModel.setModelUUID(vfModuleCustom.getVfModule().getModelUUID());
54         vduModel.setModelInvariantUUID(vfModuleCustom.getVfModule().getModelInvariantUUID());
55
56         // Map the cloud templates, attached files, and environment file
57         mapCloudTemplates(vfModuleCustom.getVfModule().getVolumeHeatTemplate(), vduModel);
58         mapCloudFiles(vfModuleCustom, vduModel);
59         mapEnvironment(vfModuleCustom.getVolumeHeatEnv(), vduModel);
60
61         return vduModel;
62     }
63
64     private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) {
65         // TODO: These catalog objects will be refactored to be non-Heat-specific
66
67         List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
68
69         // Main template. Also set the VDU timeout based on the main template.
70         vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE));
71         vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes());
72
73         // Nested templates
74         List<HeatTemplate> childTemplates = heatTemplate.getChildTemplates();
75         if (childTemplates != null) {
76             for (HeatTemplate childTemplate : childTemplates) {
77                 vduArtifacts.add(mapHeatTemplateToVduArtifact(childTemplate, ArtifactType.NESTED_TEMPLATE));
78             }
79         }
80     }
81
82     private VduArtifact mapHeatTemplateToVduArtifact(HeatTemplate heatTemplate, ArtifactType artifactType) {
83         VduArtifact vduArtifact = new VduArtifact();
84         vduArtifact.setName(heatTemplate.getTemplateName());
85         vduArtifact.setContent(heatTemplate.getHeatTemplate().getBytes());
86         vduArtifact.setType(artifactType);
87         return vduArtifact;
88     }
89
90     private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) {
91         // TODO: These catalog objects will be refactored to be non-Heat-specific
92
93         List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
94
95         // Attached Files
96         List<HeatFiles> heatFiles = vfModuleCustom.getVfModule().getHeatFiles();
97         if (heatFiles != null) {
98             for (HeatFiles file : heatFiles) {
99                 vduArtifacts.add(mapCloudFileToVduArtifact(file, ArtifactType.TEXT_FILE));
100             }
101         }
102     }
103
104     private VduArtifact mapCloudFileToVduArtifact(HeatFiles heatFile, ArtifactType artifactType) {
105         VduArtifact vduArtifact = new VduArtifact();
106         vduArtifact.setName(heatFile.getFileName());
107         vduArtifact.setContent(heatFile.getFileBody().getBytes());
108         vduArtifact.setType(artifactType);
109         return vduArtifact;
110     }
111
112     private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) {
113         // TODO: These catalog objects will be refactored to be non-Heat-specific
114         if (heatEnvironment != null) {
115             List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
116             vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment));
117         }
118     }
119
120     private VduArtifact mapEnvironmentFileToVduArtifact(HeatEnvironment heatEnv) {
121         VduArtifact vduArtifact = new VduArtifact();
122         vduArtifact.setName(heatEnv.getName());
123         vduArtifact.setContent(heatEnv.getEnvironment().getBytes());
124         vduArtifact.setType(ArtifactType.ENVIRONMENT);
125         return vduArtifact;
126     }
127
128 }