2 * Copyright (c) 2017 6WIND, Inc. 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.alarm;
11 import com.google.common.collect.ImmutableMap;
13 import java.util.ArrayList;
14 import java.util.List;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18 import java.util.concurrent.atomic.AtomicInteger;
20 import javax.management.AttributeChangeNotification;
21 import javax.management.Notification;
22 import javax.management.NotificationBroadcasterSupport;
23 import javax.management.NotificationListener;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class NvpnNbrControlPathAlarm extends NotificationBroadcasterSupport implements NvpnNbrControlPathAlarmMBean,
29 javax.management.NotificationListener {
31 private static final Logger LOG = LoggerFactory.getLogger(NvpnNbrControlPathAlarm.class);
33 private AtomicInteger sequenceNumber = new AtomicInteger(0);
35 private ConcurrentMap<String, List<String>> raiseAlarmObjectMap = new ConcurrentHashMap<String, List<String>>();
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");
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));
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");
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));
71 public int getRaiseAlarmObjectMapSize() {
72 return raiseAlarmObjectMap.size();
75 public Map<String, List<String>> getRaiseAlarmObjectMapCopy() {
76 return ImmutableMap.copyOf(raiseAlarmObjectMap);
80 public void handleNotification(NotificationListener listener, Notification notif, Object handback) {
81 AttributeChangeNotification attrib = null;
83 attrib = (AttributeChangeNotification) notif;
84 } catch (ClassCastException e) {
85 /*type of notification is not expected*/
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);
99 if (notif.getMessage().compareTo(NvpnJMXAlarmAgent.OP_CLEARALARM) == 0) {
100 this.raiseAlarmObjectMap.remove(tab.get(2), tab);
107 public void handleNotification(Notification notification, Object handback) {
108 handleNotification(null, notification, handback);