2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.adapters.vdu.mapper;
23 import java.sql.SQLException;
24 import java.util.List;
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;
38 public class VfModuleCustomizationToVduMapper {
40 private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
42 public VduModelInfo mapVfModuleCustomizationToVdu(VfModuleCustomization vfModuleCustom) throws SQLException {
43 CatalogDatabase db = CatalogDatabase.getInstance();
44 VduModelInfo vduModel = new VduModelInfo();
45 vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid());
47 // Map the cloud templates, attached files, and environment file
49 db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getHeatTemplateArtifactUUId()),
51 mapCloudFiles(vfModuleCustom, vduModel);
52 mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getHeatEnvironmentArtifactUuid()),
54 } catch (SQLException e) {
55 LOGGER.debug("unhandled exception in mapVfModuleCustomizationToVdu", e);
56 throw new SQLException("Exception during mapVfModuleCustomizationToVdu " + e.getMessage());
58 // Make sure DB session is closed
65 public VduModelInfo mapVfModuleCustVolumeToVdu(VfModuleCustomization vfModuleCustom) throws SQLException {
66 CatalogDatabase db = CatalogDatabase.getInstance();
67 VduModelInfo vduModel = new VduModelInfo();
68 vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid());
70 // Map the cloud templates, attached files, and environment file
72 db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getVolHeatTemplateArtifactUUId()),
74 mapCloudFiles(vfModuleCustom, vduModel);
75 mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getVolEnvironmentArtifactUuid()),
77 } catch (SQLException e) {
78 LOGGER.debug("unhandled exception in mapVfModuleCustVolumeToVdu", e);
79 throw new SQLException("Exception during mapVfModuleCustVolumeToVdu " + e.getMessage());
81 // Make sure DB session is closed
88 private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) throws SQLException {
89 // TODO: These catalog objects will be refactored to be
91 CatalogDatabase db = CatalogDatabase.getInstance();
93 List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
95 // Main template. Also set the VDU timeout based on the main
97 vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE));
98 vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes());
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);
110 } catch (IllegalArgumentException e) {
111 LOGGER.debug("unhandled exception in mapCloudTemplates", e);
112 throw new IllegalArgumentException("Exception during mapCloudTemplates " + e.getMessage());
114 // Make sure DB session is closed
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);
127 private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) throws SQLException {
128 // TODO: These catalog objects will be refactored to be
130 CatalogDatabase db = CatalogDatabase.getInstance();
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);
139 } catch (IllegalArgumentException e) {
140 LOGGER.debug("unhandled exception in mapCloudFiles", e);
141 throw new IllegalArgumentException("Exception during mapCloudFiles " + e.getMessage());
143 // Make sure DB session is closed
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);
157 private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) {
158 // TODO: These catalog objects will be refactored to be
160 if (heatEnvironment != null) {
161 List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
162 vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment));
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);