From d995aceafa460627054366335896de394d47af25 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Fri, 16 Feb 2018 14:34:58 +0100 Subject: [PATCH] DGXMLLoadNActivate sonar fixes Change-Id: Id94274756541b1b64892f68ece4fa9cead46d945 Issue-ID: APPC-643 Signed-off-by: Jakub Dudycz --- .../onap/sdnc/dg/loader/DGXMLLoadNActivate.java | 163 ++++++++++++--------- 1 file changed, 90 insertions(+), 73 deletions(-) diff --git a/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoadNActivate.java b/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoadNActivate.java index 424a2d6cd..17fe9a57d 100644 --- a/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoadNActivate.java +++ b/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoadNActivate.java @@ -25,26 +25,29 @@ package org.onap.sdnc.dg.loader; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicGraph; import org.onap.ccsdk.sli.core.sli.SvcLogicParser; import org.onap.ccsdk.sli.core.sli.SvcLogicStore; import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DGXMLLoadNActivate { - private final static Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class); + + private static final Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class); + private static final String STRING_ENCODING = "utf-8"; + private final SvcLogicStore store; - public static String STRING_ENCODING = "utf-8"; - public DGXMLLoadNActivate(String propfile) throws Exception { + public DGXMLLoadNActivate(String propfile) throws DGXMLException, SvcLogicException { if (StringUtils.isBlank(propfile)) { - throw new Exception(propfile + " Profile file is not defined"); + throw new DGXMLException(propfile + " Profile file is not defined"); } this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile); } @@ -59,120 +62,134 @@ public class DGXMLLoadNActivate { } } - private void loadDGXMLDir(String xmlPath) throws Exception { + private void loadDGXMLDir(String xmlPath) { try { logger.info( - "******************** Loading DG into Database *****************************"); - List errors = new ArrayList(); + "******************** Loading DG into Database *****************************"); + List errors = new ArrayList<>(); if (this.store != null) { File xmlDir = new File(xmlPath); if (xmlDir.isDirectory()) { - String[] extensions = new String[] {"xml", "XML"}; + String[] extensions = new String[]{"xml", "XML"}; List files = (List) FileUtils.listFiles(xmlDir, extensions, true); - for (File file : files) { - logger.info("Loading DG XML file :" + file.getCanonicalPath()); - try { - SvcLogicParser.load(file.getCanonicalPath(), this.store); - } catch (Exception e) { - errors.add("Failed to load XML " + file.getCanonicalPath() - + ", Exception : " + e.getMessage()); - } - } + tryLoadXmls(errors, files); } else { - throw new Exception(xmlPath + " is not a valid XML Directory"); + throw new DGXMLException(xmlPath + " is not a valid XML Directory"); } } else { - throw new Exception("Failed to initialise SvcLogicStore"); + throw new DGXMLException("Failed to initialise SvcLogicStore"); } - if (errors.size() > 0) { - throw new Exception(errors.toString()); + if (!errors.isEmpty()) { + throw new DGXMLException(errors.toString()); } } catch (Exception e) { - logger.error(e.getMessage()); + logger.error("Failed to load DGXML directories", e); } } - public void activateDg(String activateFilePath) throws Exception { + private void tryLoadXmls(List errors, List files) throws IOException { + for (File file : files) { + logger.info("Loading DG XML file :" + file.getCanonicalPath()); + try { + SvcLogicParser.load(file.getCanonicalPath(), this.store); + } catch (Exception e) { + logger.error("Failed to load XML " + file.getCanonicalPath(), e); + errors.add("Failed to load XML " + file.getCanonicalPath() + + ", Exception : " + e.getMessage()); + } + } + } + + public void activateDg(String activateFilePath) { logger.info( - "******************** Activating DG into Database *****************************"); + "******************** Activating DG into Database *****************************"); try { - List errors = new ArrayList(); + List errors = new ArrayList<>(); if (this.store != null) { File activateFile = new File(activateFilePath); if (activateFile.isFile()) { List fileLines = FileUtils.readLines(activateFile, STRING_ENCODING); - if (fileLines != null) { - for (String line : fileLines) { - if (line != null && !line.trim().startsWith("#")) { - String lineArray[] = line.trim().split(":"); - try { - if (lineArray != null && lineArray.length >= 4) { - String module = lineArray[0]; - String rpc = lineArray[1]; - String version = lineArray[2]; - String mode = lineArray[3]; - if (StringUtils.isNotBlank(module) - && StringUtils.isNotBlank(rpc) - && StringUtils.isNotBlank(version) - && StringUtils.isNotBlank(mode)) { - logger.info("Activating DG :" + line); - SvcLogicGraph graph = - this.store.fetch(module, rpc, version, mode); - if (graph != null) { - logger.info( - "Found Graph :" + line + " Activating ..."); - this.store.activate(graph); - } else { - throw new Exception( - "Failed to fetch from Database"); - } - } - } - } catch (Exception e) { - e.printStackTrace(); - errors.add( - "Failed to Activate " + line + ", " + e.getMessage()); - } - } - } - } + tryActivateDG(errors, fileLines); } else { - throw new Exception(activateFile + " is not a valid Activate file Path"); + throw new DGXMLException(activateFile + " is not a valid Activate file Path"); } } else { - throw new Exception("Failed to initialise SvcLogicStore"); + throw new DGXMLException("Failed to initialise SvcLogicStore"); } - if (errors.size() > 0) { - throw new Exception(errors.toString()); + if (!errors.isEmpty()) { + throw new DGXMLException(errors.toString()); } } catch (Exception e) { - logger.error(e.getMessage()); + logger.error("Failed to activade DG", e); + } + } + + private void tryActivateDG(List errors, List fileLines) { + if (fileLines != null) { + for (String line : fileLines) { + if (line != null && !line.trim().startsWith("#")) { + String[] lineArray = line.trim().split(":"); + doActivateDG(errors, line, lineArray); + } + } + } + } + + private void doActivateDG(List errors, String line, String[] lineArray) { + try { + if (lineArray != null && lineArray.length >= 4) { + String module = lineArray[0]; + String rpc = lineArray[1]; + String version = lineArray[2]; + String mode = lineArray[3]; + if (StringUtils.isNotBlank(module) + && StringUtils.isNotBlank(rpc) + && StringUtils.isNotBlank(version) + && StringUtils.isNotBlank(mode)) { + logger.info("Activating DG :" + line); + SvcLogicGraph graph = + this.store.fetch(module, rpc, version, mode); + tryActivateStore(line, graph); + } + } + } catch (Exception e) { + logger.error("Failed to Activate " + line, e); + errors.add("Failed to Activate " + line + ", " + e.getMessage()); + } + } + + private void tryActivateStore(String line, SvcLogicGraph graph) throws SvcLogicException, DGXMLException { + if (graph != null) { + logger.info("Found Graph :" + line + " Activating ..."); + store.activate(graph); + } else { + throw new DGXMLException("Failed to fetch from Database"); } } public static void main(String[] args) { try { - String xmlPath = null; - String propertyPath = null; - String activateFile = null; + String xmlPath; + String propertyPath; + String activateFile; if (args != null && args.length >= 3) { xmlPath = args[0]; activateFile = args[1]; propertyPath = args[2]; } else { - throw new Exception( - "Sufficient inputs for DGXMLLoadNActivate are missing "); + throw new DGXMLException( + "Sufficient inputs for DGXMLLoadNActivate are missing "); } DGXMLLoadNActivate dgXMLLoadDB = new DGXMLLoadNActivate(propertyPath); dgXMLLoadDB.loadDGXMLDir(xmlPath); dgXMLLoadDB.activateDg(activateFile); } catch (Exception e) { - e.printStackTrace(); + logger.error("Arguments missing", e); } finally { System.exit(1); } -- 2.16.6