blob: 279e6b23db70f69d45bc91e0d06b5c2c03b6b7c8 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
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 */
16package org.onosproject.faultmanagement.web;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21
22import com.fasterxml.jackson.databind.node.ObjectNode;
23
24import static com.google.common.base.Preconditions.checkNotNull;
25import org.onosproject.net.DeviceId;
26import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
27import org.onosproject.incubator.net.faultmanagement.alarm.AlarmEntityId;
28import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
29import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
30import org.slf4j.Logger;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Implementation of encoder for Alarm codec.
35 */
36public final class AlarmCodec extends JsonCodec<Alarm> {
37
38 private final Logger log = getLogger(getClass());
39
40 @Override
kmcpeakea5404812015-12-08 11:52:50 +000041 public ObjectNode encode(Alarm alarm, CodecContext context) {
kmcpeake4fe18c82015-11-17 20:07:39 +000042 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
kmcpeakea5404812015-12-08 11:52:50 +000063 public Alarm decode(ObjectNode json, CodecContext context) {
kmcpeake4fe18c82015-11-17 20:07:39 +000064 if (json == null || !json.isObject()) {
65 return null;
66 }
67
68 log.debug("id={}, full json={} ", json.get("id"), json);
kmcpeakeb172d5f2015-12-10 11:30:43 +000069 Long id = json.get("id").asLong();
kmcpeake4fe18c82015-11-17 20:07:39 +000070
kmcpeakeb172d5f2015-12-10 11:30:43 +000071 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();
kmcpeake4fe18c82015-11-17 20:07:39 +000075
kmcpeakeb172d5f2015-12-10 11:30:43 +000076 JsonNode jsonTimeCleared = json.get("timeCleared");
77 Long timeCleared = jsonTimeCleared == null || jsonTimeCleared.isNull() ? null : jsonTimeCleared.asLong();
kmcpeake4fe18c82015-11-17 20:07:39 +000078
kmcpeakeb172d5f2015-12-10 11:30:43 +000079 Alarm.SeverityLevel severity = Alarm.SeverityLevel.valueOf(json.get("severity").asText().toUpperCase());
kmcpeake4fe18c82015-11-17 20:07:39 +000080
kmcpeakeb172d5f2015-12-10 11:30:43 +000081 Boolean serviceAffecting = json.get("serviceAffecting").asBoolean();
82 Boolean acknowledged = json.get("acknowledged").asBoolean();
83 Boolean manuallyClearable = json.get("manuallyClearable").asBoolean();
kmcpeake4fe18c82015-11-17 20:07:39 +000084
kmcpeakeb172d5f2015-12-10 11:30:43 +000085 JsonNode jsonAssignedUser = json.get("assignedUser");
86 String assignedUser
kmcpeake4fe18c82015-11-17 20:07:39 +000087 = jsonAssignedUser == null || jsonAssignedUser.isNull() ? null : jsonAssignedUser.asText();
88
89 return new DefaultAlarm.Builder(
kmcpeakeb172d5f2015-12-10 11:30:43 +000090 deviceId, description, severity, timeRaised).forSource(AlarmEntityId.NONE).
91 withId(AlarmId.alarmId(id)).
kmcpeake4fe18c82015-11-17 20:07:39 +000092 withTimeUpdated(timeUpdated).
93 withTimeCleared(timeCleared).
94 withServiceAffecting(serviceAffecting).
95 withAcknowledged(acknowledged).
96 withManuallyClearable(manuallyClearable).
97 withAssignedUser(assignedUser).
98 build();
99
100 }
101}