kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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.alarms.cli; |
| 17 | |
Yuta HIGUCHI | d1e42f4 | 2017-01-24 20:04:10 -0800 | [diff] [blame] | 18 | import static java.util.Comparator.comparingInt; |
| 19 | |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Argument; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Option; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService; |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | import java.util.stream.Collectors; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Lists alarm counts across all devices. |
| 33 | */ |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 34 | @Command(scope = "onos", name = "alarms-counts", |
| 35 | description = "Lists the count of alarms for each severity") |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 36 | public class GetAllAlarmsCounts extends AbstractShellCommand { |
| 37 | |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 38 | @Option(name = "-a", aliases = "--active", description = "Shows ACTIVE alarms only", |
| 39 | required = false, multiValued = false) |
| 40 | private boolean activeOnly = false; |
| 41 | |
| 42 | @Argument(index = 0, name = "deviceId", description = "Device identity", |
| 43 | required = false, multiValued = false) |
| 44 | String deviceId = null; |
| 45 | |
| 46 | private AlarmService alarmService = AbstractShellCommand.get(AlarmService.class); |
| 47 | private Map<Alarm.SeverityLevel, Long> alarmCounts; |
| 48 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 49 | @Override |
| 50 | protected void execute() { |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 51 | if (deviceId != null) { |
| 52 | if (activeOnly) { |
| 53 | alarmCounts = alarmService.getActiveAlarms(DeviceId.deviceId(deviceId)) |
| 54 | .stream().filter(a -> !a.severity().equals(Alarm.SeverityLevel.CLEARED)) |
| 55 | .collect(Collectors.groupingBy(Alarm::severity, Collectors.counting())); |
| 56 | } else { |
| 57 | alarmCounts = alarmService. |
| 58 | getAlarmCounts(DeviceId.deviceId(deviceId)); |
| 59 | } |
| 60 | } else if (activeOnly) { |
| 61 | alarmCounts = alarmService.getActiveAlarms() |
| 62 | .stream().filter(a -> !a.severity().equals(Alarm.SeverityLevel.CLEARED)) |
| 63 | .collect(Collectors.groupingBy(Alarm::severity, Collectors.counting())); |
| 64 | } else { |
| 65 | alarmCounts = alarmService.getAlarmCounts(); |
| 66 | } |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 67 | printCounts(alarmCounts); |
| 68 | } |
| 69 | |
Andrea Campanella | e72ac55 | 2016-04-11 10:04:52 -0700 | [diff] [blame] | 70 | void printCounts(Map<Alarm.SeverityLevel, Long> alarmCounts) { |
Yuta HIGUCHI | d1e42f4 | 2017-01-24 20:04:10 -0800 | [diff] [blame] | 71 | alarmCounts.entrySet().stream() |
| 72 | .sorted(comparingInt(e -> e.getKey().ordinal())) |
| 73 | .forEach((countEntry) -> { |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 74 | print(String.format("%s, %d", countEntry.getKey(), countEntry.getValue())); |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 75 | }); |
| 76 | } |
| 77 | } |