blob: 5bbb2ceeeb5c4bfe31685229726a775802c83056 [file] [log] [blame]
kmcpeakeb172d5f2015-12-10 11:30:43 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Yuta HIGUCHId1e42f42017-01-24 20:04:10 -080018import static java.util.Comparator.comparingInt;
19
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.Option;
kmcpeakeb172d5f2015-12-10 11:30:43 +000023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
25import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
Andrea Campanella17f9ab62017-01-24 14:57:34 -080026import org.onosproject.net.DeviceId;
27
28import java.util.Map;
29import java.util.stream.Collectors;
kmcpeakeb172d5f2015-12-10 11:30:43 +000030
31/**
32 * Lists alarm counts across all devices.
33 */
Andrea Campanella17f9ab62017-01-24 14:57:34 -080034@Command(scope = "onos", name = "alarms-counts",
35 description = "Lists the count of alarms for each severity")
kmcpeakeb172d5f2015-12-10 11:30:43 +000036public class GetAllAlarmsCounts extends AbstractShellCommand {
37
Andrea Campanella17f9ab62017-01-24 14:57:34 -080038 @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
kmcpeakeb172d5f2015-12-10 11:30:43 +000049 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070050 protected void doExecute() {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080051 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 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000067 printCounts(alarmCounts);
68 }
69
Andrea Campanellae72ac552016-04-11 10:04:52 -070070 void printCounts(Map<Alarm.SeverityLevel, Long> alarmCounts) {
Yuta HIGUCHId1e42f42017-01-24 20:04:10 -080071 alarmCounts.entrySet().stream()
72 .sorted(comparingInt(e -> e.getKey().ordinal()))
73 .forEach((countEntry) -> {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080074 print(String.format("%s, %d", countEntry.getKey(), countEntry.getValue()));
kmcpeakeb172d5f2015-12-10 11:30:43 +000075 });
76 }
77}