diff options
author | eramkve <ram.krishna.verma@ericsson.com> | 2018-03-28 12:55:30 +0100 |
---|---|---|
committer | eramkve <ram.krishna.verma@ericsson.com> | 2018-03-28 14:52:28 +0100 |
commit | 5d4859fd479220fa18d17b2f6e7fb5d401771f33 (patch) | |
tree | 7cb4396ac43049167ca75f994ced516ef2bf7e3b | |
parent | 0a91f57af987030eb63bc06a9c4fcafc024387ca (diff) |
Remove checkstyle warnings in policy/engine
Remove checkstyle warnings in policy/engine LogParser
Change-Id: Ia6ecc3ecbac51755262e5825a3f5ce8d89b79e4b
Issue-ID: POLICY-710
Signed-off-by: eramkve <ram.krishna.verma@ericsson.com>
6 files changed, 1591 insertions, 1443 deletions
diff --git a/LogParser/pom.xml b/LogParser/pom.xml index b7f3f1e65..7ef977c94 100644 --- a/LogParser/pom.xml +++ b/LogParser/pom.xml @@ -172,6 +172,42 @@ </execution> </executions> </plugin> + <plugin> + <artifactId>maven-checkstyle-plugin</artifactId> + <executions> + <execution> + <id>onap-java-style</id> + <goals> + <goal>check</goal> + </goals> + <phase>process-sources</phase> + <configuration> + <!-- Use Google Java Style Guide: https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml + with minor changes --> + <configLocation>onap-checkstyle/onap-java-style.xml</configLocation> + <!-- <sourceDirectory> is needed so that checkstyle ignores the generated + sources directory --> + <sourceDirectory>${project.build.sourceDirectory}</sourceDirectory> + <includeResources>true</includeResources> + <includeTestSourceDirectory>true</includeTestSourceDirectory> + <includeTestResources>true</includeTestResources> + <excludes> + </excludes> + <consoleOutput>true</consoleOutput> + <failOnViolation>true</failOnViolation> + <violationSeverity>warning</violationSeverity> + </configuration> + </execution> + </executions> + <dependencies> + <dependency> + <groupId>org.onap.oparent</groupId> + <artifactId>checkstyle</artifactId> + <version>0.1.1</version> + <scope>compile</scope> + </dependency> + </dependencies> + </plugin> </plugins> </build> </project> diff --git a/LogParser/src/main/java/org/onap/xacml/parser/CleanUpSystemLogDB.java b/LogParser/src/main/java/org/onap/xacml/parser/CleanUpSystemLogDB.java deleted file mode 100644 index 9d3189ca3..000000000 --- a/LogParser/src/main/java/org/onap/xacml/parser/CleanUpSystemLogDB.java +++ /dev/null @@ -1,86 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * LogParser - * ================================================================================ - * Copyright (C) 2017 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.onap.xacml.parser; - -import java.text.Format; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.TimerTask; -import java.sql.Connection; -import java.sql.SQLException; - - -import org.onap.policy.common.logging.flexlogger.FlexLogger; - -public class CleanUpSystemLogDB extends TimerTask{ - - private static org.onap.policy.common.logging.flexlogger.Logger logger = FlexLogger.getLogger(CleanUpSystemLogDB.class.getName()); - Connection localConnect = null; - int timeFrame = 5; //default - public CleanUpSystemLogDB(Connection dbConnect, int argTimeFrame) { - localConnect = dbConnect; - if(argTimeFrame > 0){ - timeFrame = argTimeFrame; - } - } - String className = this.getClass().getSimpleName(); - - @Override - public void run() { - - Date date = new Date(); - Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - logger.debug("cleanLogDBTableEntries:Cleanup systemlogdb starts on date:" + formatter.format(date)); - try { - cleanLogDBTableEntries(localConnect, timeFrame); - } catch (SQLException e) { - logger.error(e); - } - - logger.debug(className + " Cleanup systemlogdb done"); - } - - public static void cleanLogDBTableEntries(Connection dbConnect, int timeFrame) throws SQLException { - - Connection connect = dbConnect; - if(dbConnect == null || dbConnect.isClosed()) { - connect = ParseLog.getDbConnection(); - } - try ( - java.sql.PreparedStatement statement = connect - .prepareStatement("DELETE FROM SYSTEMLOGDB WHERE date < DATE_SUB(CURDATE(), INTERVAL ? DAY)"); - ){ - - statement.setInt(1, timeFrame); - - int records = statement.executeUpdate(); - - logger.debug("cleanLogDBTableEntries:deleting Log files ended with " + records + " deleted."); - statement.close(); - - } catch (Exception e) { - logger.error("Failed to create SQLContainer for System Log Database", e); - } finally{ - connect.close(); - } - } -} diff --git a/LogParser/src/main/java/org/onap/xacml/parser/CleanUpSystemLogDb.java b/LogParser/src/main/java/org/onap/xacml/parser/CleanUpSystemLogDb.java new file mode 100644 index 000000000..21ff8bf36 --- /dev/null +++ b/LogParser/src/main/java/org/onap/xacml/parser/CleanUpSystemLogDb.java @@ -0,0 +1,99 @@ +/*- + * ============LICENSE_START======================================================= + * LogParser + * ================================================================================ + * Copyright (C) 2017-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.onap.xacml.parser; + +import java.sql.Connection; +import java.sql.SQLException; +import java.text.Format; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimerTask; + +import org.onap.policy.common.logging.flexlogger.FlexLogger; + +public class CleanUpSystemLogDb extends TimerTask { + + private static org.onap.policy.common.logging.flexlogger.Logger logger = + FlexLogger.getLogger(CleanUpSystemLogDb.class.getName()); + Connection localConnect = null; + int timeFrame = 5; // default + + /** + * Set time frame for database cleanup. + * + * @param dbConnect the database connection object + * @param argTimeFrame the time frame + */ + public CleanUpSystemLogDb(final Connection dbConnect, final int argTimeFrame) { + localConnect = dbConnect; + if (argTimeFrame > 0) { + timeFrame = argTimeFrame; + } + } + + String className = this.getClass().getSimpleName(); + + @Override + public void run() { + + final Date date = new Date(); + final Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + logger.debug("cleanLogDBTableEntries:Cleanup systemlogdb starts on date:" + formatter.format(date)); + try { + cleanLogDbTableEntries(localConnect, timeFrame); + } catch (final SQLException e) { + logger.error(e); + } + + logger.debug(className + " Cleanup systemlogdb done"); + } + + /** + * Clean system log database table entries based on input time frame. + * + * @param dbConnect the database connection object + * @param timeFrame the time frame + * @throws SQLException if an error occurs + */ + public static void cleanLogDbTableEntries(final Connection dbConnect, final int timeFrame) throws SQLException { + + Connection connect = dbConnect; + if (dbConnect == null || dbConnect.isClosed()) { + connect = ParseLog.getDbConnection(); + } + try (java.sql.PreparedStatement statement = + connect.prepareStatement("DELETE FROM SYSTEMLOGDB WHERE date < DATE_SUB(CURDATE(), INTERVAL ? DAY)");) { + + statement.setInt(1, timeFrame); + + final int records = statement.executeUpdate(); + + logger.debug("cleanLogDBTableEntries:deleting Log files ended with " + records + " deleted."); + statement.close(); + + } catch (final Exception e) { + logger.error("Failed to create SQLContainer for System Log Database", e); + } finally { + connect.close(); + } + } +} diff --git a/LogParser/src/main/java/org/onap/xacml/parser/LogEntryObject.java b/LogParser/src/main/java/org/onap/xacml/parser/LogEntryObject.java index 1aeac1726..c38b64742 100644 --- a/LogParser/src/main/java/org/onap/xacml/parser/LogEntryObject.java +++ b/LogParser/src/main/java/org/onap/xacml/parser/LogEntryObject.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * LogParser * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -24,51 +24,62 @@ import java.util.Date; public class LogEntryObject { - private String system; - private String description; - private Date date; - private String remote; - private String systemType; - private LOGTYPE logType; - - public enum LOGTYPE { - INFO, DEBUG, ERROR, SEVERE, WARN; - } - - public String getSystem() { - return system; - } - public void setSystem(String system) { - this.system = system; - } - public String getDescription() { - return description; - } - public void setDescription(String description) { - this.description = description; - } - public Date getDate() { - return date; - } - public void setDate(Date date) { - this.date = date; - } - public String getRemote() { - return remote; - } - public void setRemote(String remote) { - this.remote = remote; - } - public String getSystemType() { - return systemType; - } - public void setSystemType(String systemType) { - this.systemType = systemType; - } - public LOGTYPE getLogType() { - return logType; - } - public void setLogType(LOGTYPE logType) { - this.logType = logType; - } -}
\ No newline at end of file + private String system; + private String description; + private Date date; + private String remote; + private String systemType; + private LogType logType; + + public enum LogType { + INFO, DEBUG, ERROR, SEVERE, WARN; + } + + public String getSystem() { + return system; + } + + public void setSystem(final String system) { + this.system = system; + } + + public String getDescription() { + return description; + } + + public void setDescription(final String description) { + this.description = description; + } + + public Date getDate() { + return date; + } + + public void setDate(final Date date) { + this.date = date; + } + + public String getRemote() { + return remote; + } + + public void setRemote(final String remote) { + this.remote = remote; + } + + public String getSystemType() { + return systemType; + } + + public void setSystemType(final String systemType) { + this.systemType = systemType; + } + + public LogType getLogType() { + return logType; + } + + public void setLogType(final LogType logType) { + this.logType = logType; + } +} 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 5ead822b6..75ccfd477 100644 --- a/LogParser/src/main/java/org/onap/xacml/parser/ParseLog.java +++ b/LogParser/src/main/java/org/onap/xacml/parser/ParseLog.java @@ -7,9 +7,9 @@ * 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. @@ -50,789 +50,867 @@ import org.onap.policy.common.im.IntegrityMonitor; import org.onap.policy.common.im.IntegrityMonitorException; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.utils.CryptoUtils; -import org.onap.xacml.parser.LogEntryObject.LOGTYPE; +import org.onap.xacml.parser.LogEntryObject.LogType; /** * Parse log files and store the information in a H2 database. - * + * * */ public class ParseLog { - - // only logging last line of each log file processed to the log4j log file defined by property - PARSERLOGPATH - private static final Logger log4jlogger = Logger.getLogger(ParseLog.class.getName()); - - // processing logging - private static org.onap.policy.common.logging.flexlogger.Logger logger = FlexLogger.getLogger(ParseLog.class.getName()); - - private static String system; - private static int lastNumberRead = 0; - private static int debuglastNumberRead = 0; - private static int errorlastNumberRead = 0; - private static String type; - private static long startFileSize; - private static long debugStartFileSize; - private static long errorStartFileSize; - private static String systemLogFile; - private static String logFile; - private static String debuglogFile; - private static String errorlogFile; - private static String jdbcUrl; - private static String jdbcUser; - private static String jdbcPassword; - private static String jdbcDriver; - private static int maxLength = 255; //Max length that is allowed in the DB table - private static String resourceName; - private static long sleepTimer = 50000; - static IntegrityMonitor im; - private static boolean isMissingLogFile; - //Default:Timer initial delay and the delay between in milliseconds before task is to be execute - private static final int TIMER_DELAY_TIME = 1000; - //Default:Timer scheduleAtFixedRate period - time in milliseconds between successive task executions - private static int checkInterval = 86400000; // run this clean up once a day - private static String loggingProcess = "Error processing line in "; - private static int defaultTimeFrame = 5; - private static String message =" value read in: "; - private static String lineFormat = "(\\r\\n|\\n)"; - private static String lineRead = "-line-Read:"; - private static String br= "<br />"; - private static String last = "Last-"; - private static String breakLoop = "break the loop."; - private static String dateFormat = "yyyy-MM-dd HH:mm:ss"; - - public static void main(String[] args) throws Exception { - - Properties logProperties = getPropertiesValue("parserlog.properties"); - - if(logProperties == null || isMissingLogFile){ - // missing the path of log file in the properties file, so stop the process - logger.error("logProperties is null or LOGPATH is missing in parserlog.properties, so stop the process."); - return; - } - - //trigger the cleanup systemLogDb timer - startCleanUp(); - - File fileLog = new File(systemLogFile); - - im = IntegrityMonitor.getInstance(resourceName,logProperties ); - - startDebugLogParser(fileLog); - startErrorLogParser(fileLog); - startAPIRestLogParser(fileLog); - - } - - private static boolean processLine(Path debugfilePath, String dataFileName, int lastNmRead, LOGTYPE logType){ - // log4jlogger must use .info - try(Stream<String> lines = Files.lines(debugfilePath, Charset.defaultCharset()).onClose(() -> log4jlogger.info(last+dataFileName+ lineRead + lastNmRead)).skip(lastNmRead)){ - lines.forEachOrdered(line -> process(line, type, logType)); - } catch (IOException e) { - logger.error(loggingProcess + dataFileName, e); - logger.error(breakLoop); - return true; - } - return false; - } - private static void processDebugLogParser(File debugfile, Path debugfilePath, String dataFileName){ - - Runnable runnable = new Runnable (){ - boolean isStop = false; - public void run(){ - while (!isStop){ - if (debugfile.isFile()){ - isStop = processLine(debugfilePath, dataFileName, debuglastNumberRead, LOGTYPE.DEBUG); - } - try { - Thread.sleep(sleepTimer); - debugStartFileSize = countLines(debuglogFile); - } catch (Exception e) { - logger.error(loggingProcess + dataFileName, e); - logger.error(breakLoop); - isStop = true; - } - logger.debug("File Line Count of debug.log: " + debugStartFileSize + message + debuglastNumberRead); - if (debugStartFileSize < debuglastNumberRead ){ - logger.debug("Failed Rolled: set Last number read to 0"); - debuglastNumberRead = 0; - } - } - } - }; - - Thread thread = new Thread(runnable); - thread.start(); - } - - private static void startDebugLogParser(File fileLog) throws IOException{ - - if(debuglogFile != null && !debuglogFile.isEmpty()){ - - // pull the last line number - String dataFileName = "debug.log"; - String filesRead = pullLastLineRead(fileLog, dataFileName); - if (filesRead!= null){ - filesRead = filesRead.replaceAll(lineFormat, br); - debuglastNumberRead= Integer.parseInt(filesRead.trim()); - }else{ - debuglastNumberRead = 0; - } - - debugStartFileSize = countLines(debuglogFile); - if (debugStartFileSize < debuglastNumberRead ){ - logger.error("Filed Rolled: set Last debug number read to 0"); - debuglastNumberRead = 0; - } - - isMissingLogFile = false; - Path debugfilePath = Paths.get(debuglogFile); - File debugfile = new File(debuglogFile); - debugStartFileSize = debugfile.length(); - - // start process debug.log file - processDebugLogParser(debugfile, debugfilePath, dataFileName); - - } - } - - private static void processErrorLogParser(File errorfile, Path errorfilePath, String dataFileName){ - Runnable runnable = new Runnable (){ - boolean isStop = false; - public void run(){ - - while (!isStop){ - if (errorfile.isFile()){ - isStop = processLine(errorfilePath, dataFileName, errorlastNumberRead, LOGTYPE.ERROR); - } - try { - Thread.sleep(sleepTimer); - errorStartFileSize = countLines(errorlogFile); - } catch (Exception e) { - logger.error(loggingProcess + dataFileName, e); - logger.error(breakLoop); - isStop = true; - } - - logger.debug("File Line Count of error.log: " + errorStartFileSize + message + errorlastNumberRead); - if (errorStartFileSize < errorlastNumberRead ){ - logger.debug("Failed Rolled: set Last error number read to 0"); - errorlastNumberRead = 0; - } - } - } - }; - - Thread thread = new Thread(runnable); - thread.start(); - } - - private static void startErrorLogParser(File fileLog) throws IOException{ - - if(errorlogFile != null && !errorlogFile.isEmpty()){ - - // pull the last line number - String dataFileName = "error.log"; - String filesRead = pullLastLineRead(fileLog, dataFileName); - if (filesRead!= null){ - filesRead = filesRead.replaceAll(lineFormat, br); - errorlastNumberRead= Integer.parseInt(filesRead.trim()); - }else{ - errorlastNumberRead = 0; - } - - errorStartFileSize = countLines(errorlogFile); - if (errorStartFileSize < errorlastNumberRead ){ - logger.error("Filed Rolled: set Last error number read to 0"); - errorlastNumberRead = 0; - } - - isMissingLogFile = false; - Path errorfilePath = Paths.get(errorlogFile); - File errorfile = new File(errorlogFile); - errorStartFileSize = errorfile.length(); - // start process error.log file - processErrorLogParser(errorfile, errorfilePath, dataFileName); - - } - } - - private static void processAPIRestLog(File file, Path filePath, String dataFileName){ - - Runnable runnable = new Runnable () { - boolean isStop = false; - public void run(){ - while (!isStop){ - - if (file.isFile()){ - isStop = processLine(filePath, dataFileName, lastNumberRead, LOGTYPE.INFO); - } - try { - Thread.sleep(sleepTimer); - startFileSize = countLines(logFile); - } catch (Exception e) { - logger.error(loggingProcess + dataFileName, e); - logger.error(breakLoop); - isStop = true; - } - - logger.debug("File Line Count of " + dataFileName+": " + startFileSize + message + lastNumberRead); - if (startFileSize < lastNumberRead ){ - logger.debug("Failed Rolled: set Last number read to 0"); - lastNumberRead = 0; - } - } - } - }; - - Thread thread = new Thread(runnable); - thread.start(); - } - - private static void startAPIRestLogParser(File fileLog) throws IOException{ - - if(logFile != null && !logFile.isEmpty()){ - - // pull the last line number - String dataFileName = type.toLowerCase()+"-rest.log"; - String filesRead = pullLastLineRead(fileLog, dataFileName); - if (filesRead!= null){ - filesRead = filesRead.replaceAll(lineFormat, br); - lastNumberRead= Integer.parseInt(filesRead.trim()); - }else{ - lastNumberRead = 0; - } - startFileSize = countLines(logFile); - if (startFileSize < lastNumberRead ){ - logger.error("Filed Rolled: set Last number read to 0"); - lastNumberRead = 0; - } - - isMissingLogFile = false; - Path filePath = Paths.get(logFile); - File file = new File(logFile); - startFileSize = file.length(); - // start process pap/pdp-rest.log file - processAPIRestLog(file, filePath, dataFileName); - } - } - - public static int countLines(String filename){ - int cnt = 0; - try ( - FileReader freader = new FileReader(filename); - LineNumberReader reader = new LineNumberReader(freader); - ) { - String line= null; - while ((line = reader.readLine()) != null) { - logger.debug("Reading the Logs"+line); - } - cnt = reader.getLineNumber(); - logger.info("Line number:"+cnt); - reader.close(); - freader.close(); - - }catch(Exception e){ - logger.error(e); - } - - return cnt; - } - - public static String pullLastLineRead(File file, String dataFileName) throws IOException { - if(!file.exists()){ - file.createNewFile(); - return null; - } - try(RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");){ - 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); - 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)){ - String[] parseString = builder.toString().split(last+dataFileName+lineRead); - String returnValue = parseString[1].replace("\r", ""); - randomAccessFile.close(); - return returnValue.trim(); - } - builder = new StringBuilder(); - } - } - } - randomAccessFile.close(); - } - return null; - } - - private static LogEntryObject getDebugOutLogValue (String line, String type){ - - Date date; - LogEntryObject logEntry = new LogEntryObject(); - logEntry.setSystemType(type); - logEntry.setSystem(system); - String info1 = "||INFO||"; - String info2 = "INFO:"; - String error1 = "||ERROR||"; - String error2 = "ERROR:"; - - if(line.contains(info1) || line.contains(error1) || line.contains(info2) || line.contains(error2)){ - String[] splitString = null; - if(line.contains(info1) || line.contains(error1)){ - splitString = line.split("[||]"); - }else if(line.contains(info2)){ - splitString = line.split(info2); - }else{ - splitString = line.split(error2); - } - String dateString = splitString[0].substring(0, 19); - logEntry.setDescription(splitString[splitString.length-1]); - - //parse out date - date = parseDate(dateString.replace("T", " "), dateFormat, false); - logEntry.setDate(date); - - logEntry.setRemote(parseRemoteSystem(line)); - if (line.contains(info2) || line.contains(info1)){ - logEntry.setLogType(LOGTYPE.INFO); - }else{ - logEntry.setLogType(LOGTYPE.ERROR); - } - - return logEntry; - } - - return null; - } - - private static LogEntryObject getRestAPIOutLogValue (String line, String type){ - Date date; - LogEntryObject logEntry = new LogEntryObject(); - logEntry.setSystemType(type); - logEntry.setSystem(system); - String info3 = "INFO"; + + // only logging last line of each log file processed to the log4j log file defined by property - PARSERLOGPATH + private static final Logger log4jlogger = Logger.getLogger(ParseLog.class.getName()); + + // processing logging + private static org.onap.policy.common.logging.flexlogger.Logger logger = + FlexLogger.getLogger(ParseLog.class.getName()); + + private static String system; + private static int lastNumberRead = 0; + private static int debuglastNumberRead = 0; + private static int errorlastNumberRead = 0; + private static String type; + private static long startFileSize; + private static long debugStartFileSize; + private static long errorStartFileSize; + private static String systemLogFile; + private static String logFile; + private static String debuglogFile; + private static String errorlogFile; + private static String jdbcUrl; + private static String jdbcUser; + private static String jdbcPassword; + private static String jdbcDriver; + private static int maxLength = 255; // Max length that is allowed in the DB table + private static String resourceName; + private static long sleepTimer = 50000; + static IntegrityMonitor im; + private static boolean isMissingLogFile; + // Default:Timer initial delay and the delay between in milliseconds before task is to be execute + private static final int TIMER_DELAY_TIME = 1000; + // Default:Timer scheduleAtFixedRate period - time in milliseconds between successive task executions + private static int checkInterval = 86400000; // run this clean up once a day + private static String loggingProcess = "Error processing line in "; + private static int defaultTimeFrame = 5; + private static String message = " value read in: "; + private static String lineFormat = "(\\r\\n|\\n)"; + private static String lineRead = "-line-Read:"; + private static String br = "<br />"; + private static String last = "Last-"; + private static String breakLoop = "break the loop."; + private static String dateFormat = "yyyy-MM-dd HH:mm:ss"; + + /** + * The main method to start log parsing. + * + * @param args the arguments + * @throws Exception if an error occurs + */ + public static void main(final String[] args) throws Exception { + + final Properties logProperties = getPropertiesValue("parserlog.properties"); + + if (logProperties == null || isMissingLogFile) { + // missing the path of log file in the properties file, so stop the process + logger.error("logProperties is null or LOGPATH is missing in parserlog.properties, so stop the process."); + return; + } + + // trigger the cleanup systemLogDb timer + startCleanUp(); + + final File fileLog = new File(systemLogFile); + + im = IntegrityMonitor.getInstance(resourceName, logProperties); + + startDebugLogParser(fileLog); + startErrorLogParser(fileLog); + startApiRestLogParser(fileLog); + + } + + private static boolean processLine(final Path debugfilePath, final String dataFileName, final int lastNmRead, + final LogType logType) { + // log4jlogger must use .info + try (Stream<String> lines = Files.lines(debugfilePath, Charset.defaultCharset()) + .onClose(() -> log4jlogger.info(last + dataFileName + lineRead + lastNmRead)).skip(lastNmRead)) { + lines.forEachOrdered(line -> process(line, type, logType)); + } catch (final IOException e) { + logger.error(loggingProcess + dataFileName, e); + logger.error(breakLoop); + return true; + } + return false; + } + + private static void processDebugLogParser(final File debugfile, final Path debugfilePath, + final String dataFileName) { + + final Runnable runnable = new Runnable() { + boolean isStop = false; + + @Override + public void run() { + while (!isStop) { + if (debugfile.isFile()) { + isStop = processLine(debugfilePath, dataFileName, debuglastNumberRead, LogType.DEBUG); + } + try { + Thread.sleep(sleepTimer); + debugStartFileSize = countLines(debuglogFile); + } catch (final Exception e) { + logger.error(loggingProcess + dataFileName, e); + logger.error(breakLoop); + isStop = true; + } + logger.debug("File Line Count of debug.log: " + debugStartFileSize + message + debuglastNumberRead); + if (debugStartFileSize < debuglastNumberRead) { + logger.debug("Failed Rolled: set Last number read to 0"); + debuglastNumberRead = 0; + } + } + } + }; + + final Thread thread = new Thread(runnable); + thread.start(); + } + + private static void startDebugLogParser(final File fileLog) throws IOException { + + if (debuglogFile != null && !debuglogFile.isEmpty()) { + + // pull the last line number + final String dataFileName = "debug.log"; + String filesRead = pullLastLineRead(fileLog, dataFileName); + if (filesRead != null) { + filesRead = filesRead.replaceAll(lineFormat, br); + debuglastNumberRead = Integer.parseInt(filesRead.trim()); + } else { + debuglastNumberRead = 0; + } + + debugStartFileSize = countLines(debuglogFile); + if (debugStartFileSize < debuglastNumberRead) { + logger.error("Filed Rolled: set Last debug number read to 0"); + debuglastNumberRead = 0; + } + + isMissingLogFile = false; + final Path debugfilePath = Paths.get(debuglogFile); + final File debugfile = new File(debuglogFile); + debugStartFileSize = debugfile.length(); + + // start process debug.log file + processDebugLogParser(debugfile, debugfilePath, dataFileName); + + } + } + + private static void processErrorLogParser(final File errorfile, final Path errorfilePath, + final String dataFileName) { + final Runnable runnable = new Runnable() { + boolean isStop = false; + + @Override + public void run() { + + while (!isStop) { + if (errorfile.isFile()) { + isStop = processLine(errorfilePath, dataFileName, errorlastNumberRead, LogType.ERROR); + } + try { + Thread.sleep(sleepTimer); + errorStartFileSize = countLines(errorlogFile); + } catch (final Exception e) { + logger.error(loggingProcess + dataFileName, e); + logger.error(breakLoop); + isStop = true; + } + + logger.debug("File Line Count of error.log: " + errorStartFileSize + message + errorlastNumberRead); + if (errorStartFileSize < errorlastNumberRead) { + logger.debug("Failed Rolled: set Last error number read to 0"); + errorlastNumberRead = 0; + } + } + } + }; + + final Thread thread = new Thread(runnable); + thread.start(); + } + + private static void startErrorLogParser(final File fileLog) throws IOException { + + if (errorlogFile != null && !errorlogFile.isEmpty()) { + + // pull the last line number + final String dataFileName = "error.log"; + String filesRead = pullLastLineRead(fileLog, dataFileName); + if (filesRead != null) { + filesRead = filesRead.replaceAll(lineFormat, br); + errorlastNumberRead = Integer.parseInt(filesRead.trim()); + } else { + errorlastNumberRead = 0; + } + + errorStartFileSize = countLines(errorlogFile); + if (errorStartFileSize < errorlastNumberRead) { + logger.error("Filed Rolled: set Last error number read to 0"); + errorlastNumberRead = 0; + } + + isMissingLogFile = false; + final Path errorfilePath = Paths.get(errorlogFile); + final File errorfile = new File(errorlogFile); + errorStartFileSize = errorfile.length(); + // start process error.log file + processErrorLogParser(errorfile, errorfilePath, dataFileName); + + } + } + + private static void processApiRestLog(final File file, final Path filePath, final String dataFileName) { + + final Runnable runnable = new Runnable() { + boolean isStop = false; + + @Override + public void run() { + while (!isStop) { + + if (file.isFile()) { + isStop = processLine(filePath, dataFileName, lastNumberRead, LogType.INFO); + } + try { + Thread.sleep(sleepTimer); + startFileSize = countLines(logFile); + } catch (final Exception e) { + logger.error(loggingProcess + dataFileName, e); + logger.error(breakLoop); + isStop = true; + } + + logger.debug( + "File Line Count of " + dataFileName + ": " + startFileSize + message + lastNumberRead); + if (startFileSize < lastNumberRead) { + logger.debug("Failed Rolled: set Last number read to 0"); + lastNumberRead = 0; + } + } + } + }; + + final Thread thread = new Thread(runnable); + thread.start(); + } + + private static void startApiRestLogParser(final File fileLog) throws IOException { + + if (logFile != null && !logFile.isEmpty()) { + + // pull the last line number + final String dataFileName = type.toLowerCase() + "-rest.log"; + String filesRead = pullLastLineRead(fileLog, dataFileName); + if (filesRead != null) { + filesRead = filesRead.replaceAll(lineFormat, br); + lastNumberRead = Integer.parseInt(filesRead.trim()); + } else { + lastNumberRead = 0; + } + startFileSize = countLines(logFile); + if (startFileSize < lastNumberRead) { + logger.error("Filed Rolled: set Last number read to 0"); + lastNumberRead = 0; + } + + isMissingLogFile = false; + final Path filePath = Paths.get(logFile); + final File file = new File(logFile); + startFileSize = file.length(); + // start process pap/pdp-rest.log file + processApiRestLog(file, filePath, dataFileName); + } + } + + /** + * Count the number of lines in input file. + * + * @param filename the filename + * @return the lines count + */ + public static int countLines(final String filename) { + int cnt = 0; + try (FileReader freader = new FileReader(filename); LineNumberReader reader = new LineNumberReader(freader)) { + String line = null; + while ((line = reader.readLine()) != null) { + logger.debug("Reading the Logs" + line); + } + cnt = reader.getLineNumber(); + logger.info("Line number:" + cnt); + reader.close(); + freader.close(); + + } catch (final Exception e) { + logger.error(e); + } + + return cnt; + } + + /** + * Get the last line read from the input file if exists. + * + * @param file the file + * @param dataFileName the data file name + * @return last line read or null + * @throws IOException if any error occurs + */ + public static String pullLastLineRead(final File file, final String dataFileName) throws IOException { + if (!file.exists()) { + file.createNewFile(); + return null; + } + try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");) { + 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(); + } + } + } + randomAccessFile.close(); + } + return null; + } + + private static LogEntryObject getDebugOutLogValue(final String line, final String type) { + + Date date; + final LogEntryObject logEntry = new LogEntryObject(); + logEntry.setSystemType(type); + logEntry.setSystem(system); + final String info1 = "||INFO||"; + final String info2 = "INFO:"; + final String error1 = "||ERROR||"; + final String error2 = "ERROR:"; + + if (line.contains(info1) || line.contains(error1) || line.contains(info2) || line.contains(error2)) { + String[] splitString = null; + if (line.contains(info1) || line.contains(error1)) { + splitString = line.split("[||]"); + } else if (line.contains(info2)) { + splitString = line.split(info2); + } else { + splitString = line.split(error2); + } + final String dateString = splitString[0].substring(0, 19); + logEntry.setDescription(splitString[splitString.length - 1]); + + // parse out date + date = parseDate(dateString.replace("T", " "), dateFormat, false); + logEntry.setDate(date); + + logEntry.setRemote(parseRemoteSystem(line)); + if (line.contains(info2) || line.contains(info1)) { + logEntry.setLogType(LogType.INFO); + } else { + logEntry.setLogType(LogType.ERROR); + } + + return logEntry; + } + + return null; + } + + private static LogEntryObject getRestApiOutLogValue(final String line, final String type) { + Date date; + final LogEntryObject logEntry = new LogEntryObject(); + logEntry.setSystemType(type); + logEntry.setSystem(system); + final String info3 = "INFO"; // from PDP/PAP rest log file below - if (line.contains(info3) && line.contains(")-")){ - //parse out description - logEntry.setDescription(line.substring(line.indexOf(")-")+3)); - - date = parseDate(line, dateFormat, true); - logEntry.setDate(date); - - logEntry.setRemote(parseRemoteSystem(line)); - logEntry.setLogType(LOGTYPE.INFO); - - return logEntry; - } - - return null; - } - - private static LogEntryObject getInfoOutLogValue (String line, String type){ - Date date; - LogEntryObject logEntry = new LogEntryObject(); - logEntry.setSystemType(type); - logEntry.setSystem(system); - String info3 = "INFO"; - - if (line.contains(info3) && line.contains("--- [")){ - //parse out description - String temp = line.substring(line.indexOf("---")+1); - String[] split = temp.split(":"); - - logEntry.setDescription(split[1]); - - //parse out date - date = parseDate(line, dateFormat, false); - logEntry.setDate(date); - - //remote system - logEntry.setRemote(parseRemoteSystem(line)); - logEntry.setLogType(LOGTYPE.INFO); - - return logEntry; - } - - return null; - - } - private static LogEntryObject getSevereOutLogValue (String line, String type){ - Date date; - LogEntryObject logEntry = new LogEntryObject(); - logEntry.setSystemType(type); - logEntry.setSystem(system); - if (line.contains("SEVERE") && line.contains("[main]")){ - String[] splitString = line.split(" "); - StringBuilder description = new StringBuilder(); - for (int i = 5; i < splitString.length; i++){ - description.append(" " + splitString[i]); - } - - logEntry.setDescription(description.toString()); - //parse out date - date = parseDate(line, dateFormat, false); - logEntry.setDate(date); - logEntry.setLogType(LOGTYPE.SEVERE); - - return logEntry; - } - - if (line.contains("ERROR") && line.contains(")-")){ - //parse out description - StringBuilder description = new StringBuilder(); - description.append(line.substring(line.indexOf(")-")+3)); - //parse out date - date = parseDate(line, dateFormat, true); - logEntry.setDate(date); - logEntry.setDescription(description.toString()); - //remote system - logEntry.setRemote(parseRemoteSystem(line)); - logEntry.setLogType(LOGTYPE.ERROR); - - return logEntry; - } - - return null; - } - - private static LogEntryObject getWarnOutLogValue (String line, String type){ - Date date; - LogEntryObject logEntry = new LogEntryObject(); - logEntry.setSystemType(type); - logEntry.setSystem(system); - if (line.contains("WARN") && line.contains(")-")){ - //parse out description - - logEntry.setDescription(line.substring(line.indexOf(")-")+3)); - - //parse out date - date = parseDate(line, dateFormat, true); - logEntry.setDate(date); - - //remote system - logEntry.setRemote(parseRemoteSystem(line)); - logEntry.setLogType(LOGTYPE.WARN); - - return logEntry; - } - - if (line.contains("WARNING") && type =="PyPDP"){ - String[] splitString = line.split(" "); - StringBuilder description = new StringBuilder(); - - for (int i = 5; i < splitString.length; i++){ - description.append(" " + splitString[i]); - } - - //parse out date - date = parseDate(line, dateFormat, false); - logEntry.setDate(date); - logEntry.setLogType(LOGTYPE.WARN); - logEntry.setDescription(description.toString()); - return logEntry; - } - - return null; - - } - public static LogEntryObject pullOutLogValues(String line, String type){ - - LogEntryObject logEntry = getDebugOutLogValue(line, type); - - if(logEntry == null){ - logEntry = getRestAPIOutLogValue(line, type); - } - if(logEntry == null){ - logEntry = getInfoOutLogValue(line, type); - } - if(logEntry == null){ - logEntry = getSevereOutLogValue(line, type); - } - if(logEntry == null){ - logEntry = getWarnOutLogValue(line, type); - } - - return logEntry; - } - - private static void dbClose(Connection conn) { - try { - conn.close(); - } catch (SQLException e) { - logger.error("Error closing DB Connection: " + e); - - } - } - - public static void process(String line, String type, LOGTYPE logFile) { - - LogEntryObject returnLogValue = null; - if (im!=null){ - try { - im.startTransaction(); - } catch (IntegrityMonitorException e) { - logger.error("Error received" + e); - } - } - returnLogValue = pullOutLogValues(line, type); - - if(logFile.equals(LOGTYPE.DEBUG)){ - debuglastNumberRead++; - }else if(logFile.equals(LOGTYPE.ERROR)){ - errorlastNumberRead++; - }else if(logFile.equals(LOGTYPE.INFO)){ - lastNumberRead++; - } - if (returnLogValue!=null){ - writeDB(returnLogValue); - } - if (im!=null){ - im.endTransaction(); - } - } - - private static void writeDB(LogEntryObject returnLogValue) { - - Connection conn = dbConnection(jdbcDriver, jdbcUrl, jdbcUser,jdbcPassword); - dbAccesss(conn, returnLogValue.getSystem(), returnLogValue.getDescription(), - returnLogValue.getDate(), returnLogValue.getRemote(), - returnLogValue.getSystemType(), returnLogValue.getLogType().toString()); - dbClose(conn); - } - - private static Connection dbConnection(String driver, String jdbc, String user, String pass){ - + if (line.contains(info3) && line.contains(")-")) { + // parse out description + logEntry.setDescription(line.substring(line.indexOf(")-") + 3)); + + date = parseDate(line, dateFormat, true); + logEntry.setDate(date); + + logEntry.setRemote(parseRemoteSystem(line)); + logEntry.setLogType(LogType.INFO); + + return logEntry; + } + + return null; + } + + private static LogEntryObject getInfoOutLogValue(final String line, final String type) { + Date date; + final LogEntryObject logEntry = new LogEntryObject(); + logEntry.setSystemType(type); + logEntry.setSystem(system); + final String info3 = "INFO"; + + if (line.contains(info3) && line.contains("--- [")) { + // parse out description + final String temp = line.substring(line.indexOf("---") + 1); + final String[] split = temp.split(":"); + + logEntry.setDescription(split[1]); + + // parse out date + date = parseDate(line, dateFormat, false); + logEntry.setDate(date); + + // remote system + logEntry.setRemote(parseRemoteSystem(line)); + logEntry.setLogType(LogType.INFO); + + return logEntry; + } + + return null; + + } + + private static LogEntryObject getSevereOutLogValue(final String line, final String type) { + Date date; + final LogEntryObject logEntry = new LogEntryObject(); + logEntry.setSystemType(type); + logEntry.setSystem(system); + if (line.contains("SEVERE") && line.contains("[main]")) { + final String[] splitString = line.split(" "); + final StringBuilder description = new StringBuilder(); + for (int i = 5; i < splitString.length; i++) { + description.append(" " + splitString[i]); + } + + logEntry.setDescription(description.toString()); + // parse out date + date = parseDate(line, dateFormat, false); + logEntry.setDate(date); + logEntry.setLogType(LogType.SEVERE); + + return logEntry; + } + + if (line.contains("ERROR") && line.contains(")-")) { + // parse out description + final StringBuilder description = new StringBuilder(); + description.append(line.substring(line.indexOf(")-") + 3)); + // parse out date + date = parseDate(line, dateFormat, true); + logEntry.setDate(date); + logEntry.setDescription(description.toString()); + // remote system + logEntry.setRemote(parseRemoteSystem(line)); + logEntry.setLogType(LogType.ERROR); + + return logEntry; + } + + return null; + } + + private static LogEntryObject getWarnOutLogValue(final String line, final String type) { + Date date; + final LogEntryObject logEntry = new LogEntryObject(); + logEntry.setSystemType(type); + logEntry.setSystem(system); + if (line.contains("WARN") && line.contains(")-")) { + // parse out description + + logEntry.setDescription(line.substring(line.indexOf(")-") + 3)); + + // parse out date + date = parseDate(line, dateFormat, true); + logEntry.setDate(date); + + // remote system + logEntry.setRemote(parseRemoteSystem(line)); + logEntry.setLogType(LogType.WARN); + + return logEntry; + } + + if (line.contains("WARNING") && type == "PyPDP") { + final String[] splitString = line.split(" "); + final StringBuilder description = new StringBuilder(); + + for (int i = 5; i < splitString.length; i++) { + description.append(" " + splitString[i]); + } + + // parse out date + date = parseDate(line, dateFormat, false); + logEntry.setDate(date); + logEntry.setLogType(LogType.WARN); + logEntry.setDescription(description.toString()); + return logEntry; + } + + return null; + + } + + /** + * Get log values based on provided line and type. + * + * @param line the line + * @param type the type + * @return {@link LogEntryObject} + */ + public static LogEntryObject pullOutLogValues(final String line, final String type) { + + LogEntryObject logEntry = getDebugOutLogValue(line, type); + + if (logEntry == null) { + logEntry = getRestApiOutLogValue(line, type); + } + if (logEntry == null) { + logEntry = getInfoOutLogValue(line, type); + } + if (logEntry == null) { + logEntry = getSevereOutLogValue(line, type); + } + if (logEntry == null) { + logEntry = getWarnOutLogValue(line, type); + } + + return logEntry; + } + + private static void dbClose(final Connection conn) { + try { + conn.close(); + } catch (final SQLException e) { + logger.error("Error closing DB Connection: " + e); + + } + } + + /** + * Process the provided line, type and log file. + * + * @param line the line + * @param type the type + * @param logFile the log type + */ + public static void process(final String line, final String type, final LogType logFile) { + + LogEntryObject returnLogValue = null; + if (im != null) { + try { + im.startTransaction(); + } catch (final IntegrityMonitorException e) { + logger.error("Error received" + e); + } + } + returnLogValue = pullOutLogValues(line, type); + + if (logFile.equals(LogType.DEBUG)) { + debuglastNumberRead++; + } else if (logFile.equals(LogType.ERROR)) { + errorlastNumberRead++; + } else if (logFile.equals(LogType.INFO)) { + lastNumberRead++; + } + if (returnLogValue != null) { + writeDb(returnLogValue); + } + if (im != null) { + im.endTransaction(); + } + } + + private static void writeDb(final LogEntryObject returnLogValue) { + + final Connection conn = dbConnection(jdbcDriver, jdbcUrl, jdbcUser, jdbcPassword); + dbAccesss(conn, returnLogValue.getSystem(), returnLogValue.getDescription(), returnLogValue.getDate(), + returnLogValue.getRemote(), returnLogValue.getSystemType(), returnLogValue.getLogType().toString()); + dbClose(conn); + } + + private static Connection dbConnection(final String driver, final String jdbc, final String user, + final String pass) { + try { - Class.forName(driver); - return DriverManager.getConnection(jdbc, user, pass); - } catch ( Exception e) { - logger.error("Error connecting to DB: " + e); - } - return null; - } - private static void dbAccesss(Connection conn, String system, String description, Date date, String remote, String type, String logType) { - - String sdate = null; - if (date!=null){ - Format formatter = new SimpleDateFormat(dateFormat); - sdate = formatter.format(date); - logger.debug("DBAccesss : sdate : " + sdate); - }else{ - logger.debug("DBAccesss : sdate is null"); - } - - //ensure the length of description is less than the maximumm db char length - if (description.length() > maxLength) { - description = description.substring(0, maxLength); - } - - try ( - PreparedStatement prep = conn.prepareStatement("insert into SYSTEMLOGDB values (NULL, ?, ?, ?, ?, ?, ?);"); - ){ - - prep.setString(1, system); - prep.setString(2, description); - prep.setString(3, remote); - prep.setString(4, type); - prep.setString(5, sdate); - prep.setString(6, logType); - - prep.executeUpdate(); - prep.close(); - - } catch (SQLException e1) { - logger.error("Error trying to excute SQL Statment: " + e1); - } - } - - public static Date parseDate(String dateline, String pattern, boolean singleSplit) { - - Date returnDate; - String[] splitString = dateline.split(" "); - SimpleDateFormat formatter = new SimpleDateFormat(pattern); - if (singleSplit){ - try { - returnDate = formatter.parse(dateline); - } catch (ParseException e) { - logger.error("Unable to parse date for line: " + dateline); - returnDate = null; - } - }else{ - String tmpString = splitString[0] + " " + splitString[1]; - try { - returnDate = formatter.parse(tmpString); - } catch (ParseException e) { - logger.error("Unable to parse date for line: " + dateline); - returnDate = null; - } - } - - return returnDate; - } - - - public static String parseRemoteSystem(String line) { - - if (line.contains("http") && !(line.contains("www.w3.org"))){ - - Pattern pattern = Pattern.compile("://(.+?)/"); - Matcher remote = pattern.matcher(line); - if (remote.find()) - { - return remote.group(1); - } - } - return null; - } - - public static String[] getPaths(String logPath){ - String[] oneFile = null; - if(logPath != null && !logPath.isEmpty()){ - if(logPath.contains(";")){ - return logPath.split(";"); - }else{ - oneFile = new String[1]; - oneFile[0] = logPath; - } - } - - return oneFile; - } - - private static void setCleanUpProperties(String cleanupInterval, String timeFrame){ - if(cleanupInterval != null && !cleanupInterval.isEmpty()){ - int intCheckInterval = Integer.parseInt(cleanupInterval); - if(intCheckInterval > 300000) {//must be longer than 5 minutes - checkInterval = intCheckInterval; - } - }else{ - logger.debug("No value defined for CHECK_INTERVAL in parserlog.properties, so use its default value:" + checkInterval + " milliseconds"); - } - - if(timeFrame != null && !timeFrame.trim().isEmpty()){ - int intTimeFrame = defaultTimeFrame; - try{ - intTimeFrame = Integer.parseInt(timeFrame); - }catch(NumberFormatException e){ - logger.debug("Improper value defined for TIME_FRAME in parserlog.properties, so use its default value:" + defaultTimeFrame + " days"); - } - if(intTimeFrame > 0){ - defaultTimeFrame = intTimeFrame; - } - }else{ - logger.debug("No value defined for TIME_FRAME in parserlog.properties, so use its default value:" + defaultTimeFrame + " days"); - } - } - - private static void setDebuglogFile(String fileName){ - debuglogFile = fileName; - if(debuglogFile != null && !debuglogFile.isEmpty()){ - debuglogFile = debuglogFile.trim(); - }else{ - debuglogFile = null; - } - } - - private static void setErrorlogFile(String fileName){ - errorlogFile = fileName; - if(errorlogFile != null && !errorlogFile.isEmpty()){ - errorlogFile = errorlogFile.trim(); - }else{ - errorlogFile = null; - } - } - - private static void setLogFileProperties(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]); - }else { - // get path of default file - logFile = splitString[i]; - if(logFile != null && !logFile.isEmpty()){ - logFile = logFile.trim(); - }else{ - logFile = null; - } - } + Class.forName(driver); + return DriverManager.getConnection(jdbc, user, pass); + } catch (final Exception e) { + logger.error("Error connecting to DB: " + e); + } + return null; + } + + private static void dbAccesss(final Connection conn, final String system, String description, final Date date, + final String remote, final String type, final String logType) { + + String sdate = null; + if (date != null) { + final Format formatter = new SimpleDateFormat(dateFormat); + sdate = formatter.format(date); + logger.debug("DBAccesss : sdate : " + sdate); + } else { + logger.debug("DBAccesss : sdate is null"); + } + + // ensure the length of description is less than the maximumm db char length + if (description.length() > maxLength) { + description = description.substring(0, maxLength); + } + + try (PreparedStatement prep = + conn.prepareStatement("insert into SYSTEMLOGDB values (NULL, ?, ?, ?, ?, ?, ?);");) { + + prep.setString(1, system); + prep.setString(2, description); + prep.setString(3, remote); + prep.setString(4, type); + prep.setString(5, sdate); + prep.setString(6, logType); + + prep.executeUpdate(); + prep.close(); + + } catch (final SQLException e1) { + logger.error("Error trying to excute SQL Statment: " + e1); + } + } + + /** + * Parse the given date string based on the provided pattern with/without split based on input boolean value. + * + * @param dateline the date string + * @param pattern the provided pattern + * @param singleSplit the single split boolean value + * @return the parsed date or null + */ + public static Date parseDate(final String dateline, final String pattern, final boolean singleSplit) { + + Date returnDate; + final String[] splitString = dateline.split(" "); + final SimpleDateFormat formatter = new SimpleDateFormat(pattern); + if (singleSplit) { + try { + returnDate = formatter.parse(dateline); + } catch (final ParseException e) { + logger.error("Unable to parse date for line: " + dateline); + returnDate = null; + } + } else { + final String tmpString = splitString[0] + " " + splitString[1]; + try { + returnDate = formatter.parse(tmpString); + } catch (final ParseException e) { + logger.error("Unable to parse date for line: " + dateline); + returnDate = null; + } + } + + return returnDate; + } + + + /** + * Get remote system host from given string if exists. + * + * @param line the input string + * @return system host or null + */ + public static String parseRemoteSystem(final String line) { + + if (line.contains("http") && !(line.contains("www.w3.org"))) { + + final Pattern pattern = Pattern.compile("://(.+?)/"); + final Matcher remote = pattern.matcher(line); + if (remote.find()) { + return remote.group(1); } - } - } - - public static Properties getPropertiesValue(String fileName) { - Properties config = new Properties(); - 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"); - String logFiles = config.getProperty("LOGPATH"); - String cleanupInterval= config.getProperty("CHECK_INTERVAL"); - String timeFrame = config.getProperty("TIME_FRAME"); - - setCleanUpProperties(cleanupInterval, timeFrame); - - if(logFiles == null || logFiles.isEmpty()){ - isMissingLogFile = true; - return null; - } - - String[] splitString = getPaths(logFiles); - - 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 (IOException e) { - logger.error("Error porcessing Config file will be unable to create Health Check" + e); - }catch(Exception e){ - logger.error("Error getPropertiesValue on TIME_FRAME", e); - logger.debug("Error getPropertiesValue on TIME_FRAME, so use its default value:" + defaultTimeFrame + " days"); - } - } - - }else{ - - logger.debug("File doesn't exist in the specified Path " + file.toString()); - } - return null; - } - - public static Connection getDbConnection(){ - return dbConnection(jdbcDriver, jdbcUrl, jdbcUser,jdbcPassword); - } - private static void startCleanUp(){ - Connection conn = dbConnection(jdbcDriver, jdbcUrl, jdbcUser,jdbcPassword); - CleanUpSystemLogDB cleanUp = new CleanUpSystemLogDB(conn, defaultTimeFrame); - Timer timer = new Timer(true); - timer.scheduleAtFixedRate(cleanUp, TIMER_DELAY_TIME, checkInterval); - logger.info("startCleanUp begins! : " + new Date()); - } -}
\ No newline at end of file + } + return null; + } + + /** + * Get the log paths provided in input string. + * + * @param logPath the string of log paths + * @return log paths + */ + public static String[] getPaths(final String logPath) { + String[] oneFile = null; + if (logPath != null && !logPath.isEmpty()) { + if (logPath.contains(";")) { + return logPath.split(";"); + } else { + oneFile = new String[1]; + oneFile[0] = logPath; + } + } + + return oneFile; + } + + private static void setCleanUpProperties(final String cleanupInterval, final String timeFrame) { + if (cleanupInterval != null && !cleanupInterval.isEmpty()) { + final int intCheckInterval = Integer.parseInt(cleanupInterval); + if (intCheckInterval > 300000) { // must be longer than 5 minutes + checkInterval = intCheckInterval; + } + } else { + logger.debug("No value defined for CHECK_INTERVAL in parserlog.properties, so use its default value:" + + checkInterval + " milliseconds"); + } + + if (timeFrame != null && !timeFrame.trim().isEmpty()) { + int intTimeFrame = defaultTimeFrame; + try { + intTimeFrame = Integer.parseInt(timeFrame); + } catch (final NumberFormatException e) { + logger.debug("Improper value defined for TIME_FRAME in parserlog.properties, so use its default value:" + + defaultTimeFrame + " days"); + } + if (intTimeFrame > 0) { + defaultTimeFrame = intTimeFrame; + } + } else { + logger.debug("No value defined for TIME_FRAME in parserlog.properties, so use its default value:" + + defaultTimeFrame + " days"); + } + } + + private static void setDebuglogFile(final String fileName) { + debuglogFile = fileName; + if (debuglogFile != null && !debuglogFile.isEmpty()) { + debuglogFile = debuglogFile.trim(); + } else { + debuglogFile = null; + } + } + + private static void setErrorlogFile(final String fileName) { + errorlogFile = fileName; + if (errorlogFile != null && !errorlogFile.isEmpty()) { + errorlogFile = errorlogFile.trim(); + } else { + errorlogFile = null; + } + } + + 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]); + } else { + // get path of default file + logFile = splitString[i]; + if (logFile != null && !logFile.isEmpty()) { + logFile = logFile.trim(); + } else { + logFile = null; + } + } + } + } + } + + /** + * Get all the properties from file with given file name. + * + * @param fileName the filename + * @return properties from file or null + */ + 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; + } + + final String[] splitString = getPaths(logFiles); + + 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"); + } + } + + } else { + + logger.debug("File doesn't exist in the specified Path " + file.toString()); + } + return null; + } + + public static Connection getDbConnection() { + return dbConnection(jdbcDriver, jdbcUrl, jdbcUser, jdbcPassword); + } + + private static void startCleanUp() { + final Connection conn = dbConnection(jdbcDriver, jdbcUrl, jdbcUser, jdbcPassword); + final CleanUpSystemLogDb cleanUp = new CleanUpSystemLogDb(conn, defaultTimeFrame); + final Timer timer = new Timer(true); + timer.scheduleAtFixedRate(cleanUp, TIMER_DELAY_TIME, checkInterval); + logger.info("startCleanUp begins! : " + new Date()); + } +} diff --git a/LogParser/src/test/java/org/onap/xacml/parser/ParseLogTest.java b/LogParser/src/test/java/org/onap/xacml/parser/ParseLogTest.java index 91de46440..313750eeb 100644 --- a/LogParser/src/test/java/org/onap/xacml/parser/ParseLogTest.java +++ b/LogParser/src/test/java/org/onap/xacml/parser/ParseLogTest.java @@ -2,14 +2,14 @@ * ============LICENSE_START======================================================= * LogParser * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2017-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. @@ -45,536 +45,546 @@ import org.onap.policy.common.im.IntegrityMonitorException; import org.onap.policy.common.im.StandbyStatusException; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; -import org.onap.xacml.parser.LogEntryObject.LOGTYPE; +import org.onap.xacml.parser.LogEntryObject.LogType; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -@PrepareForTest({ParseLogTest.class, IntegrityMonitor.class}) +@PrepareForTest({ ParseLogTest.class, IntegrityMonitor.class }) @RunWith(PowerMockRunner.class) public class ParseLogTest { - - private static Logger logger = FlexLogger.getLogger(ParseLogTest.class); - private Properties config = new Properties(); - private String configFile; - private String configFileDebug; - private String configFileError; - private String testFile1; - private String testFile2; - private IntegrityMonitor im; - - @Before - public void setUp() throws Exception { - System.setProperty("com.sun.management.jmxremote.port", "9998"); - im = Mockito.mock(IntegrityMonitor.class); - String regex = "^\\/[a-zA-Z]\\:\\/"; - - try { - Mockito.doNothing().when(im).startTransaction(); - } catch (StandbyStatusException | AdministrativeStateException e) { - fail(); - } - Mockito.doNothing().when(im).endTransaction(); - ClassLoader classLoader = getClass().getClassLoader(); - configFile = classLoader.getResource("test_config.properties").getFile(); - configFileDebug = classLoader.getResource("test_config_debug.properties").getFile(); - configFileError = classLoader.getResource("test_config_error.properties").getFile(); - Pattern pattern = Pattern.compile(regex); - Matcher matcher = pattern.matcher(configFile); - Matcher matcherDebug = pattern.matcher(configFileDebug); - Matcher matcherError = pattern.matcher(configFileError); - - if (matcher.find()) { - configFile = configFile.substring(1); - } - if (matcherDebug.find()) { - configFileDebug = configFileDebug.substring(1); - } - if (matcherError.find()) { - configFileError = configFileError.substring(1); - } - testFile1 = classLoader.getResource("LineTest.txt").getFile(); - testFile2 = classLoader.getResource("LineTest2.txt").getFile(); - - } - - @After - public void tearDown() { - - logger.debug("tearDown: enter"); - - File file = new File("nonExistFile.txt"); - file.delete(); - logger.debug("tearDown: exit"); - } - - @Test - public void testCountLines() throws IOException { - - logger.debug("testCountLines: enter"); - - int returnValue = ParseLog.countLines(testFile1); - logger.debug("testCountLines: returnValue: " + returnValue); - assertEquals(12, returnValue); - - logger.debug("testCountLines: exit"); - } - - @Test - public void testParseRemoteSystem() { - - logger.debug("testParseRemoteSystem: enter"); - - String line = "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'"; - String returnValue = ParseLog.parseRemoteSystem(line); - logger.debug("testParseRemoteSystem: returnValue: " + returnValue); - assertEquals("localhost:8081", returnValue); - - logger.debug("testParseRemoteSystem: exit"); - } - - @Test - public void testGetPropertiesValue() { - - logger.debug("testGetPropertiesValue: enter"); - - config = new Properties(); - config.put("RESOURCE_NAME", "logparser_pap01"); - config.put("JDBC_DRIVER" ,"org.mariadb.jdbc.Driver"); - config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/"); - config.put("JDBC_USER", "root"); - config.put("JDBC_PASSWORD", "password"); - config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi"); - config.put("SERVER", "password"); - config.put("JDBC_PASSWORD", "https://localhost:9091/pap/"); - config.put("LOGTYPE", "PAP"); - config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log"); - config.put("PARSERLOGPATH", "IntegrityMonitor.log"); - - Properties returnConfig = ParseLog.getPropertiesValue(configFile); - logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig); - assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME")); - - logger.debug("testGetPropertiesValue: exit"); - } - - @Test - public void testGetPropertiesValue_1() { - - logger.debug("testGetPropertiesValue: enter"); - - config = new Properties(); - config.put("RESOURCE_NAME", "logparser_pap01"); - config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver"); - config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/"); - config.put("JDBC_USER", "root"); - config.put("JDBC_PASSWORD", "password"); - config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi"); - config.put("SERVER", "password"); - config.put("JDBC_PASSWORD", "https://localhost:9091/pap/"); - config.put("LOGTYPE", "PAP"); - config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log"); - config.put("PARSERLOGPATH", "IntegrityMonitor.log"); - - final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug); - logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig); - assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME")); - - logger.debug("testGetPropertiesValue_1: exit"); - } - - @Test - public void testGetPropertiesValue_2() { - - logger.debug("testGetPropertiesValue: enter"); - - config = new Properties(); - config.put("RESOURCE_NAME", "logparser_pap01"); - config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver"); - config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/"); - config.put("JDBC_USER", "root"); - config.put("JDBC_PASSWORD", "password"); - config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi"); - config.put("SERVER", "password"); - config.put("JDBC_PASSWORD", "https://localhost:9091/pap/"); - config.put("LOGTYPE", "PAP"); - config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log"); - config.put("PARSERLOGPATH", "IntegrityMonitor.log"); - - final Properties returnConfig = ParseLog.getPropertiesValue(configFileError); - logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig); - assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME")); - - logger.debug("testGetPropertiesValue_2: exit"); - } - - @Test - public void testGetPropertiesFail() { - - logger.debug("testGetPropertiesFail: enter"); - - Properties returnValue = ParseLog.getPropertiesValue("nonExistFile"); - logger.debug("testGetPropertiesFail: returnValue: " + returnValue); - assertEquals(null, returnValue); - - logger.debug("testGetPropertiesFail: exit"); - } - - @Test - public void testParseDate(){ - - logger.debug("testParseDate: enter"); - - String line = "2016-02-23 08:07:30"; - Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false); - logger.debug("testParseDate: returnValue: " + returnValue); - line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30")+3); - assertEquals("Tue Feb 23 08:07:30", line); - - logger.debug("testParseDate: exit"); - } - - @Test - public void testPullLastLineRead(){ - - logger.debug("testPullLastLineRead: enter"); - File file = new File(testFile1); - String returnValue = null; - try { - returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log"); - logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue); - } catch (IOException e) { - fail(); - } - assertEquals("52", returnValue); - - try { - returnValue = ParseLog.pullLastLineRead(file, "debug.log"); - logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue); - } catch (IOException e) { - fail(); - } - assertEquals("17", returnValue); - - try { - returnValue = ParseLog.pullLastLineRead(file, "error.log"); - logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue); - } catch (IOException e) { - fail(); - } - assertEquals("22", returnValue); - - logger.debug("testPullLastLineRead: exit"); - } - - @Test - public void testPullLastLineReadNoFile(){ - - logger.debug("testPullLastLineReadNoFile: enter"); - - File file = new File("nonExistFile.txt"); - try { - assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest")); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullLastLineReadNoFile: exit"); - } - - @Test - public void testPullLastLineReadFail(){ - - logger.debug("testPullLastLineReadFail: enter"); - - File file = new File(testFile2); - try { - assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest")); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullLastLineReadFail: exit"); - } - - @Test - public void testPullOutLogValues(){ - - logger.debug("testPullOutLogValues: enter"); - //ERROR_VALUE - // Open the file - FileInputStream fstream; - try { - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR"); - assertEquals("ERROR_VALUE", retrunObject.getDescription()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValues: exit"); - } - - @Test - public void testGetPaths(){ - - logger.debug("testGetPaths: enter"); - - try { - // valid test - String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log"; - String [] retrunObject = ParseLog.getPaths(logPaths); - assertEquals(3, retrunObject.length); - - // valid test - logPaths = "C:\\pap-log\\pap-rest.log"; - retrunObject = ParseLog.getPaths(logPaths); - assertEquals(1, retrunObject.length); - - // invalid test - logPaths = ""; - retrunObject = ParseLog.getPaths(logPaths); - assertTrue(retrunObject == null); - - } catch (Exception e) { - fail(); - } - - logger.debug("testGetPaths: exit"); - } - - @Test - public void testPullOutLogValuesSecond(){ - - logger.debug("testPullOutLogValuesSecond: enter"); - //ERROR_VALUE - // Open the file - FileInputStream fstream; - try { - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - strLine = br.readLine(); - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO"); - assertEquals(LOGTYPE.INFO, retrunObject.getLogType()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValuesSecond: exit"); - } - - @Test - public void testPullOutLogValuesThird(){ - - logger.debug("testPullOutLogValuesThird: enter"); - //ERROR_VALUE - // Open the file - FileInputStream fstream; - try { - int number = 3; - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - for (int i =0; i < number; i++){ - strLine = br.readLine(); - } - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP"); - assertEquals(LOGTYPE.INFO, retrunObject.getLogType()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValuesThird: exit"); - } - - @Test - public void testPullOutLogValuesFourth(){ - - logger.debug("testPullOutLogValuesFourth: enter"); - // Open the file - FileInputStream fstream; - try { - int number = 4; - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - for (int i =0; i < number; i++){ - strLine = br.readLine(); - } - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP"); - assertEquals(LOGTYPE.INFO, retrunObject.getLogType()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValuesFourth: exit"); - } - - @Test - public void testPullOutLogValuesFith(){ - - logger.debug("testPullOutLogValuesFith: enter"); - // Open the file - FileInputStream fstream; - try { - int number = 5; - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - for (int i =0; i < number; i++){ - strLine = br.readLine(); - } - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP"); - assertEquals(LOGTYPE.WARN, retrunObject.getLogType()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValuesFith: exit"); - } - - @Test - public void testPullOutLogValuesSixth(){ - - logger.debug("testPullOutLogValuesSixth: enter"); - // Open the file - FileInputStream fstream; - try { - int number = 6; - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - for (int i =0; i < number; i++){ - strLine = br.readLine(); - } - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP"); - assertEquals(LOGTYPE.SEVERE, retrunObject.getLogType()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValuesSixth: exit"); - } - - @Test - public void testPullOutLogValuesSeven(){ - - logger.debug("testPullOutLogValuesSeven: enter"); - // Open the file - FileInputStream fstream; - try { - int number = 7; - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - for (int i =0; i < number; i++){ - strLine = br.readLine(); - } - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console"); - assertEquals(LOGTYPE.ERROR, retrunObject.getLogType()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValuesSeven: exit"); - } - - @Test - public void testPullOutLogValuesEight(){ - - logger.debug("testPullOutLogValuesEight: enter"); - // Open the file - FileInputStream fstream; - try { - int number = 8; - fstream = new FileInputStream(testFile1); - BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); - String strLine = br.readLine(); - for (int i =0; i < number; i++){ - strLine = br.readLine(); - } - LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap"); - assertEquals(LOGTYPE.WARN, retrunObject.getLogType()); - br.close(); - } catch (IOException e) { - fail(); - } - - logger.debug("testPullOutLogValuesEight: exit"); - } - - @Test - public void testPullOutLogValuesNull(){ - - logger.debug("testPullOutLogValuesNull: enter"); - // Open the file - LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console"); - assertEquals(null, retrunObject); - - logger.debug("testPullOutLogValuesNull: exit"); - } - - @Test - public void testLogEntryObject(){ - - logger.debug("testLogEntryObject: enter"); - - Date date = new Date(); - - // Open the file - LogEntryObject logObject = new LogEntryObject(); - logObject.setSystem("vm02"); - logObject.setSystemType("pap"); - logObject.setDate(date); - logObject.setRemote("remote"); - - assertEquals("vm02", logObject.getSystem()); - assertEquals("pap", logObject.getSystemType()); - assertEquals(date, logObject.getDate()); - assertEquals("remote", logObject.getRemote()); - - logger.debug("testLogEntryObject: exit"); - } - - @Test - public void testProcess(){ - - logger.debug("testProcess: enter"); - - String line = "2015-04-01 09:13:44.947 DEBUG 17482 --- [nio-8480-exec-7] c.a.l.onap.policy.std.StdPolicyConfig : config Retrieved "; - - im = Mockito.mock(IntegrityMonitor.class); - try { - Mockito.doNothing().when(im).startTransaction(); - } catch (IntegrityMonitorException e) { - fail(); - } - Mockito.doNothing().when(im).endTransaction(); - ParseLog.process(line, "pap", LOGTYPE.INFO); - - logger.debug("testProcess: exit"); - } - - @Test - public void testMain() { - try { - ParseLog.main(new String[] {}); - } catch (final Exception e) { - logger.debug("exception occured while executing the test: exit"); - } - } - - @Test + + private static Logger logger = FlexLogger.getLogger(ParseLogTest.class); + private Properties config = new Properties(); + private String configFile; + private String configFileDebug; + private String configFileError; + private String testFile1; + private String testFile2; + private IntegrityMonitor im; + + /** + * Setup for the test case execution. + * + * @throws Exception if any error occurs + */ + @Before + public void setUp() throws Exception { + System.setProperty("com.sun.management.jmxremote.port", "9998"); + im = Mockito.mock(IntegrityMonitor.class); + final String regex = "^\\/[a-zA-Z]\\:\\/"; + + try { + Mockito.doNothing().when(im).startTransaction(); + } catch (StandbyStatusException | AdministrativeStateException e) { + fail(); + } + Mockito.doNothing().when(im).endTransaction(); + final ClassLoader classLoader = getClass().getClassLoader(); + configFile = classLoader.getResource("test_config.properties").getFile(); + configFileDebug = classLoader.getResource("test_config_debug.properties").getFile(); + configFileError = classLoader.getResource("test_config_error.properties").getFile(); + final Pattern pattern = Pattern.compile(regex); + final Matcher matcher = pattern.matcher(configFile); + final Matcher matcherDebug = pattern.matcher(configFileDebug); + final Matcher matcherError = pattern.matcher(configFileError); + + if (matcher.find()) { + configFile = configFile.substring(1); + } + if (matcherDebug.find()) { + configFileDebug = configFileDebug.substring(1); + } + if (matcherError.find()) { + configFileError = configFileError.substring(1); + } + testFile1 = classLoader.getResource("LineTest.txt").getFile(); + testFile2 = classLoader.getResource("LineTest2.txt").getFile(); + + } + + /** + * Cleaning off after test case execution. + */ + @After + public void tearDown() { + + logger.debug("tearDown: enter"); + + final File file = new File("nonExistFile.txt"); + file.delete(); + logger.debug("tearDown: exit"); + } + + @Test + public void testCountLines() throws IOException { + + logger.debug("testCountLines: enter"); + + final int returnValue = ParseLog.countLines(testFile1); + logger.debug("testCountLines: returnValue: " + returnValue); + assertEquals(12, returnValue); + + logger.debug("testCountLines: exit"); + } + + @Test + public void testParseRemoteSystem() { + + logger.debug("testParseRemoteSystem: enter"); + + final String line = + "||org.onap.policy.pap.xacml.rest.XACMLPapServlet$Heartbeat.run(XACMLPapServlet.java:2801)||Heartbeat 'https://localhost:8081/pdp/' status='UP_TO_DATE'"; + final String returnValue = ParseLog.parseRemoteSystem(line); + logger.debug("testParseRemoteSystem: returnValue: " + returnValue); + assertEquals("localhost:8081", returnValue); + + logger.debug("testParseRemoteSystem: exit"); + } + + @Test + public void testGetPropertiesValue() { + + logger.debug("testGetPropertiesValue: enter"); + + config = new Properties(); + config.put("RESOURCE_NAME", "logparser_pap01"); + config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver"); + config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/"); + config.put("JDBC_USER", "root"); + config.put("JDBC_PASSWORD", "password"); + config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi"); + config.put("SERVER", "password"); + config.put("JDBC_PASSWORD", "https://localhost:9091/pap/"); + config.put("LOGTYPE", "PAP"); + config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\pap-rest.log"); + config.put("PARSERLOGPATH", "IntegrityMonitor.log"); + + final Properties returnConfig = ParseLog.getPropertiesValue(configFile); + logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig); + assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME")); + + logger.debug("testGetPropertiesValue: exit"); + } + + @Test + public void testGetPropertiesValue_1() { + + logger.debug("testGetPropertiesValue: enter"); + + config = new Properties(); + config.put("RESOURCE_NAME", "logparser_pap01"); + config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver"); + config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/"); + config.put("JDBC_USER", "root"); + config.put("JDBC_PASSWORD", "password"); + config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi"); + config.put("SERVER", "password"); + config.put("JDBC_PASSWORD", "https://localhost:9091/pap/"); + config.put("LOGTYPE", "PAP"); + config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\debug\\pap-rest.log"); + config.put("PARSERLOGPATH", "IntegrityMonitor.log"); + + final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug); + logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig); + assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME")); + + logger.debug("testGetPropertiesValue_1: exit"); + } + + @Test + public void testGetPropertiesValue_2() { + + logger.debug("testGetPropertiesValue: enter"); + + config = new Properties(); + config.put("RESOURCE_NAME", "logparser_pap01"); + config.put("JDBC_DRIVER", "org.mariadb.jdbc.Driver"); + config.put("JDBC_URL", "jdbc:mariadb://localhost:3306/"); + config.put("JDBC_USER", "root"); + config.put("JDBC_PASSWORD", "password"); + config.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://localhost:9998/jmxrmi"); + config.put("SERVER", "password"); + config.put("JDBC_PASSWORD", "https://localhost:9091/pap/"); + config.put("LOGTYPE", "PAP"); + config.put("LOGPATH", "C:\\Workspaces\\HealthCheck\\error\\pap-rest.log"); + config.put("PARSERLOGPATH", "IntegrityMonitor.log"); + + final Properties returnConfig = ParseLog.getPropertiesValue(configFileError); + logger.debug("testGetPropertiesValue: returnConfig: " + returnConfig); + assertEquals(config.get("RESOURCE_NAME"), returnConfig.get("RESOURCE_NAME")); + + logger.debug("testGetPropertiesValue_2: exit"); + } + + @Test + public void testGetPropertiesFail() { + + logger.debug("testGetPropertiesFail: enter"); + + final Properties returnValue = ParseLog.getPropertiesValue("nonExistFile"); + logger.debug("testGetPropertiesFail: returnValue: " + returnValue); + assertEquals(null, returnValue); + + logger.debug("testGetPropertiesFail: exit"); + } + + @Test + public void testParseDate() { + + logger.debug("testParseDate: enter"); + + String line = "2016-02-23 08:07:30"; + final Date returnValue = ParseLog.parseDate(line, "yyyy-MM-dd HH:mm:ss", false); + logger.debug("testParseDate: returnValue: " + returnValue); + line = returnValue.toString().substring(0, returnValue.toString().lastIndexOf(":30") + 3); + assertEquals("Tue Feb 23 08:07:30", line); + + logger.debug("testParseDate: exit"); + } + + @Test + public void testPullLastLineRead() { + + logger.debug("testPullLastLineRead: enter"); + final File file = new File(testFile1); + String returnValue = null; + try { + returnValue = ParseLog.pullLastLineRead(file, "pap-rest.log"); + logger.debug("testPullLastLineRead: returnValue for pap-rest.log: " + returnValue); + } catch (final IOException e) { + fail(); + } + assertEquals("52", returnValue); + + try { + returnValue = ParseLog.pullLastLineRead(file, "debug.log"); + logger.debug("testPullLastLineRead: returnValue for debug.log: " + returnValue); + } catch (final IOException e) { + fail(); + } + assertEquals("17", returnValue); + + try { + returnValue = ParseLog.pullLastLineRead(file, "error.log"); + logger.debug("testPullLastLineRead: returnValue for error.log: " + returnValue); + } catch (final IOException e) { + fail(); + } + assertEquals("22", returnValue); + + logger.debug("testPullLastLineRead: exit"); + } + + @Test + public void testPullLastLineReadNoFile() { + + logger.debug("testPullLastLineReadNoFile: enter"); + + final File file = new File("nonExistFile.txt"); + try { + assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest")); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullLastLineReadNoFile: exit"); + } + + @Test + public void testPullLastLineReadFail() { + + logger.debug("testPullLastLineReadFail: enter"); + + final File file = new File(testFile2); + try { + assertEquals(null, ParseLog.pullLastLineRead(file, "pap-rest")); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullLastLineReadFail: exit"); + } + + @Test + public void testPullOutLogValues() { + + logger.debug("testPullOutLogValues: enter"); + // ERROR_VALUE + // Open the file + FileInputStream fstream; + try { + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + final String strLine = br.readLine(); + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "ERROR"); + assertEquals("ERROR_VALUE", retrunObject.getDescription()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValues: exit"); + } + + @Test + public void testGetPaths() { + + logger.debug("testGetPaths: enter"); + + try { + // valid test + String logPaths = "C:\\pap-log\\pap-rest.log;C:\\pap-log\\debug.log;C:\\pap-log\\error.log"; + String[] retrunObject = ParseLog.getPaths(logPaths); + assertEquals(3, retrunObject.length); + + // valid test + logPaths = "C:\\pap-log\\pap-rest.log"; + retrunObject = ParseLog.getPaths(logPaths); + assertEquals(1, retrunObject.length); + + // invalid test + logPaths = ""; + retrunObject = ParseLog.getPaths(logPaths); + assertTrue(retrunObject == null); + + } catch (final Exception e) { + fail(); + } + + logger.debug("testGetPaths: exit"); + } + + @Test + public void testPullOutLogValuesSecond() { + + logger.debug("testPullOutLogValuesSecond: enter"); + // ERROR_VALUE + // Open the file + FileInputStream fstream; + try { + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = br.readLine(); + strLine = br.readLine(); + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "INFO"); + assertEquals(LogType.INFO, retrunObject.getLogType()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValuesSecond: exit"); + } + + @Test + public void testPullOutLogValuesThird() { + + logger.debug("testPullOutLogValuesThird: enter"); + // ERROR_VALUE + // Open the file + FileInputStream fstream; + try { + final int number = 3; + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = br.readLine(); + for (int i = 0; i < number; i++) { + strLine = br.readLine(); + } + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP"); + assertEquals(LogType.INFO, retrunObject.getLogType()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValuesThird: exit"); + } + + @Test + public void testPullOutLogValuesFourth() { + + logger.debug("testPullOutLogValuesFourth: enter"); + // Open the file + FileInputStream fstream; + try { + final int number = 4; + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = br.readLine(); + for (int i = 0; i < number; i++) { + strLine = br.readLine(); + } + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PAP"); + assertEquals(LogType.INFO, retrunObject.getLogType()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValuesFourth: exit"); + } + + @Test + public void testPullOutLogValuesFith() { + + logger.debug("testPullOutLogValuesFith: enter"); + // Open the file + FileInputStream fstream; + try { + final int number = 5; + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = br.readLine(); + for (int i = 0; i < number; i++) { + strLine = br.readLine(); + } + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP"); + assertEquals(LogType.WARN, retrunObject.getLogType()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValuesFith: exit"); + } + + @Test + public void testPullOutLogValuesSixth() { + + logger.debug("testPullOutLogValuesSixth: enter"); + // Open the file + FileInputStream fstream; + try { + final int number = 6; + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = br.readLine(); + for (int i = 0; i < number; i++) { + strLine = br.readLine(); + } + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "PyPDP"); + assertEquals(LogType.SEVERE, retrunObject.getLogType()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValuesSixth: exit"); + } + + @Test + public void testPullOutLogValuesSeven() { + + logger.debug("testPullOutLogValuesSeven: enter"); + // Open the file + FileInputStream fstream; + try { + final int number = 7; + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = br.readLine(); + for (int i = 0; i < number; i++) { + strLine = br.readLine(); + } + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "Console"); + assertEquals(LogType.ERROR, retrunObject.getLogType()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValuesSeven: exit"); + } + + @Test + public void testPullOutLogValuesEight() { + + logger.debug("testPullOutLogValuesEight: enter"); + // Open the file + FileInputStream fstream; + try { + final int number = 8; + fstream = new FileInputStream(testFile1); + final BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); + String strLine = br.readLine(); + for (int i = 0; i < number; i++) { + strLine = br.readLine(); + } + final LogEntryObject retrunObject = ParseLog.pullOutLogValues(strLine, "pap"); + assertEquals(LogType.WARN, retrunObject.getLogType()); + br.close(); + } catch (final IOException e) { + fail(); + } + + logger.debug("testPullOutLogValuesEight: exit"); + } + + @Test + public void testPullOutLogValuesNull() { + + logger.debug("testPullOutLogValuesNull: enter"); + // Open the file + final LogEntryObject retrunObject = ParseLog.pullOutLogValues("", "Console"); + assertEquals(null, retrunObject); + + logger.debug("testPullOutLogValuesNull: exit"); + } + + @Test + public void testLogEntryObject() { + + logger.debug("testLogEntryObject: enter"); + + final Date date = new Date(); + + // Open the file + final LogEntryObject logObject = new LogEntryObject(); + logObject.setSystem("vm02"); + logObject.setSystemType("pap"); + logObject.setDate(date); + logObject.setRemote("remote"); + + assertEquals("vm02", logObject.getSystem()); + assertEquals("pap", logObject.getSystemType()); + assertEquals(date, logObject.getDate()); + assertEquals("remote", logObject.getRemote()); + + logger.debug("testLogEntryObject: exit"); + } + + @Test + public void testProcess() { + + logger.debug("testProcess: enter"); + + final String line = "2015-04-01 09:13:44.947" + " DEBUG 17482 --- [nio-8480-exec-7] " + + "c.a.l.onap.policy.std.StdPolicyConfig" + " : config Retrieved "; + + im = Mockito.mock(IntegrityMonitor.class); + try { + Mockito.doNothing().when(im).startTransaction(); + } catch (final IntegrityMonitorException e) { + fail(); + } + Mockito.doNothing().when(im).endTransaction(); + ParseLog.process(line, "pap", LogType.INFO); + + logger.debug("testProcess: exit"); + } + + @Test + public void testMain() { + try { + ParseLog.main(new String[] {}); + } catch (final Exception e) { + logger.debug("exception occured while executing the test: exit"); + } + } + + @Test public void testMainDebug() { try { final Properties returnConfig = ParseLog.getPropertiesValue(configFileDebug); @@ -599,4 +609,4 @@ public class ParseLogTest { logger.debug("exception occured while executing the test: exit"); } } -}
\ No newline at end of file +} |