Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andrea Campanella | 8e94b0c | 2016-04-12 13:58:07 -0700 | [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 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Rule; |
| 22 | import org.junit.Test; |
| 23 | import org.junit.rules.ExpectedException; |
| 24 | import org.onlab.junit.TestTools; |
| 25 | import org.onlab.junit.TestUtils; |
| 26 | import org.onlab.util.ItemNotFoundException; |
| 27 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 28 | import org.onosproject.event.Event; |
| 29 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 30 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 31 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEvent; |
| 32 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 33 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmListener; |
| 34 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.NetTestTools; |
| 37 | import org.onosproject.store.service.TestStorageService; |
| 38 | |
| 39 | import java.util.Collections; |
| 40 | import java.util.HashMap; |
| 41 | import java.util.List; |
| 42 | import java.util.Map; |
| 43 | |
| 44 | import static junit.framework.TestCase.assertFalse; |
| 45 | import static org.junit.Assert.assertEquals; |
| 46 | import static org.junit.Assert.assertTrue; |
| 47 | import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel.CLEARED; |
| 48 | import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel.CRITICAL; |
| 49 | |
| 50 | /** |
| 51 | * Alarm manager test suite. |
| 52 | */ |
| 53 | public class AlarmManagerTest { |
| 54 | |
| 55 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo:bar"); |
| 56 | private static final DefaultAlarm ALARM_A = new DefaultAlarm.Builder( |
| 57 | DEVICE_ID, "aaa", Alarm.SeverityLevel.CRITICAL, 0).build(); |
| 58 | |
| 59 | private static final DefaultAlarm ALARM_A_WITHSRC = new DefaultAlarm.Builder( |
| 60 | ALARM_A).forSource(AlarmEntityId.alarmEntityId("port:foo")).build(); |
| 61 | |
| 62 | private static final DefaultAlarm ALARM_B = new DefaultAlarm.Builder( |
| 63 | DEVICE_ID, "bbb", Alarm.SeverityLevel.CRITICAL, 0).build(); |
| 64 | |
| 65 | private AlarmManager manager; |
| 66 | private DistributedAlarmStore alarmStore; |
| 67 | protected TestListener listener = new TestListener(); |
| 68 | |
| 69 | @Rule |
| 70 | public final ExpectedException exception = ExpectedException.none(); |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | alarmStore = new DistributedAlarmStore(); |
| 75 | TestUtils.setField(alarmStore, "storageService", new TestStorageService()); |
| 76 | alarmStore.activate(); |
| 77 | manager = new AlarmManager(); |
| 78 | manager.addListener(listener); |
| 79 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
| 80 | manager.store = alarmStore; |
| 81 | manager.activate(); |
| 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void deactivate() throws Exception { |
| 86 | manager.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_B, ALARM_A)); |
| 87 | verifyGettingSetsOfAlarms(manager, 2, 2); |
| 88 | alarmStore.deactivate(); |
| 89 | manager.removeListener(listener); |
| 90 | manager.deactivate(); |
| 91 | NetTestTools.injectEventDispatcher(manager, null); |
| 92 | assertFalse("Store should not have delegate", alarmStore.hasDelegate()); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testGettersWhenNoAlarms() { |
| 97 | |
| 98 | assertTrue("No alarms should be present", manager.getAlarms().isEmpty()); |
| 99 | assertTrue("No active alarms should be present", manager.getActiveAlarms().isEmpty()); |
| 100 | assertTrue("The map should be empty per unknown device", |
| 101 | manager.getAlarmCounts(DeviceId.NONE).keySet().isEmpty()); |
| 102 | assertTrue("The counts should be empty", manager.getAlarmCounts().keySet().isEmpty()); |
| 103 | |
| 104 | assertEquals("Incorrect number of alarms for unknown device", |
| 105 | 0, manager.getAlarms(DeviceId.NONE).size()); |
| 106 | assertEquals("Incorrect number of major alarms for unknown device", |
| 107 | 0, manager.getAlarms(Alarm.SeverityLevel.MAJOR).size()); |
| 108 | |
| 109 | exception.expect(NullPointerException.class); |
| 110 | manager.getAlarm(null); |
| 111 | |
| 112 | exception.expect(ItemNotFoundException.class); |
| 113 | manager.getAlarm(AlarmId.alarmId(1)); |
| 114 | } |
| 115 | |
| 116 | @Test |
| 117 | public void testAlarmUpdates() throws InterruptedException { |
| 118 | |
| 119 | assertTrue("No alarms should be present", manager.getAlarms().isEmpty()); |
| 120 | manager.updateAlarms(DEVICE_ID, ImmutableSet.of()); |
| 121 | assertTrue("No alarms should be present", manager.getAlarms().isEmpty()); |
| 122 | Map<Alarm.SeverityLevel, Long> zeroAlarms = new CountsMapBuilder().create(); |
| 123 | assertEquals("No alarms count should be present", zeroAlarms, manager.getAlarmCounts()); |
| 124 | assertEquals("No alarms count should be present", zeroAlarms, manager.getAlarmCounts(DEVICE_ID)); |
| 125 | |
| 126 | manager.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_B, ALARM_A)); |
| 127 | verifyGettingSetsOfAlarms(manager, 2, 2); |
| 128 | validateEvents(AlarmEvent.Type.CREATED, AlarmEvent.Type.CREATED); |
| 129 | Map<Alarm.SeverityLevel, Long> critical2 = new CountsMapBuilder().with(CRITICAL, 2L).create(); |
| 130 | assertEquals("A critical should be present", critical2, manager.getAlarmCounts()); |
| 131 | assertEquals("A critical should be present", critical2, manager.getAlarmCounts(DEVICE_ID)); |
| 132 | |
| 133 | manager.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_A)); |
| 134 | verifyGettingSetsOfAlarms(manager, 2, 1); |
| 135 | validateEvents(AlarmEvent.Type.CREATED); |
| 136 | Map<Alarm.SeverityLevel, Long> critical1cleared1 = |
| 137 | new CountsMapBuilder().with(CRITICAL, 1L).with(CLEARED, 1L).create(); |
| 138 | assertEquals("A critical should be present and cleared", critical1cleared1, |
| 139 | manager.getAlarmCounts()); |
| 140 | assertEquals("A critical should be present and cleared", critical1cleared1, |
| 141 | manager.getAlarmCounts(DEVICE_ID)); |
| 142 | |
| 143 | // No change map when same alarms sent |
| 144 | manager.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_A)); |
| 145 | verifyGettingSetsOfAlarms(manager, 2, 1); |
| 146 | validateEvents(); |
| 147 | assertEquals("Map should not be changed for same alarm", critical1cleared1, |
| 148 | manager.getAlarmCounts()); |
| 149 | assertEquals("Map should not be changed for same alarm", critical1cleared1, |
| 150 | manager.getAlarmCounts(DEVICE_ID)); |
| 151 | |
| 152 | manager.updateAlarms(DEVICE_ID, ImmutableSet.of(ALARM_A, ALARM_A_WITHSRC)); |
| 153 | verifyGettingSetsOfAlarms(manager, 3, 2); |
| 154 | validateEvents(AlarmEvent.Type.CREATED); |
| 155 | Map<Alarm.SeverityLevel, Long> critical2cleared1 = |
| 156 | new CountsMapBuilder().with(CRITICAL, 2L).with(CLEARED, 1L).create(); |
| 157 | assertEquals("A critical should be present", critical2cleared1, manager.getAlarmCounts()); |
| 158 | assertEquals("A critical should be present", critical2cleared1, manager.getAlarmCounts(DEVICE_ID)); |
| 159 | |
| 160 | manager.updateAlarms(DEVICE_ID, ImmutableSet.of()); |
| 161 | verifyGettingSetsOfAlarms(manager, 3, 0); |
| 162 | validateEvents(AlarmEvent.Type.CREATED, AlarmEvent.Type.CREATED); |
| 163 | assertEquals(new CountsMapBuilder().with(CLEARED, 3L).create(), manager.getAlarmCounts(DEVICE_ID)); |
| 164 | |
| 165 | assertEquals("The counts should be empty for unknown devices", zeroAlarms, |
| 166 | manager.getAlarmCounts(DeviceId.NONE)); |
| 167 | assertEquals("The counts should be empty for unknown devices", zeroAlarms, |
| 168 | manager.getAlarmCounts(DeviceId.deviceId("junk:junk"))); |
| 169 | |
| 170 | } |
| 171 | |
| 172 | private void verifyGettingSetsOfAlarms(AlarmManager am, int expectedTotal, int expectedActive) { |
| 173 | assertEquals("Incorrect total alarms", expectedTotal, am.getAlarms().size()); |
| 174 | assertEquals("Incorrect active alarms count", expectedActive, am.getActiveAlarms().size()); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Method to validate that actual versus expected device key events were |
| 179 | * received correctly. |
| 180 | * |
| 181 | * @param types expected device key events. |
| 182 | */ |
| 183 | private void validateEvents(Enum... types) { |
| 184 | TestTools.assertAfter(100, () -> { |
| 185 | int i = 0; |
| 186 | assertEquals("wrong events received", types.length, listener.events.size()); |
| 187 | for (Event event : listener.events) { |
| 188 | assertEquals("incorrect event type", types[i], event.type()); |
| 189 | i++; |
| 190 | } |
| 191 | listener.events.clear(); |
| 192 | }); |
| 193 | } |
| 194 | |
| 195 | private static class CountsMapBuilder { |
| 196 | |
| 197 | private final Map<Alarm.SeverityLevel, Long> map = new HashMap<>(); |
| 198 | |
| 199 | public CountsMapBuilder with(Alarm.SeverityLevel sev, Long count) { |
| 200 | map.put(sev, count); |
| 201 | return this; |
| 202 | } |
| 203 | |
| 204 | public Map<Alarm.SeverityLevel, Long> create() { |
| 205 | return Collections.unmodifiableMap(map); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Test listener class to receive alarm events. |
| 212 | */ |
| 213 | private static class TestListener implements AlarmListener { |
| 214 | |
| 215 | protected List<AlarmEvent> events = Lists.newArrayList(); |
| 216 | |
| 217 | @Override |
| 218 | public void event(AlarmEvent event) { |
| 219 | events.add(event); |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | } |