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