Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 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 | |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 19 | import org.onosproject.codec.CodecContext; |
| 20 | import org.onosproject.codec.JsonCodec; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 21 | import org.onosproject.core.GroupId; |
| 22 | import org.onosproject.net.PortNumber; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.TrafficTreatment; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 24 | import org.onosproject.net.group.DefaultGroupBucket; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 25 | import org.onosproject.net.group.GroupBucket; |
| 26 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 28 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Group bucket JSON codec. |
| 32 | */ |
| 33 | public 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 Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 42 | private static final String BUCKET_ID = "bucketId"; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 43 | private static final String MISSING_MEMBER_MESSAGE = |
| 44 | " member is required in Group"; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 45 | |
| 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 Milkey | 957bb37 | 2018-04-03 15:39:46 -0700 | [diff] [blame] | 54 | .put(BYTES, bucket.bytes()) |
| 55 | .put(BUCKET_ID, bucket.hashCode()); |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 56 | |
| 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 Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 71 | |
| 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 Tseng | fa394de | 2017-02-01 11:26:40 -0800 | [diff] [blame] | 115 | GroupId watchGroup = new GroupId((short) groupIdInt); |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 116 | |
| 117 | groupBucket = |
| 118 | DefaultGroupBucket.createFailoverGroupBucket(trafficTreatment, watchPort, watchGroup); |
| 119 | break; |
| 120 | default: |
| 121 | DefaultGroupBucket.createAllGroupBucket(trafficTreatment); |
| 122 | } |
| 123 | |
| 124 | return groupBucket; |
| 125 | } |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 126 | } |