blob: e36fe89f6f0dfb5d84302487e1898c801cf2a5fe [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;
22import org.onosproject.core.CoreService;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.flow.DefaultFlowRule;
25import org.onosproject.net.flow.FlowRule;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.TrafficTreatment;
28
Ray Milkeyd43fe452015-05-29 09:35:12 -070029import static org.onlab.util.Tools.nullIsIllegal;
30
31/**
32 * Flow rule JSON codec.
33 */
34public final class FlowRuleCodec extends JsonCodec<FlowRule> {
35
Ray Milkeyd43fe452015-05-29 09:35:12 -070036 private static final String PRIORITY = "priority";
37 private static final String TIMEOUT = "timeout";
38 private static final String IS_PERMANENT = "isPermanent";
Jian Li7c322f42016-03-04 11:00:59 -080039 private static final String TABLE_ID = "tableId";
Ray Milkeyd43fe452015-05-29 09:35:12 -070040 private static final String DEVICE_ID = "deviceId";
41 private static final String TREATMENT = "treatment";
42 private static final String SELECTOR = "selector";
43 private static final String MISSING_MEMBER_MESSAGE =
44 " member is required in FlowRule";
Ray Milkeyeb5c7172015-06-23 14:59:27 -070045 public static final String REST_APP_ID = "org.onosproject.rest";
Ray Milkeyd43fe452015-05-29 09:35:12 -070046
47
48 @Override
49 public FlowRule decode(ObjectNode json, CodecContext context) {
50 if (json == null || !json.isObject()) {
51 return null;
52 }
53
54 FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
55
Ray Milkeyd43fe452015-05-29 09:35:12 -070056 CoreService coreService = context.getService(CoreService.class);
Ray Milkeyeb5c7172015-06-23 14:59:27 -070057 resultBuilder.fromApp(coreService
58 .registerApplication(REST_APP_ID));
Ray Milkeyd43fe452015-05-29 09:35:12 -070059
60 int priority = nullIsIllegal(json.get(PRIORITY),
61 PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
62 resultBuilder.withPriority(priority);
63
64 boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT),
65 IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean();
66 if (isPermanent) {
67 resultBuilder.makePermanent();
68 } else {
69 resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT),
70 TIMEOUT
71 + MISSING_MEMBER_MESSAGE
72 + " if the flow is temporary").asInt());
73 }
74
Jian Li7c322f42016-03-04 11:00:59 -080075 JsonNode tableIdJson = json.get(TABLE_ID);
76 if (tableIdJson != null) {
77 resultBuilder.forTable(tableIdJson.asInt());
78 }
79
Ray Milkeyd43fe452015-05-29 09:35:12 -070080 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
81 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
82 resultBuilder.forDevice(deviceId);
83
Ray Milkeyb82c42b2015-06-30 09:42:20 -070084 ObjectNode treatmentJson = get(json, TREATMENT);
Ray Milkeyd43fe452015-05-29 09:35:12 -070085 if (treatmentJson != null) {
86 JsonCodec<TrafficTreatment> treatmentCodec =
87 context.codec(TrafficTreatment.class);
88 resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
89 }
90
Ray Milkeyb82c42b2015-06-30 09:42:20 -070091 ObjectNode selectorJson = get(json, SELECTOR);
Ray Milkeyd43fe452015-05-29 09:35:12 -070092 if (selectorJson != null) {
93 JsonCodec<TrafficSelector> selectorCodec =
94 context.codec(TrafficSelector.class);
95 resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
96 }
97
98 return resultBuilder.build();
99 }
100}