diff options
author | Krishnajinka <kris.jinka@samsung.com> | 2018-07-26 20:58:24 +0900 |
---|---|---|
committer | krishnajinka <kris.jinka@samsung.com> | 2018-07-28 11:47:46 +0900 |
commit | 1ecf9de8c889adb2dbc9ddd9a79d28e1da89d87b (patch) | |
tree | a946699b1ffec4845315e7e92e08d01b4ce927f7 | |
parent | 1df87df1af0d215b9ff908dd15089393b9be270b (diff) |
Fix Sonar issues in utils PDP ParseLog rework
Fix Major Sonar issues in ParseLog BackupMonitor and ONAP-PDP component.
Issues related to nested control statements. Rework2 based on review comments
Issue-ID: POLICY-1013
Change-Id: I10edb242b45a649df902a4e7c1aba0a285b8b7fb
Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
3 files changed, 230 insertions, 216 deletions
diff --git a/LogParser/src/main/java/org/onap/xacml/parser/ParseLog.java b/LogParser/src/main/java/org/onap/xacml/parser/ParseLog.java index 75ccfd477..54e86d250 100644 --- a/LogParser/src/main/java/org/onap/xacml/parser/ParseLog.java +++ b/LogParser/src/main/java/org/onap/xacml/parser/ParseLog.java @@ -3,6 +3,7 @@ * LogParser * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -377,27 +378,28 @@ public class ParseLog { StringBuilder builder = new StringBuilder(); long length = file.length(); logger.debug("dataFileName: " + dataFileName); - if (length > 0) { - length--; - randomAccessFile.seek(length); - for (long seek = length; seek >= 0; --seek) { - randomAccessFile.seek(seek); - final char c = (char) randomAccessFile.read(); - builder.append(c); - if (c == '\n') { - builder = builder.reverse(); - logger.debug("builder.toString(): " + builder.toString()); - if (builder.toString().contains(last + dataFileName + lineRead)) { - final String[] parseString = builder.toString().split(last + dataFileName + lineRead); - final String returnValue = parseString[1].replace("\r", ""); - randomAccessFile.close(); - return returnValue.trim(); - } - builder = new StringBuilder(); + if (length == 0) { + return null; + } + + length--; + randomAccessFile.seek(length); + for (long seek = length; seek >= 0; --seek) { + randomAccessFile.seek(seek); + final char c = (char) randomAccessFile.read(); + builder.append(c); + if (c == '\n') { + builder = builder.reverse(); + logger.debug("builder.toString(): " + builder.toString()); + if (builder.toString().contains(last + dataFileName + lineRead)) { + final String[] parseString = builder.toString().split(last + dataFileName + lineRead); + final String returnValue = parseString[1].replace("\r", ""); + return returnValue.trim(); } + builder = new StringBuilder(); } } - randomAccessFile.close(); + } return null; } @@ -820,23 +822,25 @@ public class ParseLog { } private static void setLogFileProperties(final String[] splitString) { - if (splitString != null) { - for (int i = 0; i < splitString.length; i++) { - - if (splitString[i].contains("debug")) { - // get path of debug.log file - setDebuglogFile(splitString[i]); - } else if (splitString[i].contains("error")) { - // get path of error.log file - setErrorlogFile(splitString[i]); + if (splitString == null) { + return; + } + + for (int i = 0; i < splitString.length; i++) { + + if (splitString[i].contains("debug")) { + // get path of debug.log file + setDebuglogFile(splitString[i]); + } else if (splitString[i].contains("error")) { + // get path of error.log file + setErrorlogFile(splitString[i]); + } else { + // get path of default file + logFile = splitString[i]; + if (logFile != null && !logFile.isEmpty()) { + logFile = logFile.trim(); } else { - // get path of default file - logFile = splitString[i]; - if (logFile != null && !logFile.isEmpty()) { - logFile = logFile.trim(); - } else { - logFile = null; - } + logFile = null; } } } @@ -851,53 +855,48 @@ public class ParseLog { public static Properties getPropertiesValue(final String fileName) { final Properties config = new Properties(); final Path file = Paths.get(fileName); - if (file.toFile().exists()) { - - if (file.toString().endsWith(".properties")) { - InputStream in; - try { - in = new FileInputStream(file.toFile()); - config.load(in); - - resourceName = config.getProperty("RESOURCE_NAME"); - system = config.getProperty("SERVER"); - type = config.getProperty("LOGTYPE"); - systemLogFile = config.getProperty("PARSERLOGPATH"); - final String logFiles = config.getProperty("LOGPATH"); - final String cleanupInterval = config.getProperty("CHECK_INTERVAL"); - final String timeFrame = config.getProperty("TIME_FRAME"); - - setCleanUpProperties(cleanupInterval, timeFrame); - - if (logFiles == null || logFiles.isEmpty()) { - isMissingLogFile = true; - return null; - } + //ensure file exists and it is properties file + if (!(file.toFile().exists() && file.toString().endsWith(".properties"))) { + logger.debug("File doesn't exist in the specified Path Or it is not a properties file" + file.toString()); + return null; + } - final String[] splitString = getPaths(logFiles); + try (InputStream in = new FileInputStream(file.toFile())) { + config.load(in); - setLogFileProperties(splitString); + resourceName = config.getProperty("RESOURCE_NAME"); + system = config.getProperty("SERVER"); + type = config.getProperty("LOGTYPE"); + systemLogFile = config.getProperty("PARSERLOGPATH"); + final String logFiles = config.getProperty("LOGPATH"); + final String cleanupInterval = config.getProperty("CHECK_INTERVAL"); + final String timeFrame = config.getProperty("TIME_FRAME"); - jdbcUrl = config.getProperty("JDBC_URL").replace("'", ""); - jdbcUser = config.getProperty("JDBC_USER"); - jdbcDriver = config.getProperty("JDBC_DRIVER"); - jdbcPassword = CryptoUtils.decryptTxtNoExStr(config.getProperty("JDBC_PASSWORD", "")); - config.setProperty("javax.persistence.jdbc.password", - CryptoUtils.decryptTxtNoExStr(config.getProperty("javax.persistence.jdbc.password", ""))); - return config; + setCleanUpProperties(cleanupInterval, timeFrame); - } catch (final IOException e) { - logger.error("Error porcessing Config file will be unable to create Health Check" + e); - } catch (final Exception e) { - logger.error("Error getPropertiesValue on TIME_FRAME", e); - logger.debug("Error getPropertiesValue on TIME_FRAME, so use its default value:" + defaultTimeFrame - + " days"); - } + if (logFiles == null || logFiles.isEmpty()) { + isMissingLogFile = true; + return null; } - } else { + final String[] splitString = getPaths(logFiles); - logger.debug("File doesn't exist in the specified Path " + file.toString()); + setLogFileProperties(splitString); + + jdbcUrl = config.getProperty("JDBC_URL").replace("'", ""); + jdbcUser = config.getProperty("JDBC_USER"); + jdbcDriver = config.getProperty("JDBC_DRIVER"); + jdbcPassword = CryptoUtils.decryptTxtNoExStr(config.getProperty("JDBC_PASSWORD", "")); + config.setProperty("javax.persistence.jdbc.password", + CryptoUtils.decryptTxtNoExStr(config.getProperty("javax.persistence.jdbc.password", ""))); + return config; + + } catch (final IOException e) { + logger.error("Error porcessing Config file will be unable to create Health Check" + e); + } catch (final Exception e) { + logger.error("Error getPropertiesValue on TIME_FRAME", e); + logger.debug("Error getPropertiesValue on TIME_FRAME, so use its default value:" + defaultTimeFrame + + " days"); } return null; } diff --git a/ONAP-PDP/src/main/java/org/onap/policy/xacml/custom/OnapFunctionDefinitionFactory.java b/ONAP-PDP/src/main/java/org/onap/policy/xacml/custom/OnapFunctionDefinitionFactory.java index 35080d396..b69141f48 100644 --- a/ONAP-PDP/src/main/java/org/onap/policy/xacml/custom/OnapFunctionDefinitionFactory.java +++ b/ONAP-PDP/src/main/java/org/onap/policy/xacml/custom/OnapFunctionDefinitionFactory.java @@ -3,6 +3,7 @@ * ONAP-PDP * ================================================================================ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -58,25 +59,25 @@ public class OnapFunctionDefinitionFactory extends FunctionDefinitionFactory { needMapInit = false; final Field[] declaredFields = StdFunctions.class.getDeclaredFields(); for (final Field field : declaredFields) { - if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith(StdFunctions.FD_PREFIX) - && FunctionDefinition.class.isAssignableFrom(field.getType()) - && Modifier.isPublic(field.getModifiers())) { - try { - register((FunctionDefinition) (field.get(null))); - } catch (final IllegalAccessException ex) { - logger.error(ex.getMessage() + ex); - } - } + registerFunctionDefinition(field); } - // - // Our custom function - // - // register(FunctionDefinitionCustomRegexpMatch); register(FD_CUSTOM_REGEXP_MATCH); } } } + private static void registerFunctionDefinition(Field field) { + if (Modifier.isStatic(field.getModifiers()) && field.getName().startsWith(StdFunctions.FD_PREFIX) + && FunctionDefinition.class.isAssignableFrom(field.getType()) + && Modifier.isPublic(field.getModifiers())) { + try { + register((FunctionDefinition) (field.get(null))); + } catch (final IllegalAccessException ex) { + logger.error(ex.getMessage() + ex); + } + } + } + public OnapFunctionDefinitionFactory() { initMap(); } diff --git a/PolicyEngineUtils/src/main/java/org/onap/policy/utils/BackUpMonitor.java b/PolicyEngineUtils/src/main/java/org/onap/policy/utils/BackUpMonitor.java index 1353cc607..4f8a82100 100644 --- a/PolicyEngineUtils/src/main/java/org/onap/policy/utils/BackUpMonitor.java +++ b/PolicyEngineUtils/src/main/java/org/onap/policy/utils/BackUpMonitor.java @@ -3,6 +3,7 @@ * PolicyEngineUtils * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -255,130 +256,7 @@ public class BackUpMonitor { em.persist(bMEntity); em.flush(); } else { - // Check for other Master(s) - ArrayList<BackUpMonitorEntity> masterEntities = new ArrayList<>(); - // Check for self. - BackUpMonitorEntity selfEntity = null; - // Check backup monitor entities. - for (int i = 0; i < bMList.size(); i++) { - BackUpMonitorEntity bMEntity = (BackUpMonitorEntity) bMList.get(i); - LOGGER.info("Refreshing Entity. "); - em.refresh(bMEntity); - if (bMEntity.getFlag().equalsIgnoreCase(MASTER)) { - masterEntities.add(bMEntity); - } - if (bMEntity.getResourceName().equals(resourceName)) { - selfEntity = bMEntity; - } - } - if (selfEntity != null) { - LOGGER.info("Resource Name already Exists: " + resourceName); - if (selfEntity.getFlag().equalsIgnoreCase(MASTER)) { - // Already Master Mode. - setFlag(true); - LOGGER.info(resourceName + " is on Master Mode"); - selfEntity.setTimeStamp(new Date()); - selfEntity.setNotificationRecord(notificationRecord); - em.persist(selfEntity); - em.flush(); - setLastNotification(null); - if (!masterEntities.contains(selfEntity)) { - masterEntities.add(selfEntity); - } - } else { - // Already Slave Mode. - setFlag(false); - selfEntity.setTimeStamp(new Date()); - selfEntity.setNotificationRecord(notificationRecord); - em.persist(selfEntity); - em.flush(); - LOGGER.info(resourceName + " is on Slave Mode"); - } - } else { - // Resource name is null -> No resource with same name. - selfEntity = new BackUpMonitorEntity(); - selfEntity.setResourceNodeName(resourceNodeName); - selfEntity.setResourceName(resourceName); - selfEntity.setTimeStamp(new Date()); - selfEntity = setSlave(selfEntity); - setLastNotification(null); - LOGGER.info("Creating: " + resourceName + " on Slave Mode"); - em.persist(selfEntity); - em.flush(); - } - // Correct the database if any errors and perform monitor checks. - if (masterEntities.size() != 1 || !getFlag()) { - // We are either not master or there are more masters or no masters. - if (masterEntities.isEmpty()) { - // No Masters is a problem Convert ourselves to Master. - selfEntity = setMaster(selfEntity); - selfEntity.setTimeStamp(new Date()); - selfEntity.setNotificationRecord(notificationRecord); - LOGGER.info(resourceName + " changed to Master Mode - No Masters available."); - em.persist(selfEntity); - em.flush(); - } else { - if (masterEntities.size() > 1) { - // More Masters is a problem, Fix the issue by looking for the latest one and make others - // Slave. - BackUpMonitorEntity masterEntity = null; - for (BackUpMonitorEntity currentEntity : masterEntities) { - if (currentEntity.getFlag().equalsIgnoreCase(MASTER)) { - if (masterEntity == null) { - masterEntity = currentEntity; - } else if (currentEntity.getTimeStamp().getTime() > masterEntity.getTimeStamp() - .getTime()) { - // False Master, Update master to slave and take currentMaster as Master. - masterEntity.setFlag(SLAVE); - masterEntity.setTimeStamp(new Date()); - em.persist(masterEntity); - em.flush(); - masterEntity = currentEntity; - } else { - currentEntity.setFlag(SLAVE); - currentEntity.setTimeStamp(new Date()); - em.persist(currentEntity); - em.flush(); - } - } - } - masterEntities = new ArrayList<>(); - masterEntities.add(masterEntity); - } - if (masterEntities.size() == 1) { - // Correct Size, Check if Master is Latest, if not Change Master to Slave and Slave to - // Master. - BackUpMonitorEntity masterEntity = masterEntities.get(0); - if (!masterEntity.getResourceName().equals(selfEntity.getResourceName())) { - Date currentTime = new Date(); - long timeDiff; - timeDiff = currentTime.getTime() - masterEntity.getTimeStamp().getTime(); - if (timeDiff > (pingInterval + 1500)) { - // This is down or has an issue and we need to become Master while turning the - // Master to slave. - masterEntity.setFlag(SLAVE); - String lastNotification = null; - if (masterEntity.getNotificationRecord() != null) { - lastNotification = calculatePatch(masterEntity.getNotificationRecord()); - } - setLastNotification(lastNotification); - em.persist(masterEntity); - em.flush(); - // Lets Become Master. - selfEntity = setMaster(selfEntity); - LOGGER.info("Changing " + resourceName + " from slave to Master Mode"); - selfEntity.setTimeStamp(new Date()); - selfEntity.setNotificationRecord(notificationRecord); - em.persist(selfEntity); - em.flush(); - } - } - } else { - LOGGER.error( - "Backup Monitor Issue, Masters out of sync, This will be fixed in next interval."); - } - } - } + checkOtherMaster(bMList); } et.commit(); } catch (Exception e) { @@ -390,6 +268,142 @@ public class BackUpMonitor { } } + private void checkOtherMaster(List<?> bMList) { + // Check for other Master(s) + ArrayList<BackUpMonitorEntity> masterEntities = new ArrayList<>(); + // Check for self. + BackUpMonitorEntity selfEntity = null; + // Check backup monitor entities. + for (int i = 0; i < bMList.size(); i++) { + BackUpMonitorEntity bMEntity = (BackUpMonitorEntity) bMList.get(i); + LOGGER.info("Refreshing Entity. "); + em.refresh(bMEntity); + if (bMEntity.getFlag().equalsIgnoreCase(MASTER)) { + masterEntities.add(bMEntity); + } + if (bMEntity.getResourceName().equals(resourceName)) { + selfEntity = bMEntity; + } + } + if (selfEntity != null) { + LOGGER.info("Resource Name already Exists: " + resourceName); + if (selfEntity.getFlag().equalsIgnoreCase(MASTER)) { + // Already Master Mode. + setFlag(true); + LOGGER.info(resourceName + " is on Master Mode"); + selfEntity.setTimeStamp(new Date()); + selfEntity.setNotificationRecord(notificationRecord); + em.persist(selfEntity); + em.flush(); + setLastNotification(null); + if (!masterEntities.contains(selfEntity)) { + masterEntities.add(selfEntity); + } + } else { + // Already Slave Mode. + setFlag(false); + selfEntity.setTimeStamp(new Date()); + selfEntity.setNotificationRecord(notificationRecord); + em.persist(selfEntity); + em.flush(); + LOGGER.info(resourceName + " is on Slave Mode"); + } + } else { + // Resource name is null -> No resource with same name. + selfEntity = new BackUpMonitorEntity(); + selfEntity.setResourceNodeName(resourceNodeName); + selfEntity.setResourceName(resourceName); + selfEntity.setTimeStamp(new Date()); + selfEntity = setSlave(selfEntity); + setLastNotification(null); + LOGGER.info("Creating: " + resourceName + " on Slave Mode"); + em.persist(selfEntity); + em.flush(); + } + // Correct the database if any errors and perform monitor checks. + if (masterEntities.size() != 1 || !getFlag()) { + // We are either not master or there are more masters or no masters. + if (masterEntities.isEmpty()) { + // No Masters is a problem Convert ourselves to Master. + selfEntity = setMaster(selfEntity); + selfEntity.setTimeStamp(new Date()); + selfEntity.setNotificationRecord(notificationRecord); + LOGGER.info(resourceName + " changed to Master Mode - No Masters available."); + em.persist(selfEntity); + em.flush(); + } else { + if (masterEntities.size() > 1) { + masterEntities = multipleMasterEntity(masterEntities); + } + if (masterEntities.size() == 1) { + singleMasterEntity(masterEntities, selfEntity); + } else { + LOGGER.error( + "Backup Monitor Issue, Masters out of sync, This will be fixed in next interval."); + } + } + } + } + + private void singleMasterEntity(ArrayList<BackUpMonitorEntity> masterEntities, BackUpMonitorEntity selfEntity) { + // Correct Size, Check if Master is Latest, if not Change Master to Slave and Slave to + // Master. + BackUpMonitorEntity masterEntity = masterEntities.get(0); + if (!masterEntity.getResourceName().equals(selfEntity.getResourceName())) { + Date currentTime = new Date(); + long timeDiff; + timeDiff = currentTime.getTime() - masterEntity.getTimeStamp().getTime(); + if (timeDiff > (pingInterval + 1500)) { + // This is down or has an issue and we need to become Master while turning the + // Master to slave. + masterEntity.setFlag(SLAVE); + String lastNotification = null; + if (masterEntity.getNotificationRecord() != null) { + lastNotification = calculatePatch(masterEntity.getNotificationRecord()); + } + setLastNotification(lastNotification); + em.persist(masterEntity); + em.flush(); + // Lets Become Master. + selfEntity = setMaster(selfEntity); + LOGGER.info("Changing " + resourceName + " from slave to Master Mode"); + selfEntity.setTimeStamp(new Date()); + selfEntity.setNotificationRecord(notificationRecord); + em.persist(selfEntity); + em.flush(); + } + } + } + + private ArrayList<BackUpMonitorEntity> multipleMasterEntity(ArrayList<BackUpMonitorEntity> masterEntities) { + // More Masters is a problem, Fix the issue by looking for the latest one and make others + // Slave. + BackUpMonitorEntity masterEntity = null; + for (BackUpMonitorEntity currentEntity : masterEntities) { + if (currentEntity.getFlag().equalsIgnoreCase(MASTER)) { + if (masterEntity == null) { + masterEntity = currentEntity; + } else if (currentEntity.getTimeStamp().getTime() > masterEntity.getTimeStamp() + .getTime()) { + // False Master, Update master to slave and take currentMaster as Master. + masterEntity.setFlag(SLAVE); + masterEntity.setTimeStamp(new Date()); + em.persist(masterEntity); + em.flush(); + masterEntity = currentEntity; + } else { + currentEntity.setFlag(SLAVE); + currentEntity.setTimeStamp(new Date()); + em.persist(currentEntity); + em.flush(); + } + } + } + masterEntities = new ArrayList<>(); + masterEntities.add(masterEntity); + return masterEntities; + } + private static void setNotificationRecord() throws BackUpMonitorException { try { notificationRecord = PolicyUtils.objectToJsonString(NotificationStore.getNotificationRecord()); @@ -423,9 +437,9 @@ public class BackUpMonitor { LOGGER.error("Error generating Patched " + e.getMessage(), e); return null; } - } + } - /** + /** * Updates Notification in the Database while Performing the health check. * * @param notification @@ -460,10 +474,10 @@ public class BackUpMonitor { } catch (Exception e) { LOGGER.error("Error in Clients Handler Object : " + e.getMessage(), e); } - - } - // Used to set LastMasterNotification Record. + } + + // Used to set LastMasterNotification Record. private static void setLastNotification(String notification) { synchronized (notificationLock) { lastMasterNotification = notification; |