blob: 305b5680c07d41808f817a072235b824ed85bc67 [file] [log] [blame]
Ray Milkey2d572dd2017-04-14 10:01:24 -07001/*
Shravan Ambati7d199542016-04-22 16:09:05 -07002 * Copyright 2016-present Open Networking Laboratory
Ray Milkey2d572dd2017-04-14 10:01:24 -07003 *
Shravan Ambati7d199542016-04-22 16:09:05 -07004 * 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
Ray Milkey2d572dd2017-04-14 10:01:24 -07007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Shravan Ambati7d199542016-04-22 16:09:05 -070010 * 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 */
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070016package org.onosproject.kafkaintegration.impl;
Shravan Ambati7d199542016-04-22 16:09:05 -070017
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Shravan Ambati7d199542016-04-22 16:09:05 -070019import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070021import org.onosproject.kafkaintegration.api.dto.DefaultEventSubscriber;
Shravan Ambati7d199542016-04-22 16:09:05 -070022import org.onosproject.kafkaintegration.api.dto.EventSubscriber;
23import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId;
24import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070025
26import java.util.UUID;
27
28import static com.google.common.base.Preconditions.checkNotNull;
Shravan Ambati7d199542016-04-22 16:09:05 -070029
30/**
31 * Codec for encoding/decoding a Subscriber object to/from JSON.
32 *
33 */
34public final class SubscriberCodec extends JsonCodec<EventSubscriber> {
35
36 // JSON field names
37 private static final String NAME = "appName";
38 private static final String GROUP_ID = "groupId";
39 private static final String EVENT_TYPE = "eventType";
40
41 @Override
42 public ObjectNode encode(EventSubscriber data, CodecContext context) {
43 checkNotNull(data, "Subscriber cannot be null");
44 return context.mapper().createObjectNode().put(NAME, data.appName())
45 .put(GROUP_ID, data.subscriberGroupId().getId().toString())
46 .put(EVENT_TYPE, data.eventType().toString());
47 }
48
49 @Override
50 public EventSubscriber decode(ObjectNode json, CodecContext context) {
Shravan Ambati7d199542016-04-22 16:09:05 -070051
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -070052 EventSubscriber.Builder resultBuilder = new DefaultEventSubscriber
53 .Builder();
54 String appName = json.get(NAME).asText();
55 resultBuilder.setAppName(appName);
56
57 String subscriberGroupId = json.get(GROUP_ID).asText();
58 resultBuilder.setSubscriberGroupId(new EventSubscriberGroupId(UUID.
59 fromString(subscriberGroupId)));
60
61 String eventType = json.get(EVENT_TYPE).asText();
62 resultBuilder.setEventType(Type.valueOf(eventType));
63 return resultBuilder.build();
Shravan Ambati7d199542016-04-22 16:09:05 -070064 }
Ray Milkey2d572dd2017-04-14 10:01:24 -070065}