blob: 12d36b8f4494f62a3a6f5c991b8cafd058de7afa [file] [log] [blame]
Sean Condon87b78502018-09-17 20:53:24 +01001/*
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
17package org.onosproject.faultmanagement.alarms.cli;
18
Ray Milkeydf521292018-10-04 15:13:33 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Sean Condon87b78502018-09-17 20:53:24 +010022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.faultmanagement.api.AlarmStore;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080024import org.onosproject.alarm.Alarm;
25import org.onosproject.alarm.AlarmId;
26import org.onosproject.alarm.DefaultAlarm;
Sean Condon87b78502018-09-17 20:53:24 +010027import org.onosproject.net.DeviceId;
28import org.onosproject.net.device.DeviceService;
29
30import java.time.Instant;
31
32/**
33 * Creates a default alarm on a device.
34 */
Ray Milkeydf521292018-10-04 15:13:33 -070035@Service
Sean Condon87b78502018-09-17 20:53:24 +010036@Command(scope = "onos", name = "alarm-create",
37 description = "Creates an alarm")
38public 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 Milkeydf521292018-10-04 15:13:33 -070061 protected void doExecute() {
Sean Condon87b78502018-09-17 20:53:24 +010062 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}