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