blob: 1c48dfb40f224bcd5f78bfb2cc21cb5bd17bb0cf [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;
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.Set;
kmcpeakeb172d5f2015-12-10 11:30:43 +000032
33/**
34 * Lists alarms across all devices.
35 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
Andrea Campanella17f9ab62017-01-24 14:57:34 -080037@Command(scope = "onos", name = "alarms",
38 description = "Lists alarms")
kmcpeakeb172d5f2015-12-10 11:30:43 +000039public class GetAllAlarms extends AbstractShellCommand {
40
Andrea Campanella17f9ab62017-01-24 14:57:34 -080041 @Option(name = "-a", aliases = "--active", description = "Shows ACTIVE alarms only",
42 required = false, multiValued = false)
43 private boolean activeOnly = false;
44
45 @Argument(index = 0, name = "deviceId", description = "Device identity",
46 required = false, multiValued = false)
Ray Milkey79123af2018-10-03 13:28:31 -070047 @Completion(DeviceIdCompleter.class)
Andrea Campanella17f9ab62017-01-24 14:57:34 -080048 String deviceId = null;
49
50 private AlarmService alarmService = AbstractShellCommand.get(AlarmService.class);
51 private Set<Alarm> alarms;
52
kmcpeakeb172d5f2015-12-10 11:30:43 +000053 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070054 protected void doExecute() {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080055 if (deviceId != null) {
56 if (activeOnly) {
57 alarms = alarmService.getActiveAlarms(DeviceId.deviceId(deviceId));
58 } else {
59 alarms = alarmService.getAlarms(DeviceId.deviceId(deviceId));
60 }
61 } else if (activeOnly) {
62 alarms = alarmService.getActiveAlarms();
63 } else {
64 alarms = alarmService.getAlarms();
65 }
66 printAlarms(alarms);
kmcpeakeb172d5f2015-12-10 11:30:43 +000067 }
68
Andrea Campanella17f9ab62017-01-24 14:57:34 -080069
kmcpeakeb172d5f2015-12-10 11:30:43 +000070 void printAlarms(Set<Alarm> alarms) {
Andrea Campanella17f9ab62017-01-24 14:57:34 -080071 //FIXME this can be better formatted
Sho SHIMIZUa09e1bb2016-08-01 14:25:25 -070072 alarms.forEach((alarm) -> {
kmcpeakeb172d5f2015-12-10 11:30:43 +000073 print(ToStringBuilder.reflectionToString(alarm, ToStringStyle.SHORT_PREFIX_STYLE));
74 });
75 }
76}