blob: 0ffeeb28986831b99c08dfec92c62c3c2933d5e3 [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey4f5de002014-12-17 19:26:11 -08003 *
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
Seyeon Jeong8188da12020-03-03 12:49:48 -080018import com.fasterxml.jackson.databind.JsonNode;
Jonathan Hart834c8d72016-03-17 07:40:21 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey4f5de002014-12-17 19:26:11 -080020import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
Seyeon Jeong8188da12020-03-03 12:49:48 -080022import org.onosproject.net.flow.DefaultFlowEntry;
Ray Milkey4f5de002014-12-17 19:26:11 -080023import org.onosproject.net.flow.FlowEntry;
Seyeon Jeong8188da12020-03-03 12:49:48 -080024import org.onosproject.net.flow.FlowRule;
Ray Milkey4f5de002014-12-17 19:26:11 -080025import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
27
Ray Milkey4f5de002014-12-17 19:26:11 -080028import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Flow entry JSON codec.
32 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080033public final class FlowEntryCodec extends JsonCodec<FlowEntry> {
Ray Milkey4f5de002014-12-17 19:26:11 -080034
Seyeon Jeong8188da12020-03-03 12:49:48 -080035 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 Milkey4f5de002014-12-17 19:26:11 -080045 @Override
46 public ObjectNode encode(FlowEntry flowEntry, CodecContext context) {
47 checkNotNull(flowEntry, "Flow entry cannot be null");
48
Seyeon Jeong8188da12020-03-03 12:49:48 -080049 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 Milkey4f5de002014-12-17 19:26:11 -080060
61 if (flowEntry.treatment() != null) {
62 final JsonCodec<TrafficTreatment> treatmentCodec =
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080063 context.codec(TrafficTreatment.class);
Seyeon Jeong8188da12020-03-03 12:49:48 -080064 result.set(TREATMENT, treatmentCodec.encode(flowEntry.treatment(), context));
Ray Milkey4f5de002014-12-17 19:26:11 -080065 }
66
67 if (flowEntry.selector() != null) {
68 final JsonCodec<TrafficSelector> selectorCodec =
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080069 context.codec(TrafficSelector.class);
Seyeon Jeong8188da12020-03-03 12:49:48 -080070 result.set(SELECTOR, selectorCodec.encode(flowEntry.selector(), context));
Ray Milkey4f5de002014-12-17 19:26:11 -080071 }
72
73 return result;
74 }
75
Seyeon Jeong8188da12020-03-03 12:49:48 -080076 @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 Milkey4f5de002014-12-17 19:26:11 -0800104}
105