blob: 7bc1b7723a63b2567f23c71be320580a1388c09b [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
kmcpeakeb172d5f2015-12-10 11:30:43 +00003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.faultmanagement.impl;
17
Andrea Campanellae72ac552016-04-11 10:04:52 -070018import com.google.common.collect.ImmutableSet;
19import org.junit.Before;
20import org.junit.Rule;
kmcpeakeb172d5f2015-12-10 11:30:43 +000021import org.junit.Test;
Andrea Campanellae72ac552016-04-11 10:04:52 -070022import org.junit.rules.ExpectedException;
kmcpeakeb172d5f2015-12-10 11:30:43 +000023import org.onlab.util.ItemNotFoundException;
kmcpeakeb172d5f2015-12-10 11:30:43 +000024import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
kmcpeakeb172d5f2015-12-10 11:30:43 +000025import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
26import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
27import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
Andrea Campanellae72ac552016-04-11 10:04:52 -070028import org.onosproject.net.DeviceId;
kmcpeakeb172d5f2015-12-10 11:30:43 +000029
Andrea Campanellae72ac552016-04-11 10:04:52 -070030import java.util.Collections;
31import java.util.HashMap;
32import java.util.Map;
33
34import static org.junit.Assert.assertEquals;
35import static org.junit.Assert.assertTrue;
36import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel.CLEARED;
37import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel.CRITICAL;
38
39/**
40 * Alarm manager test suite.
41 */
kmcpeakeb172d5f2015-12-10 11:30:43 +000042public class AlarmsManagerTest {
43
kmcpeakeb172d5f2015-12-10 11:30:43 +000044 private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo:bar");
45 private static final DefaultAlarm ALARM_A = new DefaultAlarm.Builder(
46 DEVICE_ID, "aaa", Alarm.SeverityLevel.CRITICAL, 0).build();
47
48 private static final DefaultAlarm ALARM_A_WITHSRC = new DefaultAlarm.Builder(
49 ALARM_A).forSource(AlarmEntityId.alarmEntityId("port:foo")).build();
50
51 private static final DefaultAlarm ALARM_B = new DefaultAlarm.Builder(
52 DEVICE_ID, "bbb", Alarm.SeverityLevel.CRITICAL, 0).build();
53
Andrea Campanellae72ac552016-04-11 10:04:52 -070054 private AlarmsManager am;
55
56 @Rule
57 public final ExpectedException exception = ExpectedException.none();
58
59 @Before
60 public void setUp() throws Exception {
61 am = new AlarmsManager();
62 }
63
64 @Test
65 public void deactivate() throws Exception {
66 am.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_B, ALARM_A));
67 verifyGettingSetsOfAlarms(am, 2, 2);
68 am.deactivate(null);
69 assertEquals("Alarms should be purged", 0, am.alarms.size());
70 }
71
72 @Test
73 public void testGettersWhenNoAlarms() {
74
75 assertTrue("No alarms should be present", am.getAlarms().isEmpty());
76 assertTrue("No active alarms should be present", am.getActiveAlarms().isEmpty());
77 assertTrue("The map should be empty per unknown device",
78 am.getAlarmCounts(DeviceId.NONE).keySet().isEmpty());
79 assertTrue("The counts should be empty", am.getAlarmCounts().keySet().isEmpty());
80
81 assertEquals("Incorrect number of alarms for unknown device",
82 0, am.getAlarms(DeviceId.NONE).size());
83 assertEquals("Incorrect number of major alarms for unknown device",
84 0, am.getAlarms(Alarm.SeverityLevel.MAJOR).size());
85
86 exception.expect(NullPointerException.class);
87 am.getAlarm(null);
88
89 exception.expect(ItemNotFoundException.class);
90 am.getAlarm(AlarmId.alarmId(1));
91 }
92
93 @Test
94 public void testAlarmUpdates() {
95
96 assertTrue("No alarms should be present", am.getAlarms().isEmpty());
97 am.updateAlarms(DEVICE_ID, ImmutableSet.of());
98 assertTrue("No alarms should be present", am.getAlarms().isEmpty());
99 Map<Alarm.SeverityLevel, Long> zeroAlarms = new CountsMapBuilder().create();
100 assertEquals("No alarms count should be present", zeroAlarms, am.getAlarmCounts());
101 assertEquals("No alarms count should be present", zeroAlarms, am.getAlarmCounts(DEVICE_ID));
102
103 am.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_B, ALARM_A));
104 verifyGettingSetsOfAlarms(am, 2, 2);
105 Map<Alarm.SeverityLevel, Long> critical2 = new CountsMapBuilder().with(CRITICAL, 2L).create();
106 assertEquals("A critical should be present", critical2, am.getAlarmCounts());
107 assertEquals("A critical should be present", critical2, am.getAlarmCounts(DEVICE_ID));
108
109 am.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_A));
110 verifyGettingSetsOfAlarms(am, 2, 1);
111 Map<Alarm.SeverityLevel, Long> critical1cleared1 =
112 new CountsMapBuilder().with(CRITICAL, 1L).with(CLEARED, 1L).create();
113 assertEquals("A critical should be present and cleared", critical1cleared1,
114 am.getAlarmCounts());
115 assertEquals("A critical should be present and cleared", critical1cleared1,
116 am.getAlarmCounts(DEVICE_ID));
117
118 // No change map when same alarms sent
119 am.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_A));
120 verifyGettingSetsOfAlarms(am, 2, 1);
121 assertEquals("Map should not be changed for same alarm", critical1cleared1,
122 am.getAlarmCounts());
123 assertEquals("Map should not be changed for same alarm", critical1cleared1,
124 am.getAlarmCounts(DEVICE_ID));
125
126 am.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_A, ALARM_A_WITHSRC));
127 verifyGettingSetsOfAlarms(am, 3, 2);
128 Map<Alarm.SeverityLevel, Long> critical2cleared1 =
129 new CountsMapBuilder().with(CRITICAL, 2L).with(CLEARED, 1L).create();
130 assertEquals("A critical should be present", critical2cleared1, am.getAlarmCounts());
131 assertEquals("A critical should be present", critical2cleared1, am.getAlarmCounts(DEVICE_ID));
132
133 am.updateAlarms(DEVICE_ID, ImmutableSet.of());
134 verifyGettingSetsOfAlarms(am, 3, 0);
135 assertEquals(new CountsMapBuilder().with(CLEARED, 3L).create(), am.getAlarmCounts(DEVICE_ID));
136
137 assertEquals("The counts should be empty for unknown devices", zeroAlarms,
138 am.getAlarmCounts(DeviceId.NONE));
139 assertEquals("The counts should be empty for unknown devices", zeroAlarms,
140 am.getAlarmCounts(DeviceId.deviceId("junk:junk")));
141
142 }
143
144 private void verifyGettingSetsOfAlarms(AlarmsManager am, int expectedTotal, int expectedActive) {
145 assertEquals("Incorrect total alarms", expectedTotal, am.getAlarms().size());
146 assertEquals("Incorrect active alarms count", expectedActive, am.getActiveAlarms().size());
147 }
148
kmcpeakeb172d5f2015-12-10 11:30:43 +0000149 private static class CountsMapBuilder {
150
151 private final Map<Alarm.SeverityLevel, Long> map = new HashMap<>();
152
153 public CountsMapBuilder with(Alarm.SeverityLevel sev, Long count) {
154 map.put(sev, count);
155 return this;
156 }
157
158 public Map<Alarm.SeverityLevel, Long> create() {
159 return Collections.unmodifiableMap(map);
160 }
161 }
162
163}