blob: c3819b3f3764ca52f6f3bd05813258e84bd421cd [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
Jian Liecb3c0f2015-12-15 10:07:49 -080018import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey39616f32015-05-14 15:43:00 -070019import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
Jian Liecb3c0f2015-12-15 10:07:49 -080021import org.onosproject.core.DefaultGroupId;
22import org.onosproject.core.GroupId;
23import org.onosproject.net.PortNumber;
Ray Milkey39616f32015-05-14 15:43:00 -070024import org.onosproject.net.flow.TrafficTreatment;
Jian Liecb3c0f2015-12-15 10:07:49 -080025import org.onosproject.net.group.DefaultGroupBucket;
Ray Milkey39616f32015-05-14 15:43:00 -070026import org.onosproject.net.group.GroupBucket;
27
Ray Milkey39616f32015-05-14 15:43:00 -070028import static com.google.common.base.Preconditions.checkNotNull;
Jian Liecb3c0f2015-12-15 10:07:49 -080029import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey39616f32015-05-14 15:43:00 -070030
31/**
32 * Group bucket JSON codec.
33 */
34public class GroupBucketCodec extends JsonCodec<GroupBucket> {
35
36 private static final String TYPE = "type";
37 private static final String TREATMENT = "treatment";
38 private static final String WEIGHT = "weight";
39 private static final String WATCH_PORT = "watchPort";
40 private static final String WATCH_GROUP = "watchGroup";
41 private static final String PACKETS = "packets";
42 private static final String BYTES = "bytes";
Jian Liecb3c0f2015-12-15 10:07:49 -080043 private static final String MISSING_MEMBER_MESSAGE =
44 " member is required in Group";
Ray Milkey39616f32015-05-14 15:43:00 -070045
46 @Override
47 public ObjectNode encode(GroupBucket bucket, CodecContext context) {
48 checkNotNull(bucket, "Driver cannot be null");
49
50 ObjectNode result = context.mapper().createObjectNode()
51 .put(TYPE, bucket.type().toString())
52 .put(WEIGHT, bucket.weight())
53 .put(PACKETS, bucket.packets())
54 .put(BYTES, bucket.bytes());
55
56 if (bucket.watchPort() != null) {
57 result.put(WATCH_PORT, bucket.watchPort().toString());
58 }
59
60 if (bucket.watchGroup() != null) {
61 result.put(WATCH_GROUP, bucket.watchGroup().toString());
62 }
63
64 if (bucket.treatment() != null) {
65 result.set(TREATMENT, context.codec(TrafficTreatment.class).encode(bucket.treatment(), context));
66 }
67
68 return result;
69 }
Jian Liecb3c0f2015-12-15 10:07:49 -080070
71 @Override
72 public GroupBucket decode(ObjectNode json, CodecContext context) {
73 if (json == null || !json.isObject()) {
74 return null;
75 }
76
77 // build traffic treatment
78 ObjectNode treatmentJson = get(json, TREATMENT);
79 TrafficTreatment trafficTreatment = null;
80 if (treatmentJson != null) {
81 JsonCodec<TrafficTreatment> treatmentCodec =
82 context.codec(TrafficTreatment.class);
83 trafficTreatment = treatmentCodec.decode(treatmentJson, context);
84 }
85
86 // parse group type
87 String type = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
88 GroupBucket groupBucket = null;
89
90 switch (type) {
91 case "SELECT":
92 // parse weight
93 int weightInt = nullIsIllegal(json.get(WEIGHT), WEIGHT + MISSING_MEMBER_MESSAGE).asInt();
94
95 groupBucket =
96 DefaultGroupBucket.createSelectGroupBucket(trafficTreatment, (short) weightInt);
97 break;
98 case "INDIRECT":
99 groupBucket =
100 DefaultGroupBucket.createIndirectGroupBucket(trafficTreatment);
101 break;
102 case "ALL":
103 groupBucket =
104 DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
105 break;
106 case "FAILOVER":
107 // parse watchPort
108 PortNumber watchPort = PortNumber.portNumber(nullIsIllegal(json.get(WATCH_PORT),
109 WATCH_PORT + MISSING_MEMBER_MESSAGE).asText());
110
111 // parse watchGroup
112 int groupIdInt = nullIsIllegal(json.get(WATCH_GROUP),
113 WATCH_GROUP + MISSING_MEMBER_MESSAGE).asInt();
114 GroupId watchGroup = new DefaultGroupId((short) groupIdInt);
115
116 groupBucket =
117 DefaultGroupBucket.createFailoverGroupBucket(trafficTreatment, watchPort, watchGroup);
118 break;
119 default:
120 DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
121 }
122
123 return groupBucket;
124 }
Ray Milkey39616f32015-05-14 15:43:00 -0700125}