blob: 1a41e5c39165fe07a29e2aad12f41732bf308486 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkeyd43fe452015-05-29 09:35:12 -07003 *
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
Jian Li7c322f42016-03-04 11:00:59 -080018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkeyd43fe452015-05-29 09:35:12 -070020import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
Jian Li2907ad22016-05-12 23:08:54 -070022import org.onosproject.core.ApplicationId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import org.onosproject.core.CoreService;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.flow.DefaultFlowRule;
26import org.onosproject.net.flow.FlowRule;
27import org.onosproject.net.flow.TrafficSelector;
28import org.onosproject.net.flow.TrafficTreatment;
29
Jian Li2907ad22016-05-12 23:08:54 -070030import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyd43fe452015-05-29 09:35:12 -070031import static org.onlab.util.Tools.nullIsIllegal;
32
33/**
34 * Flow rule JSON codec.
35 */
36public final class FlowRuleCodec extends JsonCodec<FlowRule> {
37
Ray Milkeyd43fe452015-05-29 09:35:12 -070038 private static final String PRIORITY = "priority";
39 private static final String TIMEOUT = "timeout";
40 private static final String IS_PERMANENT = "isPermanent";
Jian Li2907ad22016-05-12 23:08:54 -070041 private static final String APP_ID = "appId";
Jian Li7c322f42016-03-04 11:00:59 -080042 private static final String TABLE_ID = "tableId";
Ray Milkeyd43fe452015-05-29 09:35:12 -070043 private static final String DEVICE_ID = "deviceId";
44 private static final String TREATMENT = "treatment";
45 private static final String SELECTOR = "selector";
46 private static final String MISSING_MEMBER_MESSAGE =
Jian Li2907ad22016-05-12 23:08:54 -070047 " member is required in FlowRule";
Ray Milkeyeb5c7172015-06-23 14:59:27 -070048 public static final String REST_APP_ID = "org.onosproject.rest";
Ray Milkeyd43fe452015-05-29 09:35:12 -070049
Jian Li2907ad22016-05-12 23:08:54 -070050 @Override
51 public ObjectNode encode(FlowRule flowRule, CodecContext context) {
52 checkNotNull(flowRule, "Flow rule cannot be null");
53
54 CoreService service = context.getService(CoreService.class);
55 ApplicationId appId = service.getAppId(flowRule.appId());
56 String strAppId = (appId == null) ? "<none>" : appId.name();
57
58 final ObjectNode result = context.mapper().createObjectNode()
59 .put("id", Long.toString(flowRule.id().value()))
60 .put("tableId", flowRule.tableId())
61 .put("appId", strAppId)
62 .put("priority", flowRule.priority())
63 .put("timeout", flowRule.timeout())
64 .put("isPermanent", flowRule.isPermanent())
65 .put("deviceId", flowRule.deviceId().toString());
66
67 if (flowRule.treatment() != null) {
68 final JsonCodec<TrafficTreatment> treatmentCodec =
69 context.codec(TrafficTreatment.class);
70 result.set("treatment", treatmentCodec.encode(flowRule.treatment(), context));
71 }
72
73 if (flowRule.selector() != null) {
74 final JsonCodec<TrafficSelector> selectorCodec =
75 context.codec(TrafficSelector.class);
76 result.set("selector", selectorCodec.encode(flowRule.selector(), context));
77 }
78
79 return result;
80 }
Ray Milkeyd43fe452015-05-29 09:35:12 -070081
82 @Override
83 public FlowRule decode(ObjectNode json, CodecContext context) {
84 if (json == null || !json.isObject()) {
85 return null;
86 }
87
88 FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
89
Ray Milkeyd43fe452015-05-29 09:35:12 -070090 CoreService coreService = context.getService(CoreService.class);
Jian Li2907ad22016-05-12 23:08:54 -070091 JsonNode appIdJson = json.get(APP_ID);
92 String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
93 resultBuilder.fromApp(coreService.registerApplication(appId));
Ray Milkeyd43fe452015-05-29 09:35:12 -070094
95 int priority = nullIsIllegal(json.get(PRIORITY),
96 PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
97 resultBuilder.withPriority(priority);
98
99 boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT),
100 IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean();
101 if (isPermanent) {
102 resultBuilder.makePermanent();
103 } else {
104 resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT),
105 TIMEOUT
106 + MISSING_MEMBER_MESSAGE
107 + " if the flow is temporary").asInt());
108 }
109
Jian Li7c322f42016-03-04 11:00:59 -0800110 JsonNode tableIdJson = json.get(TABLE_ID);
111 if (tableIdJson != null) {
112 resultBuilder.forTable(tableIdJson.asInt());
113 }
114
Ray Milkeyd43fe452015-05-29 09:35:12 -0700115 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
116 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
117 resultBuilder.forDevice(deviceId);
118
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700119 ObjectNode treatmentJson = get(json, TREATMENT);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700120 if (treatmentJson != null) {
121 JsonCodec<TrafficTreatment> treatmentCodec =
122 context.codec(TrafficTreatment.class);
123 resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
124 }
125
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700126 ObjectNode selectorJson = get(json, SELECTOR);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700127 if (selectorJson != null) {
128 JsonCodec<TrafficSelector> selectorCodec =
129 context.codec(TrafficSelector.class);
130 resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
131 }
132
133 return resultBuilder.build();
134 }
135}