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