blob: 6f71d1b2b975dfaaba76c68a93e77594dd7303f3 [file] [log] [blame]
kmcpeake4fe18c82015-11-17 20:07:39 +00001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
kmcpeake4fe18c82015-11-17 20:07:39 +00003 *
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;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
kmcpeake4fe18c82015-11-17 20:07:39 +000020import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080022import org.onosproject.alarm.Alarm;
23import org.onosproject.alarm.AlarmEntityId;
24import org.onosproject.alarm.AlarmId;
25import org.onosproject.alarm.DefaultAlarm;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070026import org.onosproject.net.DeviceId;
kmcpeake4fe18c82015-11-17 20:07:39 +000027import org.slf4j.Logger;
Andrea Campanella65f9eb92017-05-02 11:36:14 -070028
29import static com.google.common.base.Preconditions.checkNotNull;
kmcpeake4fe18c82015-11-17 20:07:39 +000030import static org.slf4j.LoggerFactory.getLogger;
31
32/**
33 * Implementation of encoder for Alarm codec.
34 */
35public final class AlarmCodec extends JsonCodec<Alarm> {
36
37 private final Logger log = getLogger(getClass());
38
39 @Override
kmcpeakea5404812015-12-08 11:52:50 +000040 public ObjectNode encode(Alarm alarm, CodecContext context) {
kmcpeake4fe18c82015-11-17 20:07:39 +000041 checkNotNull(alarm, "Alarm cannot be null");
42
43 return context.mapper().createObjectNode()
Andrea Campanella65f9eb92017-05-02 11:36:14 -070044 .put("id", alarm.id().toString())
kmcpeake4fe18c82015-11-17 20:07:39 +000045 .put("deviceId", alarm.deviceId().toString())
46 .put("description", alarm.description())
47 .put("source",
Andrea Campanella65f9eb92017-05-02 11:36:14 -070048 alarm.source() == null ? null
49 : alarm.source().toString())
kmcpeake4fe18c82015-11-17 20:07:39 +000050 .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
kmcpeakea5404812015-12-08 11:52:50 +000062 public Alarm decode(ObjectNode json, CodecContext context) {
kmcpeake4fe18c82015-11-17 20:07:39 +000063 if (json == null || !json.isObject()) {
64 return null;
65 }
66
67 log.debug("id={}, full json={} ", json.get("id"), json);
Andrea Campanella65f9eb92017-05-02 11:36:14 -070068 String id = json.get("id").asText();
kmcpeake4fe18c82015-11-17 20:07:39 +000069
kmcpeakeb172d5f2015-12-10 11:30:43 +000070 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();
kmcpeake4fe18c82015-11-17 20:07:39 +000074
kmcpeakeb172d5f2015-12-10 11:30:43 +000075 JsonNode jsonTimeCleared = json.get("timeCleared");
76 Long timeCleared = jsonTimeCleared == null || jsonTimeCleared.isNull() ? null : jsonTimeCleared.asLong();
kmcpeake4fe18c82015-11-17 20:07:39 +000077
kmcpeakeb172d5f2015-12-10 11:30:43 +000078 Alarm.SeverityLevel severity = Alarm.SeverityLevel.valueOf(json.get("severity").asText().toUpperCase());
kmcpeake4fe18c82015-11-17 20:07:39 +000079
kmcpeakeb172d5f2015-12-10 11:30:43 +000080 Boolean serviceAffecting = json.get("serviceAffecting").asBoolean();
81 Boolean acknowledged = json.get("acknowledged").asBoolean();
82 Boolean manuallyClearable = json.get("manuallyClearable").asBoolean();
kmcpeake4fe18c82015-11-17 20:07:39 +000083
kmcpeakeb172d5f2015-12-10 11:30:43 +000084 JsonNode jsonAssignedUser = json.get("assignedUser");
85 String assignedUser
kmcpeake4fe18c82015-11-17 20:07:39 +000086 = jsonAssignedUser == null || jsonAssignedUser.isNull() ? null : jsonAssignedUser.asText();
87
Andrea Campanella65f9eb92017-05-02 11:36:14 -070088 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();
kmcpeake4fe18c82015-11-17 20:07:39 +000097
98 }
99}