Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.faultmanagement.alarms.cli; |
| 18 | |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
| 23 | import org.onosproject.faultmanagement.api.AlarmStore; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 24 | import org.onosproject.alarm.Alarm; |
| 25 | import org.onosproject.alarm.AlarmId; |
| 26 | import org.onosproject.alarm.DefaultAlarm; |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | |
| 30 | import java.time.Instant; |
| 31 | |
| 32 | /** |
| 33 | * Creates a default alarm on a device. |
| 34 | */ |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 35 | @Service |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 36 | @Command(scope = "onos", name = "alarm-create", |
| 37 | description = "Creates an alarm") |
| 38 | public class CreateAlarm extends AbstractShellCommand { |
| 39 | |
| 40 | @Argument(index = 0, name = "deviceId", description = "Device identity", |
| 41 | required = true, multiValued = false) |
| 42 | String deviceIdStr = null; |
| 43 | |
| 44 | @Argument(index = 1, name = "severity", description = "Severity level", |
| 45 | required = true, multiValued = false) |
| 46 | String severityStr = null; |
| 47 | |
| 48 | @Argument(index = 2, name = "alarmId", description = "Unique alarm id", |
| 49 | required = true, multiValued = false) |
| 50 | String alarmId = null; |
| 51 | |
| 52 | @Argument(index = 3, name = "desc", description = "Alarm description", |
| 53 | required = true, multiValued = false) |
| 54 | String desc = null; |
| 55 | |
| 56 | private AlarmStore alarmStore = AbstractShellCommand.get(AlarmStore.class); |
| 57 | |
| 58 | private DeviceService deviceManager = AbstractShellCommand.get(DeviceService.class); |
| 59 | |
| 60 | @Override |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 61 | protected void doExecute() { |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 62 | DeviceId deviceId = DeviceId.deviceId(deviceIdStr); |
| 63 | if (!deviceManager.isAvailable(deviceId)) { |
| 64 | throw new IllegalArgumentException("Device " + deviceIdStr + " is not available"); |
| 65 | } |
| 66 | |
| 67 | Alarm.SeverityLevel severityLevel = Alarm.SeverityLevel.valueOf(severityStr); |
| 68 | |
| 69 | Alarm newAlarm = new DefaultAlarm.Builder( |
| 70 | AlarmId.alarmId(deviceId, alarmId), |
| 71 | deviceId, |
| 72 | desc, |
| 73 | severityLevel, |
| 74 | Instant.now().toEpochMilli()) |
| 75 | .build(); |
| 76 | alarmStore.createOrUpdateAlarm(newAlarm); |
| 77 | } |
| 78 | } |