kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.faultmanagement.alarms.cli; |
| 17 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 18 | import org.apache.commons.lang3.builder.ToStringBuilder; |
| 19 | import org.apache.commons.lang3.builder.ToStringStyle; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 79123af | 2018-10-03 13:28:31 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.Option; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 79123af | 2018-10-03 13:28:31 -0700 | [diff] [blame] | 26 | import org.onosproject.cli.net.DeviceIdCompleter; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 27 | import org.onosproject.alarm.Alarm; |
| 28 | import org.onosproject.alarm.AlarmService; |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | |
| 31 | import java.util.Set; |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Lists alarms across all devices. |
| 35 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 36 | @Service |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 37 | @Command(scope = "onos", name = "alarms", |
| 38 | description = "Lists alarms") |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 39 | public class GetAllAlarms extends AbstractShellCommand { |
| 40 | |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 41 | @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 Milkey | 79123af | 2018-10-03 13:28:31 -0700 | [diff] [blame] | 47 | @Completion(DeviceIdCompleter.class) |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 48 | String deviceId = null; |
| 49 | |
| 50 | private AlarmService alarmService = AbstractShellCommand.get(AlarmService.class); |
| 51 | private Set<Alarm> alarms; |
| 52 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 53 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 54 | protected void doExecute() { |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 55 | 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); |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 69 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 70 | void printAlarms(Set<Alarm> alarms) { |
Andrea Campanella | 17f9ab6 | 2017-01-24 14:57:34 -0800 | [diff] [blame] | 71 | //FIXME this can be better formatted |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 72 | alarms.forEach((alarm) -> { |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 73 | print(ToStringBuilder.reflectionToString(alarm, ToStringStyle.SHORT_PREFIX_STYLE)); |
| 74 | }); |
| 75 | } |
| 76 | } |