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