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 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import java.util.Collections; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.HashSet; |
| 22 | import java.util.Map; |
| 23 | import org.junit.Test; |
| 24 | import static org.junit.Assert.*; |
| 25 | import org.onlab.util.ItemNotFoundException; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import static org.hamcrest.Matchers.containsString; |
| 28 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 29 | import static org.onosproject.incubator.net.faultmanagement.alarm.Alarm.SeverityLevel.*; |
| 30 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 31 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 32 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 33 | |
| 34 | public class AlarmsManagerTest { |
| 35 | |
| 36 | @Test |
| 37 | public void testGettersWhenNoAlarms() { |
| 38 | AlarmsManager am = new AlarmsManager(); |
| 39 | assertTrue("No alarms", am.getAlarms().isEmpty()); |
| 40 | assertTrue("No active alarms", am.getActiveAlarms().isEmpty()); |
| 41 | assertTrue("No alarms gives empty map per unknown device", am.getAlarmCounts(DeviceId.NONE).keySet().isEmpty()); |
| 42 | assertTrue("No alarms gives empty map", am.getAlarmCounts().keySet().isEmpty()); |
| 43 | |
| 44 | assertEquals("Zero alarms for that device", 0, am.getAlarms(DeviceId.NONE).size()); |
| 45 | assertEquals("Zero major alarms", 0, am.getAlarms(Alarm.SeverityLevel.MAJOR).size()); |
| 46 | |
| 47 | try { |
| 48 | assertEquals("no alarms", 0, am.getAlarm(null)); |
| 49 | } catch (NullPointerException ex) { |
| 50 | assertThat(ex.getMessage(), |
| 51 | containsString("cannot be null")); |
| 52 | } |
| 53 | |
| 54 | try { |
| 55 | assertEquals("no alarms", 0, am.getAlarm(AlarmId.alarmId(1))); |
| 56 | } catch (ItemNotFoundException ex) { |
| 57 | assertThat(ex.getMessage(), |
| 58 | containsString("not found")); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testAlarmUpdates() { |
| 64 | AlarmsManager am = new AlarmsManager(); |
| 65 | assertTrue("no alarms", am.getAlarms().isEmpty()); |
| 66 | |
| 67 | am.updateAlarms(new HashSet<>(), DEVICE_ID); |
| 68 | assertTrue("still no alarms", am.getAlarms().isEmpty()); |
| 69 | Map<Alarm.SeverityLevel, Long> zeroAlarms = new CountsMapBuilder().create(); |
| 70 | assertEquals(zeroAlarms, am.getAlarmCounts()); |
| 71 | assertEquals(zeroAlarms, am.getAlarmCounts(DEVICE_ID)); |
| 72 | |
| 73 | am.updateAlarms(Sets.newHashSet(ALARM_B, ALARM_A), DEVICE_ID); |
| 74 | verifyGettingSetsOfAlarms(am, 2, 2); |
| 75 | Map<Alarm.SeverityLevel, Long> critical2 = new CountsMapBuilder().with(CRITICAL, 2L).create(); |
| 76 | assertEquals(critical2, am.getAlarmCounts()); |
| 77 | assertEquals(critical2, am.getAlarmCounts(DEVICE_ID)); |
| 78 | |
| 79 | am.updateAlarms(Sets.newHashSet(ALARM_A), DEVICE_ID); |
| 80 | verifyGettingSetsOfAlarms(am, 2, 1); |
| 81 | Map<Alarm.SeverityLevel, Long> critical1cleared1 = |
| 82 | new CountsMapBuilder().with(CRITICAL, 1L).with(CLEARED, 1L).create(); |
| 83 | assertEquals(critical1cleared1, am.getAlarmCounts()); |
| 84 | assertEquals(critical1cleared1, am.getAlarmCounts(DEVICE_ID)); |
| 85 | |
| 86 | // No change map when same alarms sent |
| 87 | am.updateAlarms(Sets.newHashSet(ALARM_A), DEVICE_ID); |
| 88 | verifyGettingSetsOfAlarms(am, 2, 1); |
| 89 | assertEquals(critical1cleared1, am.getAlarmCounts()); |
| 90 | assertEquals(critical1cleared1, am.getAlarmCounts(DEVICE_ID)); |
| 91 | |
| 92 | am.updateAlarms(Sets.newHashSet(ALARM_A, ALARM_A_WITHSRC), DEVICE_ID); |
| 93 | verifyGettingSetsOfAlarms(am, 3, 2); |
| 94 | Map<Alarm.SeverityLevel, Long> critical2cleared1 = |
| 95 | new CountsMapBuilder().with(CRITICAL, 2L).with(CLEARED, 1L).create(); |
| 96 | assertEquals(critical2cleared1, am.getAlarmCounts()); |
| 97 | assertEquals(critical2cleared1, am.getAlarmCounts(DEVICE_ID)); |
| 98 | |
| 99 | am.updateAlarms(Sets.newHashSet(), DEVICE_ID); |
| 100 | verifyGettingSetsOfAlarms(am, 3, 0); |
| 101 | assertEquals(new CountsMapBuilder().with(CLEARED, 3L).create(), am.getAlarmCounts(DEVICE_ID)); |
| 102 | |
| 103 | assertEquals("No alarms for unknown devices", zeroAlarms, am.getAlarmCounts(DeviceId.NONE)); |
| 104 | assertEquals("No alarms for unknown devices", zeroAlarms, am.getAlarmCounts(DeviceId.deviceId("junk:junk"))); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | private void verifyGettingSetsOfAlarms(AlarmsManager am, int expectedTotal, int expectedActive) { |
| 109 | assertEquals("Wrong total", expectedTotal, am.getAlarms().size()); |
| 110 | assertEquals("Wrong active count", expectedActive, am.getActiveAlarms().size()); |
| 111 | } |
| 112 | private static final DeviceId DEVICE_ID = DeviceId.deviceId("foo:bar"); |
| 113 | private static final DefaultAlarm ALARM_A = new DefaultAlarm.Builder( |
| 114 | DEVICE_ID, "aaa", Alarm.SeverityLevel.CRITICAL, 0).build(); |
| 115 | |
| 116 | private static final DefaultAlarm ALARM_A_WITHSRC = new DefaultAlarm.Builder( |
| 117 | ALARM_A).forSource(AlarmEntityId.alarmEntityId("port:foo")).build(); |
| 118 | |
| 119 | private static final DefaultAlarm ALARM_B = new DefaultAlarm.Builder( |
| 120 | DEVICE_ID, "bbb", Alarm.SeverityLevel.CRITICAL, 0).build(); |
| 121 | |
| 122 | private static class CountsMapBuilder { |
| 123 | |
| 124 | private final Map<Alarm.SeverityLevel, Long> map = new HashMap<>(); |
| 125 | |
| 126 | public CountsMapBuilder with(Alarm.SeverityLevel sev, Long count) { |
| 127 | map.put(sev, count); |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | public Map<Alarm.SeverityLevel, Long> create() { |
| 132 | return Collections.unmodifiableMap(map); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } |