blob: 65614dca4b9abe6405b4d72e57596992524a5628 [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
kmcpeakeb172d5f2015-12-10 11:30:43 +000018import org.apache.commons.lang3.builder.ToStringBuilder;
19import org.apache.commons.lang3.builder.ToStringStyle;
Andrea Campanella17f9ab62017-01-24 14:57:34 -080020import org.apache.karaf.shell.commands.Argument;
kmcpeakeb172d5f2015-12-10 11:30:43 +000021import org.apache.karaf.shell.commands.Command;
Andrea Campanella17f9ab62017-01-24 14:57:34 -080022import org.apache.karaf.shell.commands.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.Set;
kmcpeakeb172d5f2015-12-10 11:30:43 +000029
30/**
31 * Lists alarms across all devices.
32 */
Andrea Campanella17f9ab62017-01-24 14:57:34 -080033@Command(scope = "onos", name = "alarms",
34 description = "Lists alarms")
kmcpeakeb172d5f2015-12-10 11:30:43 +000035public class GetAllAlarms extends AbstractShellCommand {
36
Andrea Campanella17f9ab62017-01-24 14:57:34 -080037 @Option(name = "-a", aliases = "--active", description = "Shows ACTIVE alarms only",
38 required = false, multiValued = false)
39 private boolean activeOnly = false;
40
41 @Argument(index = 0, name = "deviceId", description = "Device identity",
42 required = false, multiValued = false)
43 String deviceId = null;
44
45 private AlarmService alarmService = AbstractShellCommand.get(AlarmService.class);
46 private Set<Alarm> alarms;
47
kmcpeakeb172d5f2015-12-10 11:30:43 +000048 @Override
49 protected void execute() {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080050 if (deviceId != null) {
51 if (activeOnly) {
52 alarms = alarmService.getActiveAlarms(DeviceId.deviceId(deviceId));
53 } else {
54 alarms = alarmService.getAlarms(DeviceId.deviceId(deviceId));
55 }
56 } else if (activeOnly) {
57 alarms = alarmService.getActiveAlarms();
58 } else {
59 alarms = alarmService.getAlarms();
60 }
61 printAlarms(alarms);
kmcpeakeb172d5f2015-12-10 11:30:43 +000062 }
63
Andrea Campanella17f9ab62017-01-24 14:57:34 -080064
kmcpeakeb172d5f2015-12-10 11:30:43 +000065 void printAlarms(Set<Alarm> alarms) {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080066 //FIXME this can be better formatted
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070067 alarms.forEach((alarm) -> {
kmcpeakeb172d5f2015-12-10 11:30:43 +000068 print(ToStringBuilder.reflectionToString(alarm, ToStringStyle.SHORT_PREFIX_STYLE));
69 });
70 }
71}