blob: 94a1948117a3a8f7e41732a177c346542c4d206a [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
36 private static final String APP_ID = "appId";
37 private static final String PRIORITY = "priority";
38 private static final String TIMEOUT = "timeout";
39 private static final String IS_PERMANENT = "isPermanent";
40 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";
45
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
55 short appId = nullIsIllegal(json.get(APP_ID),
56 APP_ID + MISSING_MEMBER_MESSAGE).shortValue();
57 CoreService coreService = context.getService(CoreService.class);
58 resultBuilder.fromApp(coreService.getAppId(appId));
59
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
75 DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID),
76 DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
77 resultBuilder.forDevice(deviceId);
78
79 ObjectNode treatmentJson = (ObjectNode) json.get(TREATMENT);
80 if (treatmentJson != null) {
81 JsonCodec<TrafficTreatment> treatmentCodec =
82 context.codec(TrafficTreatment.class);
83 resultBuilder.withTreatment(treatmentCodec.decode(treatmentJson, context));
84 }
85
86 ObjectNode selectorJson = (ObjectNode) json.get(SELECTOR);
87 if (selectorJson != null) {
88 JsonCodec<TrafficSelector> selectorCodec =
89 context.codec(TrafficSelector.class);
90 resultBuilder.withSelector(selectorCodec.decode(selectorJson, context));
91 }
92
93 return resultBuilder.build();
94 }
95}