blob: da34c5ef2d920d51e08e980cb5bc82c9abdd11cd [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Yi Tseng18177a52017-09-08 01:26:59 -070027import org.onosproject.net.flow.IndexTableId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070028import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.TrafficTreatment;
Yi Tseng18177a52017-09-08 01:26:59 -070030import org.onosproject.net.pi.runtime.PiTableId;
Ray Milkeyd43fe452015-05-29 09:35:12 -070031
Jian Li2907ad22016-05-12 23:08:54 -070032import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyd43fe452015-05-29 09:35:12 -070033import static org.onlab.util.Tools.nullIsIllegal;
34
35/**
36 * Flow rule JSON codec.
37 */
38public final class FlowRuleCodec extends JsonCodec<FlowRule> {
39
Ray Milkeyd43fe452015-05-29 09:35:12 -070040 private static final String PRIORITY = "priority";
41 private static final String TIMEOUT = "timeout";
42 private static final String IS_PERMANENT = "isPermanent";
Jian Li2907ad22016-05-12 23:08:54 -070043 private static final String APP_ID = "appId";
Jian Li7c322f42016-03-04 11:00:59 -080044 private static final String TABLE_ID = "tableId";
Ray Milkeyd43fe452015-05-29 09:35:12 -070045 private static final String DEVICE_ID = "deviceId";
46 private static final String TREATMENT = "treatment";
47 private static final String SELECTOR = "selector";
Yi Tseng18177a52017-09-08 01:26:59 -070048 private static final String ID = "id";
49 private static final String TABLE_NAME = "tableName";
Ray Milkeyd43fe452015-05-29 09:35:12 -070050 private static final String MISSING_MEMBER_MESSAGE =
Jian Li2907ad22016-05-12 23:08:54 -070051 " member is required in FlowRule";
Ray Milkeyeb5c7172015-06-23 14:59:27 -070052 public static final String REST_APP_ID = "org.onosproject.rest";
Ray Milkeyd43fe452015-05-29 09:35:12 -070053
Jian Li2907ad22016-05-12 23:08:54 -070054 @Override
55 public ObjectNode encode(FlowRule flowRule, CodecContext context) {
56 checkNotNull(flowRule, "Flow rule cannot be null");
57
58 CoreService service = context.getService(CoreService.class);
59 ApplicationId appId = service.getAppId(flowRule.appId());
60 String strAppId = (appId == null) ? "<none>" : appId.name();
61
62 final ObjectNode result = context.mapper().createObjectNode()
Yi Tseng18177a52017-09-08 01:26:59 -070063 .put(ID, Long.toString(flowRule.id().value()))
64 .put(APP_ID, strAppId)
65 .put(PRIORITY, flowRule.priority())
66 .put(TIMEOUT, flowRule.timeout())
67 .put(IS_PERMANENT, flowRule.isPermanent())
68 .put(DEVICE_ID, flowRule.deviceId().toString())
69 .put(TABLE_ID, flowRule.tableId())
70 .put(TABLE_NAME, flowRule.table().toString());
Jian Li2907ad22016-05-12 23:08:54 -070071
72 if (flowRule.treatment() != null) {
73 final JsonCodec<TrafficTreatment> treatmentCodec =
74 context.codec(TrafficTreatment.class);
Yi Tseng18177a52017-09-08 01:26:59 -070075 result.set(TREATMENT, treatmentCodec.encode(flowRule.treatment(), context));
Jian Li2907ad22016-05-12 23:08:54 -070076 }
77
78 if (flowRule.selector() != null) {
79 final JsonCodec<TrafficSelector> selectorCodec =
80 context.codec(TrafficSelector.class);
Yi Tseng18177a52017-09-08 01:26:59 -070081 result.set(SELECTOR, selectorCodec.encode(flowRule.selector(), context));
Jian Li2907ad22016-05-12 23:08:54 -070082 }
83
84 return result;
85 }
Ray Milkeyd43fe452015-05-29 09:35:12 -070086
87 @Override
88 public FlowRule decode(ObjectNode json, CodecContext context) {
89 if (json == null || !json.isObject()) {
90 return null;
91 }
92
93 FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
94
Ray Milkeyd43fe452015-05-29 09:35:12 -070095 CoreService coreService = context.getService(CoreService.class);
Jian Li2907ad22016-05-12 23:08:54 -070096 JsonNode appIdJson = json.get(APP_ID);
97 String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
98 resultBuilder.fromApp(coreService.registerApplication(appId));
Ray Milkeyd43fe452015-05-29 09:35:12 -070099
100 int priority = nullIsIllegal(json.get(PRIORITY),
101 PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
102 resultBuilder.withPriority(priority);
103
104 boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT),
105 IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean();
106 if (isPermanent) {
107 resultBuilder.makePermanent();
108 } else {
109 resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT),
110 TIMEOUT
111 + MISSING_MEMBER_MESSAGE
112 + " if the flow is temporary").asInt());
113 }
114
Jian Li7c322f42016-03-04 11:00:59 -0800115 JsonNode tableIdJson = json.get(TABLE_ID);
116 if (tableIdJson != null) {
Yi Tseng18177a52017-09-08 01:26:59 -0700117 String tableId = tableIdJson.asText();
118 try {
119 int tid = Integer.parseInt(tableId);
120 resultBuilder.forTable(IndexTableId.of(tid));
121 } catch (NumberFormatException e) {
122 resultBuilder.forTable(PiTableId.of(tableId));
123 }
Jian Li7c322f42016-03-04 11:00:59 -0800124 }
125
Ray Milkeyd43fe452015-05-29 09:35:12 -0700126 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
127 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
128 resultBuilder.forDevice(deviceId);
129
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700130 ObjectNode treatmentJson = get(json, TREATMENT);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700131 if (treatmentJson != null) {
132 JsonCodec<TrafficTreatment> treatmentCodec =
133 context.codec(TrafficTreatment.class);
134 resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
135 }
136
Ray Milkeyb82c42b2015-06-30 09:42:20 -0700137 ObjectNode selectorJson = get(json, SELECTOR);
Ray Milkeyd43fe452015-05-29 09:35:12 -0700138 if (selectorJson != null) {
139 JsonCodec<TrafficSelector> selectorCodec =
140 context.codec(TrafficSelector.class);
141 resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
142 }
143
144 return resultBuilder.build();
145 }
146}