Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 3 | * |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 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 |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 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 | */ |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 16 | package org.onosproject.kafkaintegration.impl; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 17 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 21 | import org.onosproject.kafkaintegration.api.dto.DefaultEventSubscriber; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 22 | import org.onosproject.kafkaintegration.api.dto.EventSubscriber; |
| 23 | import org.onosproject.kafkaintegration.api.dto.EventSubscriberGroupId; |
| 24 | import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type; |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.UUID; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Shravan Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Codec for encoding/decoding a Subscriber object to/from JSON. |
| 32 | * |
| 33 | */ |
| 34 | public 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 Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 51 | |
Sanjana Agarwal | eb9f0c5 | 2016-06-07 11:10:34 -0700 | [diff] [blame] | 52 | 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 Ambati | 7d19954 | 2016-04-22 16:09:05 -0700 | [diff] [blame] | 64 | } |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 65 | } |