blob: 5766ed76cc6da260e10b52f7faf6f8f64843ec69 [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
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.faultmanagement.api.AlarmStore;
23import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
24import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
25import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
26import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
27
28import java.time.Instant;
29
30/**
31 * Updates an existing alarm.
32 */
33@Command(scope = "onos", name = "alarm-update",
34 description = "Updates an alarm")
35public class UpdateAlarm extends AbstractShellCommand {
36
37 @Argument(index = 0, name = "alarmId", description = "Unique alarm id",
38 required = true, multiValued = false)
39 String alarmId = null;
40
41 @Argument(index = 1, name = "desc", description = "Alarm field",
42 required = true, multiValued = false)
43 String alarmField = null;
44
45 @Argument(index = 2, name = "value", description = "The new value of the chosen Alarm field.",
46 required = true, multiValued = false)
47 String alarmFieldValue = null;
48
49 private AlarmStore alarmStore = AbstractShellCommand.get(AlarmStore.class);
50
51 @Override
52 protected void execute() {
53 Alarm existing = alarmStore.getAlarm(AlarmId.alarmId(alarmId));
54
55 DefaultAlarm.Builder newAlarmBuilder = new DefaultAlarm.Builder(existing);
56 UpdateAlarm.AlarmField field = UpdateAlarm.AlarmField.valueOf(alarmField);
57 switch (field) {
58 case SOURCE:
59 AlarmEntityId sourceId = AlarmEntityId.alarmEntityId(alarmFieldValue);
60 newAlarmBuilder.forSource(sourceId);
61 break;
62 case ASSIGNED_USER:
63 newAlarmBuilder.withAssignedUser(alarmFieldValue);
64 break;
65 case ACKNOWLEDGED:
66 newAlarmBuilder.withAcknowledged("TRUE".equalsIgnoreCase(alarmFieldValue));
67 break;
68 case MANUALLY_CLEARABLE:
69 newAlarmBuilder.withManuallyClearable("TRUE".equalsIgnoreCase(alarmFieldValue));
70 break;
71 case SERVICE_AFFECTING:
72 newAlarmBuilder.withServiceAffecting("TRUE".equalsIgnoreCase(alarmFieldValue));
73 break;
74 case TIME_CLEARED:
75 newAlarmBuilder.clear();
76 newAlarmBuilder.withTimeCleared(Instant.parse(alarmFieldValue).toEpochMilli());
77 break;
78 case TIME_UPDATED:
79 default:
80 newAlarmBuilder.withTimeUpdated(Instant.parse(alarmFieldValue).toEpochMilli());
81 break;
82 }
83 alarmStore.createOrUpdateAlarm(newAlarmBuilder.build());
84 }
85
86 public enum AlarmField {
87 SOURCE,
88 ASSIGNED_USER,
89 ACKNOWLEDGED,
90 MANUALLY_CLEARABLE,
91 SERVICE_AFFECTING,
92 TIME_CLEARED,
93 TIME_UPDATED
94 }
95}