Containerization feature of SO
[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                 
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         {               
52                 VduModelInfo vduModel = new VduModelInfo();
53                 vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUUID());
54                 
55                 // Map the cloud templates, attached files, and environment file
56                 mapCloudTemplates(vfModuleCustom.getVfModule().getVolumeHeatTemplate(), vduModel);
57                 mapCloudFiles(vfModuleCustom,vduModel);
58                 mapEnvironment(vfModuleCustom.getVolumeHeatEnv(), vduModel);
59
60                 return vduModel;
61         }
62
63         private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) {
64                 // TODO:  These catalog objects will be refactored to be non-Heat-specific
65                 
66                 List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
67                 
68                 // Main template.  Also set the VDU timeout based on the main template.
69                 vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE));
70                 vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes());
71                 
72                 // Nested templates
73                 List<HeatTemplate> childTemplates = heatTemplate.getChildTemplates();
74                 if (childTemplates != null) {
75                         for(HeatTemplate childTemplate : childTemplates){
76                                 vduArtifacts.add(mapHeatTemplateToVduArtifact(childTemplate, ArtifactType.NESTED_TEMPLATE));
77                         }
78                 }               
79         }
80
81         private VduArtifact mapHeatTemplateToVduArtifact(HeatTemplate heatTemplate, ArtifactType artifactType) {
82                 VduArtifact vduArtifact = new VduArtifact();
83                 vduArtifact.setName(heatTemplate.getTemplateName());
84                 vduArtifact.setContent(heatTemplate.getHeatTemplate().getBytes());
85                 vduArtifact.setType(artifactType);
86                 return vduArtifact;
87         }
88         
89         private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) {
90                 // TODO:  These catalog objects will be refactored to be non-Heat-specific
91                 
92                 List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
93                 
94                 // Attached Files
95                 List<HeatFiles> heatFiles = vfModuleCustom.getVfModule().getHeatFiles();
96                 if (heatFiles != null) {
97                         for(HeatFiles file : heatFiles){
98                                 vduArtifacts.add(mapCloudFileToVduArtifact(file, ArtifactType.TEXT_FILE));
99                         }
100                 }
101         }
102
103         private VduArtifact mapCloudFileToVduArtifact(HeatFiles heatFile, ArtifactType artifactType) {
104                 VduArtifact vduArtifact = new VduArtifact();
105                 vduArtifact.setName(heatFile.getFileName());
106                 vduArtifact.setContent(heatFile.getFileBody().getBytes());
107                 vduArtifact.setType(artifactType);
108                 return vduArtifact;
109         }
110
111         private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) {
112                 // TODO:  These catalog objects will be refactored to be non-Heat-specific
113                 if (heatEnvironment != null) {
114                         List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
115                         vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment));
116                 }
117         }
118         
119         private VduArtifact mapEnvironmentFileToVduArtifact(HeatEnvironment heatEnv) {
120                 VduArtifact vduArtifact = new VduArtifact();
121                 vduArtifact.setName(heatEnv.getName());
122                 vduArtifact.setContent(heatEnv.getEnvironment().getBytes());
123                 vduArtifact.setType(ArtifactType.ENVIRONMENT);
124                 return vduArtifact;
125         }
126
127 }