blob: a9bf5d8a50105f69c359ba4b8d0a7bc833bc9bb1 [file] [log] [blame]
Ray Milkey39616f32015-05-14 15:43:00 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey39616f32015-05-14 15:43:00 -07003 *
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.GroupId;
22import org.onosproject.net.PortNumber;
Ray Milkey39616f32015-05-14 15:43:00 -070023import org.onosproject.net.flow.TrafficTreatment;
Jian Liecb3c0f2015-12-15 10:07:49 -080024import org.onosproject.net.group.DefaultGroupBucket;
Ray Milkey39616f32015-05-14 15:43:00 -070025import org.onosproject.net.group.GroupBucket;
26
Ray Milkey39616f32015-05-14 15:43:00 -070027import static com.google.common.base.Preconditions.checkNotNull;
Jian Liecb3c0f2015-12-15 10:07:49 -080028import static org.onlab.util.Tools.nullIsIllegal;
Ray Milkey39616f32015-05-14 15:43:00 -070029
30/**
31 * Group bucket JSON codec.
32 */
33public class GroupBucketCodec extends JsonCodec<GroupBucket> {
34
35 private static final String TYPE = "type";
36 private static final String TREATMENT = "treatment";
37 private static final String WEIGHT = "weight";
38 private static final String WATCH_PORT = "watchPort";
39 private static final String WATCH_GROUP = "watchGroup";
40 private static final String PACKETS = "packets";
41 private static final String BYTES = "bytes";
Jian Liecb3c0f2015-12-15 10:07:49 -080042 private static final String MISSING_MEMBER_MESSAGE =
43 " member is required in Group";
Ray Milkey39616f32015-05-14 15:43:00 -070044
45 @Override
46 public ObjectNode encode(GroupBucket bucket, CodecContext context) {
47 checkNotNull(bucket, "Driver cannot be null");
48
49 ObjectNode result = context.mapper().createObjectNode()
50 .put(TYPE, bucket.type().toString())
51 .put(WEIGHT, bucket.weight())
52 .put(PACKETS, bucket.packets())
53 .put(BYTES, bucket.bytes());
54
55 if (bucket.watchPort() != null) {
56 result.put(WATCH_PORT, bucket.watchPort().toString());
57 }
58
59 if (bucket.watchGroup() != null) {
60 result.put(WATCH_GROUP, bucket.watchGroup().toString());
61 }
62
63 if (bucket.treatment() != null) {
64 result.set(TREATMENT, context.codec(TrafficTreatment.class).encode(bucket.treatment(), context));
65 }
66
67 return result;
68 }
Jian Liecb3c0f2015-12-15 10:07:49 -080069
70 @Override
71 public GroupBucket decode(ObjectNode json, CodecContext context) {
72 if (json == null || !json.isObject()) {
73 return null;
74 }
75
76 // build traffic treatment
77 ObjectNode treatmentJson = get(json, TREATMENT);
78 TrafficTreatment trafficTreatment = null;
79 if (treatmentJson != null) {
80 JsonCodec<TrafficTreatment> treatmentCodec =
81 context.codec(TrafficTreatment.class);
82 trafficTreatment = treatmentCodec.decode(treatmentJson, context);
83 }
84
85 // parse group type
86 String type = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
87 GroupBucket groupBucket = null;
88
89 switch (type) {
90 case "SELECT":
91 // parse weight
92 int weightInt = nullIsIllegal(json.get(WEIGHT), WEIGHT + MISSING_MEMBER_MESSAGE).asInt();
93
94 groupBucket =
95 DefaultGroupBucket.createSelectGroupBucket(trafficTreatment, (short) weightInt);
96 break;
97 case "INDIRECT":
98 groupBucket =
99 DefaultGroupBucket.createIndirectGroupBucket(trafficTreatment);
100 break;
101 case "ALL":
102 groupBucket =
103 DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
104 break;
105 case "FAILOVER":
106 // parse watchPort
107 PortNumber watchPort = PortNumber.portNumber(nullIsIllegal(json.get(WATCH_PORT),
108 WATCH_PORT + MISSING_MEMBER_MESSAGE).asText());
109
110 // parse watchGroup
111 int groupIdInt = nullIsIllegal(json.get(WATCH_GROUP),
112 WATCH_GROUP + MISSING_MEMBER_MESSAGE).asInt();
Yi Tsengfa394de2017-02-01 11:26:40 -0800113 GroupId watchGroup = new GroupId((short) groupIdInt);
Jian Liecb3c0f2015-12-15 10:07:49 -0800114
115 groupBucket =
116 DefaultGroupBucket.createFailoverGroupBucket(trafficTreatment, watchPort, watchGroup);
117 break;
118 default:
119 DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
120 }
121
122 return groupBucket;
123 }
Ray Milkey39616f32015-05-14 15:43:00 -0700124}