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