kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.faultmanagement.impl; |
| 17 | |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Rule; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 21 | import org.junit.Test; |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame^] | 22 | import org.junit.rules.ExpectedException; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 23 | import org.onlab.util.ItemNotFoundException; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 24 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 25 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 26 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 27 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame^] | 28 | import org.onosproject.net.DeviceId; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 29 | |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame^] | 30 | import java.util.Collections; |
| 31 | import java.util.HashMap; |
| 32 | import java.util.Map; |
| 33 | |
| 34 | import static org.junit.Assert.assertEquals; |
| 35 | import static org.junit.Assert.assertTrue; |
| 36 | import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel.CLEARED; |
| 37 | import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel.CRITICAL; |
| 38 | |
| 39 | /** |
| 40 | * Alarm manager test suite. |
| 41 | */ |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 42 | public class AlarmsManagerTest { |
| 43 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 44 | 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 Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame^] | 54 | 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 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 149 | 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 | } |