Refactor alarm poms to match latest conventions
[netvirt.git] / vpnservice / alarm / impl / src / main / java / org / opendaylight / netvirt / alarm / NvpnNbrControlPathAlarm.java
1 /*
2  * Copyright (c) 2017 6WIND, Inc. 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.alarm;
10
11 import com.google.common.collect.ImmutableMap;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.atomic.AtomicInteger;
19
20 import javax.management.AttributeChangeNotification;
21 import javax.management.Notification;
22 import javax.management.NotificationBroadcasterSupport;
23 import javax.management.NotificationListener;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NvpnNbrControlPathAlarm extends NotificationBroadcasterSupport implements NvpnNbrControlPathAlarmMBean,
29     javax.management.NotificationListener {
30
31     private static final Logger LOG = LoggerFactory.getLogger(NvpnNbrControlPathAlarm.class);
32
33     private AtomicInteger sequenceNumber = new AtomicInteger(0);
34
35     private ConcurrentMap<String, List<String>> raiseAlarmObjectMap = new ConcurrentHashMap<String, List<String>>();
36
37     @Override
38     public void raiseAlarm(String alarmName, String additionalText, String source, String detailsInfo) {
39         if (alarmName == null || source == null || detailsInfo == null) {
40             LOG.error("NvpnNbrControlPathAlarm.raiseAlarm has bad argument");
41             return;
42         }
43         ArrayList<String> raiseAlarmObject = new ArrayList<>();
44         raiseAlarmObject.add(alarmName);
45         raiseAlarmObject.add(additionalText);
46         raiseAlarmObject.add(source);
47         raiseAlarmObject.add(detailsInfo);
48         sendNotification(new AttributeChangeNotification(this,
49             sequenceNumber.getAndIncrement(), System.currentTimeMillis(),
50             NvpnJMXAlarmAgent.OP_RAISEALARM, "raiseAlarmObject", "List",
51             "", raiseAlarmObject));
52     }
53
54     @Override
55     public void clearAlarm(String alarmName, String additionalText, String source, String detailsInfo) {
56         if (alarmName == null || source == null || detailsInfo == null) {
57             LOG.error("NvpnNbrControlPathAlarm.clearAlarm has bad argument");
58             return;
59         }
60         ArrayList<String> clearAlarmObject = new ArrayList<>();
61         clearAlarmObject.add(alarmName);
62         clearAlarmObject.add(additionalText);
63         clearAlarmObject.add(source);
64         clearAlarmObject.add(detailsInfo);
65         sendNotification(new AttributeChangeNotification(this,
66                 sequenceNumber.getAndIncrement(), System.currentTimeMillis(),
67                 NvpnJMXAlarmAgent.OP_CLEARALARM, "clearAlarmObject", "List",
68                 "", clearAlarmObject));
69     }
70
71     public int getRaiseAlarmObjectMapSize() {
72         return raiseAlarmObjectMap.size();
73     }
74
75     public Map<String, List<String>> getRaiseAlarmObjectMapCopy() {
76         return ImmutableMap.copyOf(raiseAlarmObjectMap);
77     }
78
79     @Override
80     public void handleNotification(NotificationListener listener, Notification notif, Object handback) {
81         AttributeChangeNotification attrib = null;
82         try {
83             attrib = (AttributeChangeNotification) notif;
84         } catch (ClassCastException e) {
85             /*type of notification is not expected*/
86             return;
87         }
88         /*below you could treat this new alarm and change the behavior if needed*/
89         if (notif != null && notif.getSource() != null
90                 && notif.getSource().getClass().equals(NvpnNbrControlPathAlarm.class)) {
91             List<String> tab = (List<String>) attrib.getNewValue();
92             if (tab != null && tab.size() == 4) {
93                 if (notif.getMessage().compareTo(NvpnJMXAlarmAgent.OP_RAISEALARM) == 0) {
94                     //only the last list is stored in the map (for a unique key),
95                     //ensure that you have get the old list if needed
96                     //and store it in the new list if you are expecting to save it again.
97                     this.raiseAlarmObjectMap.put(tab.get(2), tab);
98                 }
99                 if (notif.getMessage().compareTo(NvpnJMXAlarmAgent.OP_CLEARALARM) == 0) {
100                     this.raiseAlarmObjectMap.remove(tab.get(2), tab);
101                 }
102             }
103         }
104     }
105
106     @Override
107     public void handleNotification(Notification notification, Object handback) {
108         handleNotification(null, notification, handback);
109     }
110 }