blob: deefd2270c79dde2dce51d1681a8cd814f7b8a07 [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;
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.Set;
kmcpeakeb172d5f2015-12-10 11:30:43 +000030
31/**
32 * Lists alarms across all devices.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
Andrea Campanella17f9ab62017-01-24 14:57:34 -080035@Command(scope = "onos", name = "alarms",
36 description = "Lists alarms")
kmcpeakeb172d5f2015-12-10 11:30:43 +000037public class GetAllAlarms extends AbstractShellCommand {
38
Andrea Campanella17f9ab62017-01-24 14:57:34 -080039 @Option(name = "-a", aliases = "--active", description = "Shows ACTIVE alarms only",
40 required = false, multiValued = false)
41 private boolean activeOnly = false;
42
43 @Argument(index = 0, name = "deviceId", description = "Device identity",
44 required = false, multiValued = false)
45 String deviceId = null;
46
47 private AlarmService alarmService = AbstractShellCommand.get(AlarmService.class);
48 private Set<Alarm> alarms;
49
kmcpeakeb172d5f2015-12-10 11:30:43 +000050 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070051 protected void doExecute() {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080052 if (deviceId != null) {
53 if (activeOnly) {
54 alarms = alarmService.getActiveAlarms(DeviceId.deviceId(deviceId));
55 } else {
56 alarms = alarmService.getAlarms(DeviceId.deviceId(deviceId));
57 }
58 } else if (activeOnly) {
59 alarms = alarmService.getActiveAlarms();
60 } else {
61 alarms = alarmService.getAlarms();
62 }
63 printAlarms(alarms);
kmcpeakeb172d5f2015-12-10 11:30:43 +000064 }
65
Andrea Campanella17f9ab62017-01-24 14:57:34 -080066
kmcpeakeb172d5f2015-12-10 11:30:43 +000067 void printAlarms(Set<Alarm> alarms) {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080068 //FIXME this can be better formatted
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070069 alarms.forEach((alarm) -> {
kmcpeakeb172d5f2015-12-10 11:30:43 +000070 print(ToStringBuilder.reflectionToString(alarm, ToStringStyle.SHORT_PREFIX_STYLE));
71 });
72 }
73}