From 3ac3837a96fd9484d481d491bbd98d2ecc2abddb Mon Sep 17 00:00:00 2001 From: AS00465059 Date: Tue, 24 Apr 2018 16:16:23 +0530 Subject: Sonar-Critical: Use Dedicated Exception File: VfModuleCustomizationToVduMapper.java Line: L56 L79 L88 L112 L127 L141 Sonar Link: https://sonar.onap.org/project/issues?assignees=as00465059&id=org.onap.so%3Aso&open=AWJqzmfZRGy6eclHEzWx&resolved=false&severities=CRITICAL Change-Id: I4fd2494ee4d883910cc1d78785665d3f0c5fdd92 Issue-ID: SO-590 Signed-off-by: AS00465059 --- .../mapper/VfModuleCustomizationToVduMapper.java | 346 ++++++++++----------- 1 file changed, 173 insertions(+), 173 deletions(-) diff --git a/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vdu/mapper/VfModuleCustomizationToVduMapper.java b/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vdu/mapper/VfModuleCustomizationToVduMapper.java index 22d988f4e4..b04f3c30db 100644 --- a/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vdu/mapper/VfModuleCustomizationToVduMapper.java +++ b/adapters/mso-vnf-adapter/src/main/java/org/openecomp/mso/adapters/vdu/mapper/VfModuleCustomizationToVduMapper.java @@ -1,174 +1,174 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.mso.adapters.vdu.mapper; - -import java.util.List; -import java.util.Map; - -import org.openecomp.mso.adapters.vdu.VduArtifact; -import org.openecomp.mso.adapters.vdu.VduArtifact.ArtifactType; -import org.openecomp.mso.adapters.vdu.VduModelInfo; -import org.openecomp.mso.db.catalog.CatalogDatabase; -import org.openecomp.mso.db.catalog.beans.HeatEnvironment; -import org.openecomp.mso.db.catalog.beans.HeatFiles; -import org.openecomp.mso.db.catalog.beans.HeatTemplate; -import org.openecomp.mso.db.catalog.beans.VfModuleCustomization; -import org.openecomp.mso.logger.MsoLogger; -import org.springframework.stereotype.Component; - -@Component -public class VfModuleCustomizationToVduMapper { - - private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA); - - public VduModelInfo mapVfModuleCustomizationToVdu(VfModuleCustomization vfModuleCustom) throws Exception { - CatalogDatabase db = CatalogDatabase.getInstance(); - VduModelInfo vduModel = new VduModelInfo(); - vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid()); - try { - // Map the cloud templates, attached files, and environment file - mapCloudTemplates( - db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getHeatTemplateArtifactUUId()), - vduModel); - mapCloudFiles(vfModuleCustom, vduModel); - mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getHeatEnvironmentArtifactUuid()), - vduModel); - } catch (Exception e) { - LOGGER.debug("unhandled exception in mapVfModuleCustomizationToVdu", e); - throw new Exception("Exception during mapVfModuleCustomizationToVdu " + e.getMessage()); - } finally { - // Make sure DB session is closed - db.close(); - } - - return vduModel; - } - - public VduModelInfo mapVfModuleCustVolumeToVdu(VfModuleCustomization vfModuleCustom) throws Exception { - CatalogDatabase db = CatalogDatabase.getInstance(); - VduModelInfo vduModel = new VduModelInfo(); - vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid()); - try { - // Map the cloud templates, attached files, and environment file - mapCloudTemplates( - db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getVolHeatTemplateArtifactUUId()), - vduModel); - mapCloudFiles(vfModuleCustom, vduModel); - mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getVolEnvironmentArtifactUuid()), - vduModel); - } catch (Exception e) { - LOGGER.debug("unhandled exception in mapVfModuleCustVolumeToVdu", e); - throw new Exception("Exception during mapVfModuleCustVolumeToVdu " + e.getMessage()); - } finally { - // Make sure DB session is closed - db.close(); - } - - return vduModel; - } - - private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) throws Exception { - // TODO: These catalog objects will be refactored to be - // non-Heat-specific - CatalogDatabase db = CatalogDatabase.getInstance(); - try { - List vduArtifacts = vduModel.getArtifacts(); - - // Main template. Also set the VDU timeout based on the main - // template. - vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE)); - vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes()); - - // Nested templates - Map nestedTemplates = db.getNestedTemplates(heatTemplate.getArtifactUuid()); - if (nestedTemplates != null) { - for (String name : nestedTemplates.keySet()) { - String body = (String) nestedTemplates.get(name); - VduArtifact vduArtifact = new VduArtifact(name, body.getBytes(), ArtifactType.NESTED_TEMPLATE); - vduArtifacts.add(vduArtifact); - } - } - - } catch (Exception e) { - LOGGER.debug("unhandled exception in mapCloudTemplates", e); - throw new Exception("Exception during mapCloudTemplates " + e.getMessage()); - } finally { - // Make sure DB session is closed - db.close(); - } - } - - private VduArtifact mapHeatTemplateToVduArtifact(HeatTemplate heatTemplate, ArtifactType artifactType) { - VduArtifact vduArtifact = new VduArtifact(); - vduArtifact.setName(heatTemplate.getTemplateName()); - vduArtifact.setContent(heatTemplate.getHeatTemplate().getBytes()); - vduArtifact.setType(artifactType); - return vduArtifact; - } - - private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) throws Exception { - // TODO: These catalog objects will be refactored to be - // non-Heat-specific - CatalogDatabase db = CatalogDatabase.getInstance(); - - try{ - Map heatFiles = db.getHeatFilesForVfModule(vfModuleCustom.getVfModuleModelUuid()); - if (heatFiles != null) { - for (HeatFiles heatFile: heatFiles.values()) { - mapCloudFileToVduArtifact(heatFile, ArtifactType.TEXT_FILE); - } - } - } catch (Exception e) { - LOGGER.debug("unhandled exception in mapCloudFiles", e); - throw new Exception("Exception during mapCloudFiles " + e.getMessage()); - } finally { - // Make sure DB session is closed - db.close(); - } - - } - - private VduArtifact mapCloudFileToVduArtifact(HeatFiles heatFile, ArtifactType artifactType) { - VduArtifact vduArtifact = new VduArtifact(); - vduArtifact.setName(heatFile.getFileName()); - vduArtifact.setContent(heatFile.getFileBody().getBytes()); - vduArtifact.setType(artifactType); - return vduArtifact; - } - - private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) { - // TODO: These catalog objects will be refactored to be - // non-Heat-specific - if (heatEnvironment != null) { - List vduArtifacts = vduModel.getArtifacts(); - vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment)); - } - } - - private VduArtifact mapEnvironmentFileToVduArtifact(HeatEnvironment heatEnv) { - VduArtifact vduArtifact = new VduArtifact(); - vduArtifact.setName(heatEnv.getName()); - vduArtifact.setContent(heatEnv.getEnvironment().getBytes()); - vduArtifact.setType(ArtifactType.ENVIRONMENT); - return vduArtifact; - } - +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.adapters.vdu.mapper; + +import java.sql.SQLException; +import java.util.List; +import java.util.Map; +import org.openecomp.mso.adapters.vdu.VduArtifact; +import org.openecomp.mso.adapters.vdu.VduArtifact.ArtifactType; +import org.openecomp.mso.adapters.vdu.VduModelInfo; +import org.openecomp.mso.db.catalog.CatalogDatabase; +import org.openecomp.mso.db.catalog.beans.HeatEnvironment; +import org.openecomp.mso.db.catalog.beans.HeatFiles; +import org.openecomp.mso.db.catalog.beans.HeatTemplate; +import org.openecomp.mso.db.catalog.beans.VfModuleCustomization; +import org.openecomp.mso.logger.MsoLogger; +import org.springframework.stereotype.Component; + +@Component +public class VfModuleCustomizationToVduMapper { + + private static MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA); + + public VduModelInfo mapVfModuleCustomizationToVdu(VfModuleCustomization vfModuleCustom) throws SQLException { + CatalogDatabase db = CatalogDatabase.getInstance(); + VduModelInfo vduModel = new VduModelInfo(); + vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid()); + try { + // Map the cloud templates, attached files, and environment file + mapCloudTemplates( + db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getHeatTemplateArtifactUUId()), + vduModel); + mapCloudFiles(vfModuleCustom, vduModel); + mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getHeatEnvironmentArtifactUuid()), + vduModel); + } catch (SQLException e) { + LOGGER.debug("unhandled exception in mapVfModuleCustomizationToVdu", e); + throw new SQLException("Exception during mapVfModuleCustomizationToVdu " + e.getMessage()); + } finally { + // Make sure DB session is closed + db.close(); + } + + return vduModel; + } + + public VduModelInfo mapVfModuleCustVolumeToVdu(VfModuleCustomization vfModuleCustom) throws SQLException { + CatalogDatabase db = CatalogDatabase.getInstance(); + VduModelInfo vduModel = new VduModelInfo(); + vduModel.setModelCustomizationUUID(vfModuleCustom.getModelCustomizationUuid()); + try { + // Map the cloud templates, attached files, and environment file + mapCloudTemplates( + db.getHeatTemplateByArtifactUuid(vfModuleCustom.getVfModule().getVolHeatTemplateArtifactUUId()), + vduModel); + mapCloudFiles(vfModuleCustom, vduModel); + mapEnvironment(db.getHeatEnvironmentByArtifactUuid(vfModuleCustom.getVolEnvironmentArtifactUuid()), + vduModel); + } catch (SQLException e) { + LOGGER.debug("unhandled exception in mapVfModuleCustVolumeToVdu", e); + throw new SQLException("Exception during mapVfModuleCustVolumeToVdu " + e.getMessage()); + } finally { + // Make sure DB session is closed + db.close(); + } + + return vduModel; + } + + private void mapCloudTemplates(HeatTemplate heatTemplate, VduModelInfo vduModel) throws SQLException { + // TODO: These catalog objects will be refactored to be + // non-Heat-specific + CatalogDatabase db = CatalogDatabase.getInstance(); + try { + List vduArtifacts = vduModel.getArtifacts(); + + // Main template. Also set the VDU timeout based on the main + // template. + vduArtifacts.add(mapHeatTemplateToVduArtifact(heatTemplate, ArtifactType.MAIN_TEMPLATE)); + vduModel.setTimeoutMinutes(heatTemplate.getTimeoutMinutes()); + + // Nested templates + Map nestedTemplates = db.getNestedTemplates(heatTemplate.getArtifactUuid()); + if (nestedTemplates != null) { + for (String name : nestedTemplates.keySet()) { + String body = (String) nestedTemplates.get(name); + VduArtifact vduArtifact = new VduArtifact(name, body.getBytes(), ArtifactType.NESTED_TEMPLATE); + vduArtifacts.add(vduArtifact); + } + } + + } catch (IllegalArgumentException e) { + LOGGER.debug("unhandled exception in mapCloudTemplates", e); + throw new IllegalArgumentException("Exception during mapCloudTemplates " + e.getMessage()); + } finally { + // Make sure DB session is closed + db.close(); + } + } + + private VduArtifact mapHeatTemplateToVduArtifact(HeatTemplate heatTemplate, ArtifactType artifactType) { + VduArtifact vduArtifact = new VduArtifact(); + vduArtifact.setName(heatTemplate.getTemplateName()); + vduArtifact.setContent(heatTemplate.getHeatTemplate().getBytes()); + vduArtifact.setType(artifactType); + return vduArtifact; + } + + private void mapCloudFiles(VfModuleCustomization vfModuleCustom, VduModelInfo vduModel) throws SQLException { + // TODO: These catalog objects will be refactored to be + // non-Heat-specific + CatalogDatabase db = CatalogDatabase.getInstance(); + + try{ + Map heatFiles = db.getHeatFilesForVfModule(vfModuleCustom.getVfModuleModelUuid()); + if (heatFiles != null) { + for (HeatFiles heatFile: heatFiles.values()) { + mapCloudFileToVduArtifact(heatFile, ArtifactType.TEXT_FILE); + } + } + } catch (IllegalArgumentException e) { + LOGGER.debug("unhandled exception in mapCloudFiles", e); + throw new IllegalArgumentException("Exception during mapCloudFiles " + e.getMessage()); + } finally { + // Make sure DB session is closed + db.close(); + } + + } + + private VduArtifact mapCloudFileToVduArtifact(HeatFiles heatFile, ArtifactType artifactType) { + VduArtifact vduArtifact = new VduArtifact(); + vduArtifact.setName(heatFile.getFileName()); + vduArtifact.setContent(heatFile.getFileBody().getBytes()); + vduArtifact.setType(artifactType); + return vduArtifact; + } + + private void mapEnvironment(HeatEnvironment heatEnvironment, VduModelInfo vduModel) { + // TODO: These catalog objects will be refactored to be + // non-Heat-specific + if (heatEnvironment != null) { + List vduArtifacts = vduModel.getArtifacts(); + vduArtifacts.add(mapEnvironmentFileToVduArtifact(heatEnvironment)); + } + } + + private VduArtifact mapEnvironmentFileToVduArtifact(HeatEnvironment heatEnv) { + VduArtifact vduArtifact = new VduArtifact(); + vduArtifact.setName(heatEnv.getName()); + vduArtifact.setContent(heatEnv.getEnvironment().getBytes()); + vduArtifact.setType(ArtifactType.ENVIRONMENT); + return vduArtifact; + } + } \ No newline at end of file -- cgit 1.2.3-korg