Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [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 | 4f5de00 | 2014-12-17 19:26:11 -0800 | [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 | |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Jonathan Hart | 834c8d7 | 2016-03-17 07:40:21 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 20 | import org.onosproject.codec.CodecContext; |
| 21 | import org.onosproject.codec.JsonCodec; |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 22 | import org.onosproject.net.flow.DefaultFlowEntry; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 23 | import org.onosproject.net.flow.FlowEntry; |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.FlowRule; |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * Flow entry JSON codec. |
| 32 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 33 | public final class FlowEntryCodec extends JsonCodec<FlowEntry> { |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 34 | |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 35 | public static final String GROUP_ID = "groupId"; |
| 36 | public static final String STATE = "state"; |
| 37 | public static final String LIFE = "life"; |
| 38 | public static final String LIVE_TYPE = "liveType"; |
| 39 | public static final String LAST_SEEN = "lastSeen"; |
| 40 | public static final String PACKETS = "packets"; |
| 41 | public static final String BYTES = "bytes"; |
| 42 | public static final String TREATMENT = "treatment"; |
| 43 | public static final String SELECTOR = "selector"; |
| 44 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 45 | @Override |
| 46 | public ObjectNode encode(FlowEntry flowEntry, CodecContext context) { |
| 47 | checkNotNull(flowEntry, "Flow entry cannot be null"); |
| 48 | |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 49 | ObjectNode result = context.mapper().createObjectNode() |
| 50 | .put(GROUP_ID, flowEntry.groupId().id()) |
| 51 | .put(STATE, flowEntry.state().toString()) |
| 52 | //FIXME life is destroying precision (seconds granularity is default) |
| 53 | .put(LIFE, flowEntry.life()) |
| 54 | .put(LIVE_TYPE, flowEntry.liveType().toString()) |
| 55 | .put(LAST_SEEN, flowEntry.lastSeen()) |
| 56 | .put(PACKETS, flowEntry.packets()) |
| 57 | .put(BYTES, flowEntry.bytes()) |
| 58 | // encode FlowRule-specific fields using the FlowRule codec |
| 59 | .setAll(context.codec(FlowRule.class).encode((FlowRule) flowEntry, context)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 60 | |
| 61 | if (flowEntry.treatment() != null) { |
| 62 | final JsonCodec<TrafficTreatment> treatmentCodec = |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 63 | context.codec(TrafficTreatment.class); |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 64 | result.set(TREATMENT, treatmentCodec.encode(flowEntry.treatment(), context)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (flowEntry.selector() != null) { |
| 68 | final JsonCodec<TrafficSelector> selectorCodec = |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 69 | context.codec(TrafficSelector.class); |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 70 | result.set(SELECTOR, selectorCodec.encode(flowEntry.selector(), context)); |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | return result; |
| 74 | } |
| 75 | |
Seyeon Jeong | 8188da1 | 2020-03-03 12:49:48 -0800 | [diff] [blame] | 76 | @Override |
| 77 | public FlowEntry decode(ObjectNode json, CodecContext context) { |
| 78 | checkNotNull(json, "JSON object cannot be null"); |
| 79 | |
| 80 | // decode FlowRule-specific fields using the FlowRule codec |
| 81 | FlowRule flowRule = context.codec(FlowRule.class).decode(json, context); |
| 82 | |
| 83 | JsonNode stateNode = json.get(STATE); |
| 84 | FlowEntry.FlowEntryState state = (null == stateNode) ? |
| 85 | FlowEntry.FlowEntryState.ADDED : FlowEntry.FlowEntryState.valueOf(stateNode.asText()); |
| 86 | |
| 87 | JsonNode lifeNode = json.get(LIFE); |
| 88 | long life = (null == lifeNode) ? 0 : lifeNode.asLong(); |
| 89 | |
| 90 | JsonNode liveTypeNode = json.get(LIVE_TYPE); |
| 91 | FlowEntry.FlowLiveType liveType = (null == liveTypeNode) ? |
| 92 | FlowEntry.FlowLiveType.UNKNOWN : FlowEntry.FlowLiveType.valueOf(liveTypeNode.asText()); |
| 93 | |
| 94 | JsonNode packetsNode = json.get(PACKETS); |
| 95 | long packets = (null == packetsNode) ? 0 : packetsNode.asLong(); |
| 96 | |
| 97 | JsonNode bytesNode = json.get(BYTES); |
| 98 | long bytes = (null == bytesNode) ? 0 : bytesNode.asLong(); |
| 99 | |
| 100 | return new DefaultFlowEntry(flowRule, state, life, |
| 101 | liveType, packets, bytes); |
| 102 | } |
| 103 | |
Ray Milkey | 4f5de00 | 2014-12-17 19:26:11 -0800 | [diff] [blame] | 104 | } |
| 105 | |