blob: 5926832e82d2be620788c7e9a0d7e8b3a63993ba [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Jonathan Hart834c8d72016-03-17 07:40:21 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey4f5de002014-12-17 19:26:11 -080019import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
Jonathan Hart834c8d72016-03-17 07:40:21 -070021import org.onosproject.core.ApplicationId;
Ray Milkeybe539db2015-09-04 11:00:43 -070022import org.onosproject.core.CoreService;
Ray Milkey4f5de002014-12-17 19:26:11 -080023import org.onosproject.net.flow.FlowEntry;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flow.TrafficTreatment;
26
Ray Milkey4f5de002014-12-17 19:26:11 -080027import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Flow entry JSON codec.
31 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080032public final class FlowEntryCodec extends JsonCodec<FlowEntry> {
Ray Milkey4f5de002014-12-17 19:26:11 -080033
34 @Override
35 public ObjectNode encode(FlowEntry flowEntry, CodecContext context) {
36 checkNotNull(flowEntry, "Flow entry cannot be null");
37
Ray Milkeybe539db2015-09-04 11:00:43 -070038 CoreService service = context.getService(CoreService.class);
39
Jonathan Hart834c8d72016-03-17 07:40:21 -070040 ApplicationId appId = service.getAppId(flowEntry.appId());
41
42 String strAppId = (appId == null) ? "<none>" : appId.name();
43
Ray Milkey4f5de002014-12-17 19:26:11 -080044 final ObjectNode result = context.mapper().createObjectNode()
45 .put("id", Long.toString(flowEntry.id().value()))
Srikanth Vavilapallid120f5c2015-11-24 14:15:01 -080046 .put("tableId", flowEntry.tableId())
Jonathan Hart834c8d72016-03-17 07:40:21 -070047 .put("appId", strAppId)
Ray Milkey4f5de002014-12-17 19:26:11 -080048 .put("groupId", flowEntry.groupId().id())
49 .put("priority", flowEntry.priority())
50 .put("timeout", flowEntry.timeout())
51 .put("isPermanent", flowEntry.isPermanent())
52 .put("deviceId", flowEntry.deviceId().toString())
53 .put("state", flowEntry.state().toString())
54 .put("life", flowEntry.life())
55 .put("packets", flowEntry.packets())
56 .put("bytes", flowEntry.bytes())
57 .put("lastSeen", flowEntry.lastSeen());
58
59 if (flowEntry.treatment() != null) {
60 final JsonCodec<TrafficTreatment> treatmentCodec =
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080061 context.codec(TrafficTreatment.class);
Ray Milkey4f5de002014-12-17 19:26:11 -080062 result.set("treatment", treatmentCodec.encode(flowEntry.treatment(), context));
63 }
64
65 if (flowEntry.selector() != null) {
66 final JsonCodec<TrafficSelector> selectorCodec =
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080067 context.codec(TrafficSelector.class);
Ray Milkey4f5de002014-12-17 19:26:11 -080068 result.set("selector", selectorCodec.encode(flowEntry.selector(), context));
69 }
70
71 return result;
72 }
73
74}
75