Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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.DefaultGroupId; |
| 22 | import org.onosproject.core.GroupId; |
| 23 | import org.onosproject.net.PortNumber; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 24 | import org.onosproject.net.flow.TrafficTreatment; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 25 | import org.onosproject.net.group.DefaultGroupBucket; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 26 | import org.onosproject.net.group.GroupBucket; |
| 27 | |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 29 | import static org.onlab.util.Tools.nullIsIllegal; |
Ray Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Group bucket JSON codec. |
| 33 | */ |
| 34 | public 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 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()) |
| 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 Li | ecb3c0f | 2015-12-15 10:07:49 -0800 | [diff] [blame] | 70 | |
| 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 Milkey | 39616f3 | 2015-05-14 15:43:00 -0700 | [diff] [blame] | 125 | } |