blob: 2876ca1826368fbd187cda57477355d89646896f [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;
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070023import org.onosproject.kafkaintegration.api.dto.DefaultEventSubscriber;
Shravan Ambati7d199542016-04-22 16:09:05 -070024import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
25import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
26import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
Shravan Ambati7d199542016-04-22 16:09:05 -070027import 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) {
Shravan Ambati7d199542016-04-22 16:09:05 -070050
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070051 EventSubscriber.Builder resultBuilder = new DefaultEventSubscriber
52 .Builder();
53 String appName = json.get(NAME).asText();
54 resultBuilder.setAppName(appName);
55
56 String subscriberGroupId = json.get(GROUP_ID).asText();
57 resultBuilder.setSubscriberGroupId(new EventSubscriberGroupId(UUID.
58 fromString(subscriberGroupId)));
59
60 String eventType = json.get(EVENT_TYPE).asText();
61 resultBuilder.setEventType(Type.valueOf(eventType));
62 return resultBuilder.build();
Shravan Ambati7d199542016-04-22 16:09:05 -070063 }
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070064}