blob: bd81ee0e3b18730c39fcd3e19450917ea4d71188 [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
18import org.onosproject.codec.CodecContext;
19import org.onosproject.codec.JsonCodec;
20import org.onosproject.core.CoreService;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.flow.DefaultFlowRule;
23import org.onosproject.net.flow.FlowRule;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flow.TrafficTreatment;
26
27import com.fasterxml.jackson.databind.node.ObjectNode;
28
29import 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";
39 private static final String DEVICE_ID = "deviceId";
40 private static final String TREATMENT = "treatment";
41 private static final String SELECTOR = "selector";
42 private static final String MISSING_MEMBER_MESSAGE =
43 " member is required in FlowRule";
Ray Milkeyeb5c7172015-06-23 14:59:27 -070044 public static final String REST_APP_ID = "org.onosproject.rest";
Ray Milkeyd43fe452015-05-29 09:35:12 -070045
46
47 @Override
48 public FlowRule decode(ObjectNode json, CodecContext context) {
49 if (json == null || !json.isObject()) {
50 return null;
51 }
52
53 FlowRule.Builder resultBuilder = new DefaultFlowRule.Builder();
54
Ray Milkeyd43fe452015-05-29 09:35:12 -070055 CoreService coreService = context.getService(CoreService.class);
Ray Milkeyeb5c7172015-06-23 14:59:27 -070056 resultBuilder.fromApp(coreService
57 .registerApplication(REST_APP_ID));
Ray Milkeyd43fe452015-05-29 09:35:12 -070058
59 int priority = nullIsIllegal(json.get(PRIORITY),
60 PRIORITY + MISSING_MEMBER_MESSAGE).asInt();
61 resultBuilder.withPriority(priority);
62
63 boolean isPermanent = nullIsIllegal(json.get(IS_PERMANENT),
64 IS_PERMANENT + MISSING_MEMBER_MESSAGE).asBoolean();
65 if (isPermanent) {
66 resultBuilder.makePermanent();
67 } else {
68 resultBuilder.makeTemporary(nullIsIllegal(json.get(TIMEOUT),
69 TIMEOUT
70 + MISSING_MEMBER_MESSAGE
71 + " if the flow is temporary").asInt());
72 }
73
74 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
75 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
76 resultBuilder.forDevice(deviceId);
77
78 ObjectNode treatmentJson = (ObjectNode) json.get(TREATMENT);
79 if (treatmentJson != null) {
80 JsonCodec<TrafficTreatment> treatmentCodec =
81 context.codec(TrafficTreatment.class);
82 resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
83 }
84
85 ObjectNode selectorJson = (ObjectNode) json.get(SELECTOR);
86 if (selectorJson != null) {
87 JsonCodec<TrafficSelector> selectorCodec =
88 context.codec(TrafficSelector.class);
89 resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
90 }
91
92 return resultBuilder.build();
93 }
94}