blob: 1ce2ed21f4a7ad5fe38e8cdc2da99ca7dff4d98e [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;
Ray Milkey79123af2018-10-03 13:28:31 -070022import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070023import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
kmcpeakeb172d5f2015-12-10 11:30:43 +000025import org.onosproject.cli.AbstractShellCommand;
Ray Milkey79123af2018-10-03 13:28:31 -070026import org.onosproject.cli.net.DeviceIdCompleter;
kmcpeakeb172d5f2015-12-10 11:30:43 +000027import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
28import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
Andrea Campanella17f9ab62017-01-24 14:57:34 -080029import org.onosproject.net.DeviceId;
30
31import java.util.Map;
32import java.util.stream.Collectors;
kmcpeakeb172d5f2015-12-10 11:30:43 +000033
34/**
35 * Lists alarm counts across all devices.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Andrea Campanella17f9ab62017-01-24 14:57:34 -080038@Command(scope = "onos", name = "alarms-counts",
39 description = "Lists the count of alarms for each severity")
kmcpeakeb172d5f2015-12-10 11:30:43 +000040public class GetAllAlarmsCounts extends AbstractShellCommand {
41
Andrea Campanella17f9ab62017-01-24 14:57:34 -080042 @Option(name = "-a", aliases = "--active", description = "Shows ACTIVE alarms only",
43 required = false, multiValued = false)
44 private boolean activeOnly = false;
45
46 @Argument(index = 0, name = "deviceId", description = "Device identity",
47 required = false, multiValued = false)
Ray Milkey79123af2018-10-03 13:28:31 -070048 @Completion(DeviceIdCompleter.class)
Andrea Campanella17f9ab62017-01-24 14:57:34 -080049 String deviceId = null;
50
51 private AlarmService alarmService = AbstractShellCommand.get(AlarmService.class);
52 private Map<Alarm.SeverityLevel, Long> alarmCounts;
53
kmcpeakeb172d5f2015-12-10 11:30:43 +000054 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070055 protected void doExecute() {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080056 if (deviceId != null) {
57 if (activeOnly) {
58 alarmCounts = alarmService.getActiveAlarms(DeviceId.deviceId(deviceId))
59 .stream().filter(a -> !a.severity().equals(Alarm.SeverityLevel.CLEARED))
60 .collect(Collectors.groupingBy(Alarm::severity, Collectors.counting()));
61 } else {
62 alarmCounts = alarmService.
63 getAlarmCounts(DeviceId.deviceId(deviceId));
64 }
65 } else if (activeOnly) {
66 alarmCounts = alarmService.getActiveAlarms()
67 .stream().filter(a -> !a.severity().equals(Alarm.SeverityLevel.CLEARED))
68 .collect(Collectors.groupingBy(Alarm::severity, Collectors.counting()));
69 } else {
70 alarmCounts = alarmService.getAlarmCounts();
71 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000072 printCounts(alarmCounts);
73 }
74
Andrea Campanellae72ac552016-04-11 10:04:52 -070075 void printCounts(Map<Alarm.SeverityLevel, Long> alarmCounts) {
Yuta HIGUCHId1e42f42017-01-24 20:04:10 -080076 alarmCounts.entrySet().stream()
77 .sorted(comparingInt(e -> e.getKey().ordinal()))
78 .forEach((countEntry) -> {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080079 print(String.format("%s, %d", countEntry.getKey(), countEntry.getValue()));
kmcpeakeb172d5f2015-12-10 11:30:43 +000080 });
81 }
82}