2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package org.openecomp.mso.adapters.vdu.mapper;
\r
23 import java.util.List;
\r
24 import java.util.Map;
\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
38 public class VfModuleCustomizationToVduMapper {
\r
40 private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
\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
47 // Map the cloud templates, attached files, and environment file
\r
49 db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getHeatTemplateArtifactUUId()),
\r
51 mapCloudFiles(vfModuleCustom, vduModel);
\r
52 mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getHeatEnvironmentArtifactUuid()),
\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
58 // Make sure DB session is closed
\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
70 // Map the cloud templates, attached files, and environment file
\r
72 db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getVolHeatTemplateArtifactUUId()),
\r
74 mapCloudFiles(vfModuleCustom, vduModel);
\r
75 mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getVolEnvironmentArtifactUuid()),
\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
81 // Make sure DB session is closed
\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
93 List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
\r
95 // Main template. Also set the VDU timeout based on the main
\r
97 vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE));
\r
98 vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes());
\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
110 } catch (Exception e) {
\r
111 LOGGER.debug("unhandled exception in mapCloudTemplates", e);
\r
112 throw new Exception("Exception during mapCloudTemplates " + e.getMessage());
\r
114 // Make sure DB session is closed
\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
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
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
139 } catch (Exception e) {
\r
140 LOGGER.debug("unhandled exception in mapCloudFiles", e);
\r
141 throw new Exception("Exception during mapCloudFiles " + e.getMessage());
\r
143 // Make sure DB session is closed
\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
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
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