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 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.faultmanagement.api.AlarmStore; |
| 23 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 24 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | |
| 29 | import java.time.Instant; |
| 30 | |
| 31 | /** |
| 32 | * Creates a default alarm on a device. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "alarm-create", |
| 35 | description = "Creates an alarm") |
| 36 | public class CreateAlarm extends AbstractShellCommand { |
| 37 | |
| 38 | @Argument(index = 0, name = "deviceId", description = "Device identity", |
| 39 | required = true, multiValued = false) |
| 40 | String deviceIdStr = null; |
| 41 | |
| 42 | @Argument(index = 1, name = "severity", description = "Severity level", |
| 43 | required = true, multiValued = false) |
| 44 | String severityStr = null; |
| 45 | |
| 46 | @Argument(index = 2, name = "alarmId", description = "Unique alarm id", |
| 47 | required = true, multiValued = false) |
| 48 | String alarmId = null; |
| 49 | |
| 50 | @Argument(index = 3, name = "desc", description = "Alarm description", |
| 51 | required = true, multiValued = false) |
| 52 | String desc = null; |
| 53 | |
| 54 | private AlarmStore alarmStore = AbstractShellCommand.get(AlarmStore.class); |
| 55 | |
| 56 | private DeviceService deviceManager = AbstractShellCommand.get(DeviceService.class); |
| 57 | |
| 58 | @Override |
| 59 | protected void execute() { |
| 60 | DeviceId deviceId = DeviceId.deviceId(deviceIdStr); |
| 61 | if (!deviceManager.isAvailable(deviceId)) { |
| 62 | throw new IllegalArgumentException("Device " + deviceIdStr + " is not available"); |
| 63 | } |
| 64 | |
| 65 | Alarm.SeverityLevel severityLevel = Alarm.SeverityLevel.valueOf(severityStr); |
| 66 | |
| 67 | Alarm newAlarm = new DefaultAlarm.Builder( |
| 68 | AlarmId.alarmId(deviceId, alarmId), |
| 69 | deviceId, |
| 70 | desc, |
| 71 | severityLevel, |
| 72 | Instant.now().toEpochMilli()) |
| 73 | .build(); |
| 74 | alarmStore.createOrUpdateAlarm(newAlarm); |
| 75 | } |
| 76 | } |