blob: 0fa2e3e142d6b87c58efd11556e88d1074c18176 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
kmcpeakeb172d5f2015-12-10 11:30:43 +000024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
26import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService;
Andrea Campanella17f9ab62017-01-24 14:57:34 -080027import org.onosproject.net.DeviceId;
28
29import java.util.Map;
30import java.util.stream.Collectors;
kmcpeakeb172d5f2015-12-10 11:30:43 +000031
32/**
33 * Lists alarm counts across all devices.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Andrea Campanella17f9ab62017-01-24 14:57:34 -080036@Command(scope = "onos", name = "alarms-counts",
37 description = "Lists the count of alarms for each severity")
kmcpeakeb172d5f2015-12-10 11:30:43 +000038public class GetAllAlarmsCounts extends AbstractShellCommand {
39
Andrea Campanella17f9ab62017-01-24 14:57:34 -080040 @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
kmcpeakeb172d5f2015-12-10 11:30:43 +000051 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070052 protected void doExecute() {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080053 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 }
kmcpeakeb172d5f2015-12-10 11:30:43 +000069 printCounts(alarmCounts);
70 }
71
Andrea Campanellae72ac552016-04-11 10:04:52 -070072 void printCounts(Map<Alarm.SeverityLevel, Long> alarmCounts) {
Yuta HIGUCHId1e42f42017-01-24 20:04:10 -080073 alarmCounts.entrySet().stream()
74 .sorted(comparingInt(e -> e.getKey().ordinal()))
75 .forEach((countEntry) -> {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080076 print(String.format("%s, %d", countEntry.getKey(), countEntry.getValue()));
kmcpeakeb172d5f2015-12-10 11:30:43 +000077 });
78 }
79}