2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netvirt.qosservice;
11 import java.io.IOException;
12 import java.math.BigInteger;
13 import java.util.Dictionary;
14 import java.util.Enumeration;
15 import java.util.HashMap;
16 import java.util.Hashtable;
18 import org.osgi.service.cm.Configuration;
19 import org.osgi.service.cm.ConfigurationAdmin;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class QosAlertGenerator {
24 private static final Logger LOG = LoggerFactory.getLogger(QosAlertGenerator.class);
26 private final ConfigurationAdmin configurationAdmin;
28 public QosAlertGenerator(ConfigurationAdmin configurationAdmin) {
29 this.configurationAdmin = configurationAdmin;
31 updateQoSAlertLog4jProperties(getPropertyMap(QosConstants.QOS_ALERT_PROPERTIES_PID));
32 } catch (IOException e) {
33 LOG.error("Error updating log4j properties", e);
37 public void update(Map<String, Object> qosAlertProperties) {
39 updateQoSAlertLog4jProperties(qosAlertProperties);
40 } catch (IOException e) {
41 LOG.error("Error updating log4j properties", e);
45 public static void raiseAlert(final String qosPolicyName, final String qosPolicyUuid, final String portUuid,
46 final String networkUuid, final BigInteger rxPackets,
47 final BigInteger rxDroppedPackets) {
48 // Log the alert message in a text file using log4j appender qosalertmsg
49 LOG.debug(QosConstants.ALERT_MSG_FORMAT, qosPolicyName, qosPolicyUuid, portUuid, networkUuid,
50 rxPackets, rxDroppedPackets);
53 private Configuration getConfig(String pid) throws IOException {
54 return configurationAdmin.getConfiguration(pid);
57 private Map<String, Object> getPropertyMap(String pid) throws IOException {
58 Map<String, Object> propertyMap = null;
59 Configuration configurationInit = getConfig(pid);
60 Dictionary<String, Object> config = configurationInit.getProperties();
61 propertyMap = new HashMap<>(config.size());
62 Enumeration<String> keys = config.keys();
63 while (keys.hasMoreElements()) {
64 String key = keys.nextElement();
65 propertyMap.put(key, config.get(key));
70 private static Map<String, Object> removeNonLog4jProperties(Map<String, Object> qosAlertLog4jProperties) {
71 qosAlertLog4jProperties.remove(QosConstants.FELIX_FILEINSTALL_FILENAME);
72 qosAlertLog4jProperties.remove(QosConstants.SERVICE_PID);
73 return qosAlertLog4jProperties;
76 private void updateQoSAlertLog4jProperties(Map<String, Object> qosAlertLog4jProperties) throws IOException {
77 Map<String, Object> log4jProperties = getPropertyMap(QosConstants.ORG_OPS4J_PAX_LOGGING);
78 Hashtable<String, Object> updateLog4jProperties = new Hashtable<>();
79 updateLog4jProperties.putAll(log4jProperties);
80 updateLog4jProperties.putAll(removeNonLog4jProperties(qosAlertLog4jProperties));
82 Configuration log4jConfig = getConfig(QosConstants.ORG_OPS4J_PAX_LOGGING);
84 log4jConfig.update(updateLog4jProperties);
86 } catch (IOException ioe) {
87 LOG.error("Exception in configuration {}", ioe);