Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
| 18 | import org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
| 20 | import org.onosproject.net.group.Group; |
| 21 | import org.onosproject.net.group.GroupBucket; |
| 22 | |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 27 | |
| 28 | /** |
| 29 | * Group JSON codec. |
| 30 | */ |
| 31 | public final class GroupCodec extends JsonCodec<Group> { |
| 32 | // JSON field names |
| 33 | private static final String ID = "id"; |
| 34 | private static final String STATE = "state"; |
| 35 | private static final String LIFE = "life"; |
| 36 | private static final String PACKETS = "packets"; |
| 37 | private static final String BYTES = "bytes"; |
| 38 | private static final String REFERENCE_COUNT = "referenceCount"; |
| 39 | private static final String TYPE = "type"; |
| 40 | private static final String DEVICE_ID = "deviceId"; |
| 41 | private static final String APP_ID = "appId"; |
| 42 | private static final String APP_COOKIE = "appCookie"; |
| 43 | private static final String GIVEN_GROUP_ID = "givenGroupId"; |
| 44 | private static final String BUCKETS = "buckets"; |
| 45 | |
| 46 | @Override |
| 47 | public ObjectNode encode(Group group, CodecContext context) { |
| 48 | checkNotNull(group, "Group cannot be null"); |
| 49 | ObjectNode result = context.mapper().createObjectNode() |
| 50 | .put(ID, group.id().toString()) |
| 51 | .put(STATE, group.state().toString()) |
| 52 | .put(LIFE, group.life()) |
| 53 | .put(PACKETS, group.packets()) |
| 54 | .put(BYTES, group.bytes()) |
| 55 | .put(REFERENCE_COUNT, group.referenceCount()) |
| 56 | .put(TYPE, group.type().toString()) |
| 57 | .put(DEVICE_ID, group.deviceId().toString()); |
| 58 | |
| 59 | if (group.appId() != null) { |
| 60 | result.put(APP_ID, group.appId().toString()); |
| 61 | } |
| 62 | |
| 63 | if (group.appCookie() != null) { |
| 64 | result.put(APP_COOKIE, group.appCookie().toString()); |
| 65 | } |
| 66 | |
| 67 | if (group.givenGroupId() != null) { |
| 68 | result.put(GIVEN_GROUP_ID, group.givenGroupId()); |
| 69 | } |
| 70 | |
| 71 | ArrayNode buckets = context.mapper().createArrayNode(); |
| 72 | group.buckets().buckets().forEach(bucket -> { |
| 73 | ObjectNode bucketJson = context.codec(GroupBucket.class).encode(bucket, context); |
| 74 | buckets.add(bucketJson); |
| 75 | }); |
| 76 | result.set(BUCKETS, buckets); |
| 77 | return result; |
| 78 | } |
| 79 | } |