blob: f7a02b42ebc546fca26f682e1eaaf6c887847444 [file] [log] [blame]
Shravan Ambati7d199542016-04-22 16:09:05 -07001/**
2 * Copyright 2016-present Open Networking Laboratory
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6
7 * http://www.apache.org/licenses/LICENSE-2.0
8
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package org.onosproject.kafkaintegration.rest;
16
17import static com.google.common.base.Preconditions.checkNotNull;
18
19import java.util.UUID;
20
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
23import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
24import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
25import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
26
27import com.fasterxml.jackson.databind.node.ObjectNode;
28
29/**
30 * Codec for encoding/decoding a Subscriber object to/from JSON.
31 *
32 */
33public final class SubscriberCodec extends JsonCodec<EventSubscriber> {
34
35 // JSON field names
36 private static final String NAME = "appName";
37 private static final String GROUP_ID = "groupId";
38 private static final String EVENT_TYPE = "eventType";
39
40 @Override
41 public ObjectNode encode(EventSubscriber data, CodecContext context) {
42 checkNotNull(data, "Subscriber cannot be null");
43 return context.mapper().createObjectNode().put(NAME, data.appName())
44 .put(GROUP_ID, data.subscriberGroupId().getId().toString())
45 .put(EVENT_TYPE, data.eventType().toString());
46 }
47
48 @Override
49 public EventSubscriber decode(ObjectNode json, CodecContext context) {
50 String name = json.path(NAME).asText();
51 String groupId = json.path(GROUP_ID).asText();
52 EventSubscriberGroupId subscriberGroupId = new EventSubscriberGroupId(UUID
53 .fromString(groupId));
54 String eventType = json.path(EVENT_TYPE).asText();
55
56 return new EventSubscriber(name, subscriberGroupId,
57 Type.valueOf(eventType));
58 }
59}