move vpnservice and cleanup poms
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosAlertGenerator.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.qosservice;
10
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;
17 import java.util.Map;
18 import org.osgi.service.cm.Configuration;
19 import org.osgi.service.cm.ConfigurationAdmin;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class QosAlertGenerator {
24     private static final Logger LOG = LoggerFactory.getLogger(QosAlertGenerator.class);
25
26     private final ConfigurationAdmin configurationAdmin;
27
28     public QosAlertGenerator(ConfigurationAdmin configurationAdmin) {
29         this.configurationAdmin = configurationAdmin;
30         try {
31             updateQoSAlertLog4jProperties(getPropertyMap(QosConstants.QOS_ALERT_PROPERTIES_PID));
32         } catch (IOException e) {
33             LOG.error("Error updating log4j properties", e);
34         }
35     }
36
37     public void update(Map<String, Object> qosAlertProperties) {
38         try {
39             updateQoSAlertLog4jProperties(qosAlertProperties);
40         } catch (IOException e) {
41             LOG.error("Error updating log4j properties", e);
42         }
43     }
44
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);
51     }
52
53     private Configuration getConfig(String pid) throws IOException {
54         return configurationAdmin.getConfiguration(pid);
55     }
56
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));
66         }
67         return propertyMap;
68     }
69
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;
74     }
75
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));
81
82         Configuration log4jConfig = getConfig(QosConstants.ORG_OPS4J_PAX_LOGGING);
83         try {
84             log4jConfig.update(updateLog4jProperties);
85             log4jConfig.update();
86         } catch (IOException ioe) {
87             LOG.error("Exception in configuration {}", ioe);
88         }
89     }
90 }