kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 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 | package org.onosproject.faultmanagement.web; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 22 | import org.onosproject.alarm.Alarm; |
| 23 | import org.onosproject.alarm.AlarmEntityId; |
| 24 | import org.onosproject.alarm.AlarmId; |
| 25 | import org.onosproject.alarm.DefaultAlarm; |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 27 | import org.slf4j.Logger; |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 30 | import static org.slf4j.LoggerFactory.getLogger; |
| 31 | |
| 32 | /** |
| 33 | * Implementation of encoder for Alarm codec. |
| 34 | */ |
| 35 | public final class AlarmCodec extends JsonCodec<Alarm> { |
| 36 | |
| 37 | private final Logger log = getLogger(getClass()); |
| 38 | |
| 39 | @Override |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 40 | public ObjectNode encode(Alarm alarm, CodecContext context) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 41 | checkNotNull(alarm, "Alarm cannot be null"); |
| 42 | |
| 43 | return context.mapper().createObjectNode() |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 44 | .put("id", alarm.id().toString()) |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 45 | .put("deviceId", alarm.deviceId().toString()) |
| 46 | .put("description", alarm.description()) |
| 47 | .put("source", |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 48 | alarm.source() == null ? null |
| 49 | : alarm.source().toString()) |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 50 | .put("timeRaised", alarm.timeRaised()) |
| 51 | .put("timeUpdated", alarm.timeUpdated()) |
| 52 | .put("timeCleared", alarm.timeCleared()) |
| 53 | .put("severity", alarm.severity().toString()) |
| 54 | .put("serviceAffecting", alarm.serviceAffecting()) |
| 55 | .put("acknowledged", alarm.acknowledged()) |
| 56 | .put("manuallyClearable", alarm.manuallyClearable()) |
| 57 | .put("assignedUser", alarm.assignedUser()); |
| 58 | |
| 59 | } |
| 60 | |
| 61 | @Override |
kmcpeake | a540481 | 2015-12-08 11:52:50 +0000 | [diff] [blame] | 62 | public Alarm decode(ObjectNode json, CodecContext context) { |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 63 | if (json == null || !json.isObject()) { |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | log.debug("id={}, full json={} ", json.get("id"), json); |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 68 | String id = json.get("id").asText(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 69 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 70 | DeviceId deviceId = DeviceId.deviceId(json.get("deviceId").asText()); |
| 71 | String description = json.get("description").asText(); |
| 72 | Long timeRaised = json.get("timeRaised").asLong(); |
| 73 | Long timeUpdated = json.get("timeUpdated").asLong(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 74 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 75 | JsonNode jsonTimeCleared = json.get("timeCleared"); |
| 76 | Long timeCleared = jsonTimeCleared == null || jsonTimeCleared.isNull() ? null : jsonTimeCleared.asLong(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 77 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 78 | Alarm.SeverityLevel severity = Alarm.SeverityLevel.valueOf(json.get("severity").asText().toUpperCase()); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 79 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 80 | Boolean serviceAffecting = json.get("serviceAffecting").asBoolean(); |
| 81 | Boolean acknowledged = json.get("acknowledged").asBoolean(); |
| 82 | Boolean manuallyClearable = json.get("manuallyClearable").asBoolean(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 83 | |
kmcpeake | b172d5f | 2015-12-10 11:30:43 +0000 | [diff] [blame] | 84 | JsonNode jsonAssignedUser = json.get("assignedUser"); |
| 85 | String assignedUser |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 86 | = jsonAssignedUser == null || jsonAssignedUser.isNull() ? null : jsonAssignedUser.asText(); |
| 87 | |
Andrea Campanella | 65f9eb9 | 2017-05-02 11:36:14 -0700 | [diff] [blame] | 88 | return new DefaultAlarm.Builder(AlarmId.alarmId(deviceId, id), |
| 89 | deviceId, description, severity, timeRaised).forSource(AlarmEntityId.NONE) |
| 90 | .withTimeUpdated(timeUpdated) |
| 91 | .withTimeCleared(timeCleared) |
| 92 | .withServiceAffecting(serviceAffecting) |
| 93 | .withAcknowledged(acknowledged) |
| 94 | .withManuallyClearable(manuallyClearable) |
| 95 | .withAssignedUser(assignedUser) |
| 96 | .build(); |
kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame] | 97 | |
| 98 | } |
| 99 | } |