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.util.List;
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;
37 public class VfModuleCustomizationToVduMapper {
39 private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
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
47 db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getHeatTemplateArtifactUUId()),
49 mapCloudFiles(vfModuleCustom, vduModel);
50 mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getHeatEnvironmentArtifactUuid()),
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
62 db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getVolHeatTemplateArtifactUUId()),
64 mapCloudFiles(vfModuleCustom, vduModel);
65 mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getVolEnvironmentArtifactUuid()),
71 private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) {
72 // TODO: These catalog objects will be refactored to be
74 try (CatalogDatabase db = CatalogDatabase.getInstance()) {
75 List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
77 // Main template. Also set the VDU timeout based on the main
79 vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE));
80 vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes());
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);
90 } catch (IllegalArgumentException e) {
91 LOGGER.debug("unhandled exception in mapCloudTemplates", e);
92 throw new IllegalArgumentException("Exception during mapCloudTemplates " + e.getMessage());
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);
104 private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) {
105 // TODO: These catalog objects will be refactored to be
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));
114 } catch (IllegalArgumentException e) {
115 LOGGER.debug("unhandled exception in mapCloudFiles", e);
116 throw new IllegalArgumentException("Exception during mapCloudFiles " + e.getMessage());
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);
128 private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) {
129 // TODO: These catalog objects will be refactored to be
131 if (heatEnvironment != null) {
132 List<VduArtifact> vduArtifacts = vduModel.getArtifacts();
133 vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment));
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);