blob: a5413dcd54e44bee6cbcd7f2b8a7adc9f80c8c56 [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
kmcpeakeb172d5f2015-12-10 11:30:43 +00003 *
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 */
16package org.onosproject.faultmanagement.alarms.cli;
17
Andrea Campanella17f9ab62017-01-24 14:57:34 -080018import org.apache.karaf.shell.commands.Argument;
kmcpeakeb172d5f2015-12-10 11:30:43 +000019import org.apache.karaf.shell.commands.Command;
Andrea Campanella17f9ab62017-01-24 14:57:34 -080020import org.apache.karaf.shell.commands.Option;
kmcpeakeb172d5f2015-12-10 11:30:43 +000021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
23import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
Andrea Campanella17f9ab62017-01-24 14:57:34 -080024import org.onosproject.net.DeviceId;
25
26import java.util.Map;
27import java.util.stream.Collectors;
kmcpeakeb172d5f2015-12-10 11:30:43 +000028
29/**
30 * Lists alarm counts across all devices.
31 */
Andrea Campanella17f9ab62017-01-24 14:57:34 -080032@Command(scope = "onos", name = "alarms-counts",
33 description = "Lists the count of alarms for each severity")
kmcpeakeb172d5f2015-12-10 11:30:43 +000034public class GetAllAlarmsCounts extends AbstractShellCommand {
35
Andrea Campanella17f9ab62017-01-24 14:57:34 -080036 @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
kmcpeakeb172d5f2015-12-10 11:30:43 +000047 @Override
48 protected void execute() {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080049 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 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000065 printCounts(alarmCounts);
66 }
67
Andrea Campanellae72ac552016-04-11 10:04:52 -070068 void printCounts(Map<Alarm.SeverityLevel, Long> alarmCounts) {
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070069 alarmCounts.entrySet().forEach((countEntry) -> {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080070 print(String.format("%s, %d", countEntry.getKey(), countEntry.getValue()));
kmcpeakeb172d5f2015-12-10 11:30:43 +000071
72 });
73 }
74}