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