Initial commit of VDU plugin implementation
[so.git] / adapters / mso-vnf-adapter / src / main / java / org / openecomp / mso / adapters / vdu / mapper / VfModuleCustomizationToVduMapper.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.adapters.vdu.mapper;\r
22 \r
23 import java.util.List;\r
24 import java.util.Map;\r
25 \r
26 import org.openecomp.mso.adapters.vdu.VduArtifact;\r
27 import org.openecomp.mso.adapters.vdu.VduArtifact.ArtifactType;\r
28 import org.openecomp.mso.adapters.vdu.VduModelInfo;\r
29 import org.openecomp.mso.db.catalog.CatalogDatabase;\r
30 import org.openecomp.mso.db.catalog.beans.HeatEnvironment;\r
31 import org.openecomp.mso.db.catalog.beans.HeatFiles;\r
32 import org.openecomp.mso.db.catalog.beans.HeatTemplate;\r
33 import org.openecomp.mso.db.catalog.beans.VfModuleCustomization;\r
34 import org.openecomp.mso.logger.MsoLogger;\r
35 import org.springframework.stereotype.Component;\r
36 \r
37 @Component\r
38 public class VfModuleCustomizationToVduMapper {\r
39 \r
40         private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);\r
41 \r
42         public VduModelInfo mapVfModuleCustomizationToVdu(VfModuleCustomization vfModuleCustom) throws Exception {\r
43                 CatalogDatabase db = CatalogDatabase.getInstance();\r
44                 VduModelInfo vduModel = new VduModelInfo();\r
45                 vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid());\r
46                 try {\r
47                         // Map the cloud templates, attached files, and environment file\r
48                         mapCloudTemplates(\r
49                                         db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getHeatTemplateArtifactUUId()),\r
50                                         vduModel);\r
51                         mapCloudFiles(vfModuleCustom, vduModel);\r
52                         mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getHeatEnvironmentArtifactUuid()),\r
53                                         vduModel);\r
54                 } catch (Exception e) {\r
55                         LOGGER.debug("unhandled exception in mapVfModuleCustomizationToVdu", e);\r
56                         throw new Exception("Exception during mapVfModuleCustomizationToVdu " + e.getMessage());\r
57                 } finally {\r
58                         // Make sure DB session is closed\r
59                         db.close();\r
60                 }\r
61 \r
62                 return vduModel;\r
63         }\r
64 \r
65         public VduModelInfo mapVfModuleCustVolumeToVdu(VfModuleCustomization vfModuleCustom) throws Exception {\r
66                 CatalogDatabase db = CatalogDatabase.getInstance();\r
67                 VduModelInfo vduModel = new VduModelInfo();\r
68                 vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid());\r
69                 try {\r
70                         // Map the cloud templates, attached files, and environment file\r
71                         mapCloudTemplates(\r
72                                         db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getVolHeatTemplateArtifactUUId()),\r
73                                         vduModel);\r
74                         mapCloudFiles(vfModuleCustom, vduModel);\r
75                         mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getVolEnvironmentArtifactUuid()),\r
76                                         vduModel);\r
77                 } catch (Exception e) {\r
78                         LOGGER.debug("unhandled exception in mapVfModuleCustVolumeToVdu", e);\r
79                         throw new Exception("Exception during mapVfModuleCustVolumeToVdu " + e.getMessage());\r
80                 } finally {\r
81                         // Make sure DB session is closed\r
82                         db.close();\r
83                 }\r
84 \r
85                 return vduModel;\r
86         }\r
87 \r
88         private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) throws Exception {\r
89                 // TODO: These catalog objects will be refactored to be\r
90                 // non-Heat-specific\r
91                 CatalogDatabase db = CatalogDatabase.getInstance();\r
92                 try {\r
93                         List<VduArtifact> vduArtifacts = vduModel.getArtifacts();\r
94 \r
95                         // Main template. Also set the VDU timeout based on the main\r
96                         // template.\r
97                         vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE));\r
98                         vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes());\r
99                         \r
100                         // Nested templates\r
101                         Map<String,Object> nestedTemplates = db.getNestedTemplates(heatTemplate.getArtifactUuid());\r
102                         if (nestedTemplates != null) {\r
103                                 for (String name : nestedTemplates.keySet()) {\r
104                                         String body = (String) nestedTemplates.get(name);\r
105                                         VduArtifact vduArtifact = new VduArtifact(name, body.getBytes(), ArtifactType.NESTED_TEMPLATE);\r
106                                         vduArtifacts.add(vduArtifact);\r
107                                 }\r
108                         }\r
109                         \r
110                 } catch (Exception e) {\r
111                         LOGGER.debug("unhandled exception in mapCloudTemplates", e);\r
112                         throw new Exception("Exception during mapCloudTemplates " + e.getMessage());\r
113                 } finally {\r
114                         // Make sure DB session is closed\r
115                         db.close();\r
116                 }\r
117         }\r
118 \r
119         private VduArtifact mapHeatTemplateToVduArtifact(HeatTemplate heatTemplate, ArtifactType artifactType) {\r
120                 VduArtifact vduArtifact = new VduArtifact();\r
121                 vduArtifact.setName(heatTemplate.getTemplateName());\r
122                 vduArtifact.setContent(heatTemplate.getHeatTemplate().getBytes());\r
123                 vduArtifact.setType(artifactType);\r
124                 return vduArtifact;\r
125         }\r
126 \r
127         private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) throws Exception {\r
128                 // TODO: These catalog objects will be refactored to be\r
129                 // non-Heat-specific\r
130                 CatalogDatabase db = CatalogDatabase.getInstance();\r
131 \r
132                 try{\r
133                         Map <String, HeatFiles> heatFiles = db.getHeatFilesForVfModule(vfModuleCustom.getVfModuleModelUuid());\r
134                         if (heatFiles != null) {\r
135                                 for (HeatFiles heatFile: heatFiles.values()) {\r
136                                         mapCloudFileToVduArtifact(heatFile, ArtifactType.TEXT_FILE);\r
137                                 }\r
138                         }\r
139                 } catch (Exception e) {\r
140                         LOGGER.debug("unhandled exception in mapCloudFiles", e);\r
141                         throw new Exception("Exception during mapCloudFiles " + e.getMessage());\r
142                 } finally {\r
143                         // Make sure DB session is closed\r
144                         db.close();\r
145                 }\r
146                 \r
147         }\r
148 \r
149         private VduArtifact mapCloudFileToVduArtifact(HeatFiles heatFile, ArtifactType artifactType) {\r
150                 VduArtifact vduArtifact = new VduArtifact();\r
151                 vduArtifact.setName(heatFile.getFileName());\r
152                 vduArtifact.setContent(heatFile.getFileBody().getBytes());\r
153                 vduArtifact.setType(artifactType);\r
154                 return vduArtifact;\r
155         }\r
156 \r
157         private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) {\r
158                 // TODO: These catalog objects will be refactored to be\r
159                 // non-Heat-specific\r
160                 if (heatEnvironment != null) {\r
161                         List<VduArtifact> vduArtifacts = vduModel.getArtifacts();\r
162                         vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment));\r
163                 }\r
164         }\r
165 \r
166         private VduArtifact mapEnvironmentFileToVduArtifact(HeatEnvironment heatEnv) {\r
167                 VduArtifact vduArtifact = new VduArtifact();\r
168                 vduArtifact.setName(heatEnv.getName());\r
169                 vduArtifact.setContent(heatEnv.getEnvironment().getBytes());\r
170                 vduArtifact.setType(ArtifactType.ENVIRONMENT);\r
171                 return vduArtifact;\r
172         }\r
173 \r
174 }