kmcpeake | 4fe18c8 | 2015-11-17 20:07:39 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 |
| 41 | public ObjectNode encode(final Alarm alarm, final CodecContext context) { |
| 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 |
| 63 | public Alarm decode(final ObjectNode json, final CodecContext context) { |
| 64 | if (json == null || !json.isObject()) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | log.debug("id={}, full json={} ", json.get("id"), json); |
| 69 | final Long id = json.get("id").asLong(); |
| 70 | |
| 71 | final DeviceId deviceId = DeviceId.deviceId(json.get("deviceId").asText()); |
| 72 | final String description = json.get("description").asText(); |
| 73 | final Long timeRaised = json.get("timeRaised").asLong(); |
| 74 | final Long timeUpdated = json.get("timeUpdated").asLong(); |
| 75 | |
| 76 | final JsonNode jsonTimeCleared = json.get("timeCleared"); |
| 77 | final Long timeCleared = jsonTimeCleared == null || jsonTimeCleared.isNull() ? null : jsonTimeCleared.asLong(); |
| 78 | |
| 79 | final Alarm.SeverityLevel severity |
| 80 | = Alarm.SeverityLevel.valueOf(json.get("severity").asText().toUpperCase()); |
| 81 | |
| 82 | final Boolean serviceAffecting = json.get("serviceAffecting").asBoolean(); |
| 83 | final Boolean acknowledged = json.get("acknowledged").asBoolean(); |
| 84 | final Boolean manuallyClearable = json.get("manuallyClearable").asBoolean(); |
| 85 | |
| 86 | final JsonNode jsonAssignedUser = json.get("assignedUser"); |
| 87 | final String assignedUser |
| 88 | = jsonAssignedUser == null || jsonAssignedUser.isNull() ? null : jsonAssignedUser.asText(); |
| 89 | |
| 90 | return new DefaultAlarm.Builder( |
| 91 | AlarmId.valueOf(id), deviceId, description, severity, timeRaised).forSource(AlarmEntityId.NONE). |
| 92 | withTimeUpdated(timeUpdated). |
| 93 | withTimeCleared(timeCleared). |
| 94 | withServiceAffecting(serviceAffecting). |
| 95 | withAcknowledged(acknowledged). |
| 96 | withManuallyClearable(manuallyClearable). |
| 97 | withAssignedUser(assignedUser). |
| 98 | build(); |
| 99 | |
| 100 | } |
| 101 | } |