From fbe2ea46cf3d8902bf2425f12e679523455beaa3 Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Fri, 16 Nov 2018 12:40:25 -0500 Subject: Remove all usage of AlarmLogger Fix broken UT from removing alarm logger fixed compilation and merge issues Remove all usage of AlarmLogger Change-Id: Ic87abd51423274570bfca0ed976d9642a91a843d Issue-ID: SO-1229 Signed-off-by: Benjamin, Max (mb388a) --- .../java/org/onap/so/logger/MsoAlarmLogger.java | 187 --------------------- .../so/web/exceptions/RuntimeExceptionMapper.java | 6 +- .../so/adapter_utils/tests/MsoAlarmLoggerTest.java | 134 --------------- .../web/exceptions/RuntimeExceptionMapperTest.java | 9 +- 4 files changed, 5 insertions(+), 331 deletions(-) delete mode 100644 common/src/main/java/org/onap/so/logger/MsoAlarmLogger.java delete mode 100644 common/src/test/java/org/onap/so/adapter_utils/tests/MsoAlarmLoggerTest.java (limited to 'common') diff --git a/common/src/main/java/org/onap/so/logger/MsoAlarmLogger.java b/common/src/main/java/org/onap/so/logger/MsoAlarmLogger.java deleted file mode 100644 index 890aac93c0..0000000000 --- a/common/src/main/java/org/onap/so/logger/MsoAlarmLogger.java +++ /dev/null @@ -1,187 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * 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.so.logger; - - -import java.io.File; - -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; - -import org.slf4j.LoggerFactory; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.encoder.PatternLayoutEncoder; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.rolling.RollingFileAppender; -import ch.qos.logback.core.rolling.TimeBasedRollingPolicy; - -/** - * Wrapper around log4j and Nagios NRDP passive alarming for MSO. - * - * For local alarm logging, this class will look for an alarm log file name - * in the servlet context parameter "mso.alarms.file". If none is found, - * it will look for an MsoProperty of the same name. As a last resort, - * it will use the default path "/var/log/ecomp/MSO/alarms/alarm.log". - * It is expected that all alarms within an application will use the same - * alarm file, so there is no way to dynamically add other alarm files. - * - * Alarms are logged as a simple pipe-delimited string of the format: - * ||| - * - * This class also supports real-time Nagios NRDP alarming. If enabled via - * MsoProperties, all alarms generated and logged to the local alarm file will - * also be transmitted to a Nagios NRDP instance. NRDP requires 4 parameters - * in service alarm events (all Mso Alarms will be Service Alarms): - * hostname, servicename, state, detail - * - * The log file format is also intended to be compatible with Nagios NRDP for - * non-real-time reporting. The command-line tool for sending alarms is - * is "send_nrdp.php", which takes the same 4 parameters as input. - * It will be easy enough to translate entries from an alarm.log file to - * NRDP if real-time NRDP alarming is not enabled. - * - * For Nagios integration, the alarmTypes should all match "service names" - * configured in the receiving Nagios server. Also, the alarm state will - * be limited to the 4 values defined by Nagios: - * 0 = OK, 1 = Warning, 2 = Critical, 3 = Unknown - * - * - */ -public class MsoAlarmLogger implements ServletContextListener { - - private Logger alarmLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(MSO_ALARM_CONTEXT); - private static RollingFileAppender fileAppender = null; - public static final String DEFAULT_MSO_ALARM_FILE = "/var/log/ecomp/MSO/alarms/alarm.log"; - public static final String MSO_ALARM_CONTEXT = "mso.alarms"; - - public static final int OK = 0; - public static final int WARNING = 1; - public static final int CRITICAL = 2; - public static final int UNKNOWN = 3; - - /** - * Get the default MSO Alarm Logger - */ - public MsoAlarmLogger () { - - initializeAlarmLogger(null); - - } - - public MsoAlarmLogger (String alarmFile) { - initializeAlarmLogger(alarmFile); - - } - - /** - * Method to record an alarm. - * - * @param alarm - the alarm identifier (Nagios "service") - * @param state - the alarm state/severity, based on Nagios service - * state values: 0 = OK, 1 = Warning, 2 = Critical, 3 = Unknown - * @param detail - detail message (may contain additional internal - * structure per alarm type) - */ - public void sendAlarm (String alarm, int state, String detail) { - // Write the alarm to Log file - if (alarmLogger != null) { - String output = alarm + "|" + state + "|" + detail; - alarmLogger.info (output); - } - - } - - @Override - public void contextDestroyed (ServletContextEvent event) { - // Nothing to do... - } - - @Override - public void contextInitialized (ServletContextEvent event) { - String msoAlarmFile = event.getServletContext ().getInitParameter ("mso.alarm.file"); - if (msoAlarmFile == null) { - msoAlarmFile = DEFAULT_MSO_ALARM_FILE; - } - - initializeAlarmLogger (msoAlarmFile); - } - - private void initializeAlarmLogger (String alarmFile) { - synchronized (MsoAlarmLogger.class) { - if (fileAppender == null) { - if (alarmFile != null) { - fileAppender = MsoAlarmLogger.getAppender (alarmFile); - } else { - fileAppender = MsoAlarmLogger.getAppender (DEFAULT_MSO_ALARM_FILE); - } - } - } - // The alarmLogger was static originally. - // The initialization of the alarmLogger was fine, but not sure why, it lost its appender info later - // Due to that issue, the alarmLogger is not static any more. - // Instead static attribute fileAppender is added and will be assigned to the alarmLogger every time new MsoAlarmLogger is created. - alarmLogger.setLevel (Level.INFO); - alarmLogger.addAppender (fileAppender); - alarmLogger.setAdditive (false); - } - - public void resetAppender() { - synchronized (MsoAlarmLogger.class) { - fileAppender = null; - } - } - - private static RollingFileAppender getAppender (String msoAlarmFile) { - // Create a Logger for alarms. Just use a default Pattern that outputs - // a message. MsoAlarmLogger will handle the formatting. - File alarmFile = new File (msoAlarmFile); - File alarmDir = alarmFile.getParentFile (); - if (!alarmDir.exists ()) { - alarmDir.mkdirs (); - } - - String logPattern = "%d{yyyy-MM-dd HH:mm:ss}|%m%n"; - - LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); - PatternLayoutEncoder encoder=new PatternLayoutEncoder(); - encoder.setPattern(logPattern); - encoder.setContext(context); - encoder.start(); - RollingFileAppender fileAppender= new RollingFileAppender<>(); - TimeBasedRollingPolicy rollingPolicy= new TimeBasedRollingPolicy<>(); - rollingPolicy.setContext(context); - rollingPolicy.setFileNamePattern(msoAlarmFile + ".%d"); - rollingPolicy.setParent(fileAppender); - rollingPolicy.start(); - fileAppender.setFile(msoAlarmFile); - fileAppender.setAppend(true); - fileAppender.setEncoder(encoder); - fileAppender.setRollingPolicy(rollingPolicy); - fileAppender.setContext(context); - fileAppender.start(); - - return fileAppender; - } - -} diff --git a/common/src/main/java/org/onap/so/web/exceptions/RuntimeExceptionMapper.java b/common/src/main/java/org/onap/so/web/exceptions/RuntimeExceptionMapper.java index 72e609acbd..9ddfd0592c 100644 --- a/common/src/main/java/org/onap/so/web/exceptions/RuntimeExceptionMapper.java +++ b/common/src/main/java/org/onap/so/web/exceptions/RuntimeExceptionMapper.java @@ -25,20 +25,20 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; -import org.onap.so.logger.MsoAlarmLogger; + import org.onap.so.logger.MsoLogger; public class RuntimeExceptionMapper implements ExceptionMapper { private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.GENERAL, RuntimeExceptionMapper.class); - private static MsoAlarmLogger alarmLogger = new MsoAlarmLogger(); + @Override public Response toResponse(RuntimeException exception) { if (exception instanceof NotFoundException) { return Response.status(Status.NOT_FOUND).build(); } else { - alarmLogger.sendAlarm("MsoApplicationError", MsoAlarmLogger.CRITICAL, exception.getMessage()); + logger.error(exception); return Response.status(Status.INTERNAL_SERVER_ERROR).entity(new ExceptionResponse("Unexpected Internal Exception")).build(); } diff --git a/common/src/test/java/org/onap/so/adapter_utils/tests/MsoAlarmLoggerTest.java b/common/src/test/java/org/onap/so/adapter_utils/tests/MsoAlarmLoggerTest.java deleted file mode 100644 index 6756bc98ad..0000000000 --- a/common/src/test/java/org/onap/so/adapter_utils/tests/MsoAlarmLoggerTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * 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.so.adapter_utils.tests; - - -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.PrintWriter; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.onap.so.logger.MsoAlarmLogger; - - -/** - * This junit test very roughly the alarm logger - * - */ -public class MsoAlarmLoggerTest { - - public static MsoAlarmLogger msoAlarmLogger; - - @BeforeClass - public static final void createObjects() throws IOException - { - - File outputFile = new File ("./target/alarm-test.log"); - if (outputFile.exists()) { - outputFile.delete(); - } else { - outputFile.createNewFile(); - } - msoAlarmLogger = new MsoAlarmLogger("./target/alarm-test.log"); - } - - @AfterClass - public static void tearDown() { - msoAlarmLogger.resetAppender(); - } - @Test - public void testAlarmConfig() throws IOException { - - msoAlarmLogger.sendAlarm("test", 0, "detail message"); - - FileInputStream inputStream = new FileInputStream("./target/alarm-test.log"); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - - String line = reader.readLine(); - String[] splitLine = line.split("\\|"); - assertTrue(splitLine.length==4); - assertTrue("test".equals(splitLine[1])); - assertTrue("0".equals(splitLine[2])); - assertTrue("detail message".equals(splitLine[3])); - - line = reader.readLine(); - assertNull(line); - reader.close(); - inputStream.close(); - - // Reset the file for others tests - PrintWriter writer = new PrintWriter(new File("./target/alarm-test.log")); - writer.print(""); - writer.close(); - - } - - @Test - public void testAlarm() throws IOException { - - msoAlarmLogger.sendAlarm("test", 0, "detail message"); - msoAlarmLogger.sendAlarm("test2", 1, "detail message2"); - msoAlarmLogger.sendAlarm("test3", 2, "detail message3"); - - FileInputStream inputStream = new FileInputStream("./target/alarm-test.log"); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - - String line = reader.readLine(); - String[] splitLine = line.split("\\|"); - assertTrue(splitLine.length==4); - assertTrue("test".equals(splitLine[1])); - assertTrue("0".equals(splitLine[2])); - assertTrue("detail message".equals(splitLine[3])); - - line = reader.readLine(); - splitLine = line.split("\\|"); - assertTrue(splitLine.length==4); - assertTrue("test2".equals(splitLine[1])); - assertTrue("1".equals(splitLine[2])); - assertTrue("detail message2".equals(splitLine[3])); - - line = reader.readLine(); - splitLine = line.split("\\|"); - assertTrue(splitLine.length==4); - assertTrue("test3".equals(splitLine[1])); - assertTrue("2".equals(splitLine[2])); - assertTrue("detail message3".equals(splitLine[3])); - - line = reader.readLine(); - assertNull(line); - reader.close(); - inputStream.close(); - - // Reset the file for others tests - PrintWriter writer = new PrintWriter(new File("./target/alarm-test.log")); - writer.print(""); - writer.close(); - - } -} diff --git a/common/src/test/java/org/onap/so/web/exceptions/RuntimeExceptionMapperTest.java b/common/src/test/java/org/onap/so/web/exceptions/RuntimeExceptionMapperTest.java index 8bcc73515b..b49c5312e5 100644 --- a/common/src/test/java/org/onap/so/web/exceptions/RuntimeExceptionMapperTest.java +++ b/common/src/test/java/org/onap/so/web/exceptions/RuntimeExceptionMapperTest.java @@ -33,16 +33,11 @@ import javax.ws.rs.core.Response.Status; import org.junit.AfterClass; import org.junit.Test; -import org.onap.so.logger.MsoAlarmLogger; + public class RuntimeExceptionMapperTest { - - @AfterClass - public static void tearDown() { - MsoAlarmLogger logger = new MsoAlarmLogger(); - logger.resetAppender(); - } + @Test public void testResponse() { -- cgit 1.2.3-korg