blob: c710514ffb3656c5b700c87ef6f7917debeca7fe [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
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 */
16package org.onosproject.codec.impl;
17
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.net.flow.TrafficTreatment;
21import org.onosproject.net.group.GroupBucket;
22
23import com.fasterxml.jackson.databind.node.ObjectNode;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
27/**
28 * Group bucket JSON codec.
29 */
30public class GroupBucketCodec extends JsonCodec<GroupBucket> {
31
32 private static final String TYPE = "type";
33 private static final String TREATMENT = "treatment";
34 private static final String WEIGHT = "weight";
35 private static final String WATCH_PORT = "watchPort";
36 private static final String WATCH_GROUP = "watchGroup";
37 private static final String PACKETS = "packets";
38 private static final String BYTES = "bytes";
39
40 @Override
41 public ObjectNode encode(GroupBucket bucket, CodecContext context) {
42 checkNotNull(bucket, "Driver cannot be null");
43
44 ObjectNode result = context.mapper().createObjectNode()
45 .put(TYPE, bucket.type().toString())
46 .put(WEIGHT, bucket.weight())
47 .put(PACKETS, bucket.packets())
48 .put(BYTES, bucket.bytes());
49
50 if (bucket.watchPort() != null) {
51 result.put(WATCH_PORT, bucket.watchPort().toString());
52 }
53
54 if (bucket.watchGroup() != null) {
55 result.put(WATCH_GROUP, bucket.watchGroup().toString());
56 }
57
58 if (bucket.treatment() != null) {
59 result.set(TREATMENT, context.codec(TrafficTreatment.class).encode(bucket.treatment(), context));
60 }
61
62 return result;
63 }
64}