blob: 9170820aaabd9ac7c63fb29b6ddceabf7758a99b [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";
Ray Milkey957bb372018-04-03 15:39:46 -070042 private static final String BUCKET_ID = "bucketId";
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())
Ray Milkey957bb372018-04-03 15:39:46 -070054 .put(BYTES, bucket.bytes())
55 .put(BUCKET_ID, bucket.hashCode());
Ray Milkey39616f32015-05-14 15:43:00 -070056
57 if (bucket.watchPort() != null) {
58 result.put(WATCH_PORT, bucket.watchPort().toString());
59 }
60
61 if (bucket.watchGroup() != null) {
62 result.put(WATCH_GROUP, bucket.watchGroup().toString());
63 }
64
65 if (bucket.treatment() != null) {
66 result.set(TREATMENT, context.codec(TrafficTreatment.class).encode(bucket.treatment(), context));
67 }
68
69 return result;
70 }
Jian Liecb3c0f2015-12-15 10:07:49 -080071
72 @Override
73 public GroupBucket decode(ObjectNode json, CodecContext context) {
74 if (json == null || !json.isObject()) {
75 return null;
76 }
77
78 // build traffic treatment
79 ObjectNode treatmentJson = get(json, TREATMENT);
80 TrafficTreatment trafficTreatment = null;
81 if (treatmentJson != null) {
82 JsonCodec<TrafficTreatment> treatmentCodec =
83 context.codec(TrafficTreatment.class);
84 trafficTreatment = treatmentCodec.decode(treatmentJson, context);
85 }
86
87 // parse group type
88 String type = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
89 GroupBucket groupBucket = null;
90
91 switch (type) {
92 case "SELECT":
93 // parse weight
94 int weightInt = nullIsIllegal(json.get(WEIGHT), WEIGHT + MISSING_MEMBER_MESSAGE).asInt();
95
96 groupBucket =
97 DefaultGroupBucket.createSelectGroupBucket(trafficTreatment, (short) weightInt);
98 break;
99 case "INDIRECT":
100 groupBucket =
101 DefaultGroupBucket.createIndirectGroupBucket(trafficTreatment);
102 break;
103 case "ALL":
104 groupBucket =
105 DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
106 break;
107 case "FAILOVER":
108 // parse watchPort
109 PortNumber watchPort = PortNumber.portNumber(nullIsIllegal(json.get(WATCH_PORT),
110 WATCH_PORT + MISSING_MEMBER_MESSAGE).asText());
111
112 // parse watchGroup
113 int groupIdInt = nullIsIllegal(json.get(WATCH_GROUP),
114 WATCH_GROUP + MISSING_MEMBER_MESSAGE).asInt();
Yi Tsengfa394de2017-02-01 11:26:40 -0800115 GroupId watchGroup = new GroupId((short) groupIdInt);
Jian Liecb3c0f2015-12-15 10:07:49 -0800116
117 groupBucket =
118 DefaultGroupBucket.createFailoverGroupBucket(trafficTreatment, watchPort, watchGroup);
119 break;
120 default:
121 DefaultGroupBucket.createAllGroupBucket(trafficTreatment);
122 }
123
124 return groupBucket;
125 }
Ray Milkey39616f32015-05-14 15:43:00 -0700126}