Merge "Fix the variables in delete E2E workflow"
[so.git] / adapters / mso-vnf-adapter / src / main / java / org / openecomp / mso / 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.openecomp.mso.adapters.vdu.mapper;
22
23 import java.sql.SQLException;
24 import java.util.List;
25 import java.util.Map;
26 import org.openecomp.mso.adapters.vdu.VduArtifact;
27 import org.openecomp.mso.adapters.vdu.VduArtifact.ArtifactType;
28 import org.openecomp.mso.adapters.vdu.VduModelInfo;
29 import org.openecomp.mso.db.catalog.CatalogDatabase;
30 import org.openecomp.mso.db.catalog.beans.HeatEnvironment;
31 import org.openecomp.mso.db.catalog.beans.HeatFiles;
32 import org.openecomp.mso.db.catalog.beans.HeatTemplate;
33 import org.openecomp.mso.db.catalog.beans.VfModuleCustomization;
34 import org.openecomp.mso.logger.MsoLogger;
35 import org.springframework.stereotype.Component;
36
37 @Component
38 public class VfModuleCustomizationToVduMapper {
39
40         private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
41
42         public VduModelInfo mapVfModuleCustomizationToVdu(VfModuleCustomization vfModuleCustom) throws SQLException {
43                 CatalogDatabase db = CatalogDatabase.getInstance();
44                 VduModelInfo vduModel = new VduModelInfo();
45                 vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid());
46                 try {
47                         // Map the cloud templates, attached files, and environment file
48                         mapCloudTemplates(
49                                         db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getHeatTemplateArtifactUUId()),
50                                         vduModel);
51                         mapCloudFiles(vfModuleCustom, vduModel);
52                         mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getHeatEnvironmentArtifactUuid()),
53                                         vduModel);
54                 } catch (SQLException e) {
55                         LOGGER.debug("unhandled exception in mapVfModuleCustomizationToVdu", e);
56                         throw new SQLException("Exception during mapVfModuleCustomizationToVdu " + e.getMessage());
57                 } finally {
58                         // Make sure DB session is closed
59                         db.close();
60                 }
61
62                 return vduModel;
63         }
64
65         public VduModelInfo mapVfModuleCustVolumeToVdu(VfModuleCustomization vfModuleCustom) throws SQLException {
66                 CatalogDatabase db = CatalogDatabase.getInstance();
67                 VduModelInfo vduModel = new VduModelInfo();
68                 vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid());
69                 try {
70                         // Map the cloud templates, attached files, and environment file
71                         mapCloudTemplates(
72                                         db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getVolHeatTemplateArtifactUUId()),
73                                         vduModel);
74                         mapCloudFiles(vfModuleCustom, vduModel);
75                         mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getVolEnvironmentArtifactUuid()),
76                                         vduModel);
77                 } catch (SQLException e) {
78                         LOGGER.debug("unhandled exception in mapVfModuleCustVolumeToVdu", e);
79                         throw new SQLException("Exception during mapVfModuleCustVolumeToVdu " + e.getMessage());
80                 } finally {
81                         // Make sure DB session is closed
82                         db.close();
83                 }
84
85                 return vduModel;
86         }
87
88         private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) throws SQLException {
89                 // TODO: These catalog objects will be refactored to be
90                 // non-Heat-specific
91                 CatalogDatabase db = CatalogDatabase.getInstance();
92                 try {
93                         List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
94
95                         // Main template. Also set the VDU timeout based on the main
96                         // template.
97                         vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE));
98                         vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes());
99                         
100                         // Nested templates
101                         Map<String,Object> nestedTemplates = db.getNestedTemplates(heatTemplate.getArtifactUuid());
102                         if (nestedTemplates != null) {
103                                 for (String name : nestedTemplates.keySet()) {
104                                         String body = (String) nestedTemplates.get(name);
105                                         VduArtifact vduArtifact = new VduArtifact(name, body.getBytes(), ArtifactType.NESTED_TEMPLATE);
106                                         vduArtifacts.add(vduArtifact);
107                                 }
108                         }
109                         
110                 } catch (IllegalArgumentException e) {
111                         LOGGER.debug("unhandled exception in mapCloudTemplates", e);
112                         throw new IllegalArgumentException("Exception during mapCloudTemplates " + e.getMessage());
113                 } finally {
114                         // Make sure DB session is closed
115                         db.close();
116                 }
117         }
118
119         private VduArtifact mapHeatTemplateToVduArtifact(HeatTemplate heatTemplate, ArtifactType artifactType) {
120                 VduArtifact vduArtifact = new VduArtifact();
121                 vduArtifact.setName(heatTemplate.getTemplateName());
122                 vduArtifact.setContent(heatTemplate.getHeatTemplate().getBytes());
123                 vduArtifact.setType(artifactType);
124                 return vduArtifact;
125         }
126
127         private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) throws SQLException {
128                 // TODO: These catalog objects will be refactored to be
129                 // non-Heat-specific
130                 CatalogDatabase db = CatalogDatabase.getInstance();
131
132                 try{
133                         Map <String, HeatFiles> heatFiles = db.getHeatFilesForVfModule(vfModuleCustom.getVfModuleModelUuid());
134                         if (heatFiles != null) {
135                                 for (HeatFiles heatFile: heatFiles.values()) {
136                                         mapCloudFileToVduArtifact(heatFile, ArtifactType.TEXT_FILE);
137                                 }
138                         }
139                 } catch (IllegalArgumentException e) {
140                         LOGGER.debug("unhandled exception in mapCloudFiles", e);
141                         throw new IllegalArgumentException("Exception during mapCloudFiles " + e.getMessage());
142                 } finally {
143                         // Make sure DB session is closed
144                         db.close();
145                 }
146                 
147         }
148
149         private VduArtifact mapCloudFileToVduArtifact(HeatFiles heatFile, ArtifactType artifactType) {
150                 VduArtifact vduArtifact = new VduArtifact();
151                 vduArtifact.setName(heatFile.getFileName());
152                 vduArtifact.setContent(heatFile.getFileBody().getBytes());
153                 vduArtifact.setType(artifactType);
154                 return vduArtifact;
155         }
156
157         private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) {
158                 // TODO: These catalog objects will be refactored to be
159                 // non-Heat-specific
160                 if (heatEnvironment != null) {
161                         List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
162                         vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment));
163                 }
164         }
165
166         private VduArtifact mapEnvironmentFileToVduArtifact(HeatEnvironment heatEnv) {
167                 VduArtifact vduArtifact = new VduArtifact();
168                 vduArtifact.setName(heatEnv.getName());
169                 vduArtifact.setContent(heatEnv.getEnvironment().getBytes());
170                 vduArtifact.setType(ArtifactType.ENVIRONMENT);
171                 return vduArtifact;
172         }
173
174 }