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; |
| 24 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId; |
| 26 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 27 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 28 | |
| 29 | import java.time.Instant; |
| 30 | |
| 31 | /** |
| 32 | * Updates an existing alarm. |
| 33 | */ |
| 34 | @Command(scope = "onos", name = "alarm-update", |
| 35 | description = "Updates an alarm") |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 36 | @Service |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 37 | public 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 Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 54 | protected void doExecute() { |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 55 | 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 | } |