blob: ad198c98d14f66de32e42232686abed3706b618b [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li8bcef8b2016-01-06 11:35:53 -08003 *
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 com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import org.onosproject.codec.CodecContext;
21import org.onosproject.codec.JsonCodec;
Jian Li8bcef8b2016-01-06 11:35:53 -080022import org.onosproject.core.CoreService;
23import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.flowobjective.DefaultForwardingObjective;
26import org.onosproject.net.flowobjective.ForwardingObjective;
27import org.slf4j.Logger;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30import static org.onlab.util.Tools.nullIsIllegal;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Forwarding Objective Codec.
35 */
Jian Lia424a052016-05-30 21:02:33 +090036public final class ForwardingObjectiveCodec extends JsonCodec<ForwardingObjective> {
Jian Li8bcef8b2016-01-06 11:35:53 -080037 private final Logger log = getLogger(getClass());
38
39 // JSON field names
40 private static final String ID = "id";
Jian Lia424a052016-05-30 21:02:33 +090041 private static final String APP_ID = "appId";
Jian Li8bcef8b2016-01-06 11:35:53 -080042 private static final String SELECTOR = "selector";
43 private static final String FLAG = "flag";
44 private static final String OPERATION = "operation";
45 private static final String NEXT_ID = "nextId";
46 private static final String TREATMENT = "treatment";
47
48 // messages to be printed out
49 private static final String MISSING_MEMBER_MESSAGE =
50 " member is required in ForwardingObjective";
51 private static final String NOT_NULL_MESSAGE =
52 "ForwardingObjective cannot be null";
Jian Li8bcef8b2016-01-06 11:35:53 -080053
54 public static final String REST_APP_ID = "org.onosproject.rest";
55
56 @Override
57 public ObjectNode encode(ForwardingObjective forwardingObjective, CodecContext context) {
58
59 checkNotNull(forwardingObjective, NOT_NULL_MESSAGE);
60
61 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
62 final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
63
64 // encode common properties
65 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
66 ObjectNode result = och.encode(forwardingObjective, context);
67
68 // encode id
69 result.put(ID, forwardingObjective.id());
70
71 // encode flag
72 result.put(FLAG, forwardingObjective.flag().toString());
73
74 // encode op
75 result.put(OPERATION, forwardingObjective.op().toString());
76
77 // encode selector
78 ObjectNode trafficSelectorNode =
79 trafficSelectorCodec.encode(forwardingObjective.selector(), context);
80 result.set(SELECTOR, trafficSelectorNode);
81
82 // encode nextId
83 if (forwardingObjective.nextId() != null) {
84 result.put(NEXT_ID, forwardingObjective.nextId());
85 }
86
87 // encode treatment
88 if (forwardingObjective.treatment() != null) {
89 ObjectNode trafficTreatmentNode =
90 trafficTreatmentCodec.encode(forwardingObjective.treatment(), context);
91 result.set(TREATMENT, trafficTreatmentNode);
92 }
93
94 return result;
95 }
96
97 @Override
98 public ForwardingObjective decode(ObjectNode json, CodecContext context) {
99 if (json == null || !json.isObject()) {
100 return null;
101 }
102
103 CoreService coreService = context.getService(CoreService.class);
104
105 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
106 final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
107
108 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
109
110 DefaultForwardingObjective.Builder baseBuilder = DefaultForwardingObjective.builder();
111 final DefaultForwardingObjective.Builder builder =
112 (DefaultForwardingObjective.Builder) och.decode(json, baseBuilder, context);
113
114 // application id
Jian Lia424a052016-05-30 21:02:33 +0900115 JsonNode appIdJson = json.get(APP_ID);
116 String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
117 builder.fromApp(coreService.registerApplication(appId));
Jian Li8bcef8b2016-01-06 11:35:53 -0800118
119 // decode flag
120 String flagStr = nullIsIllegal(json.get(FLAG), FLAG + MISSING_MEMBER_MESSAGE).asText();
121 switch (flagStr) {
122 case "SPECIFIC":
123 builder.withFlag(ForwardingObjective.Flag.SPECIFIC);
124 break;
125 case "VERSATILE":
126 builder.withFlag(ForwardingObjective.Flag.VERSATILE);
127 break;
128 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530129 throw new IllegalArgumentException("The requested flag " + flagStr +
130 " is not defined for FilteringObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800131 }
132
133 // decode selector
134 JsonNode selectorJson = json.get(SELECTOR);
135 if (selectorJson != null) {
136 TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) selectorJson, context);
137 builder.withSelector(trafficSelector);
138 }
139
140 // decode treatment
141 JsonNode treatmentJson = json.get(TREATMENT);
142 if (treatmentJson != null) {
143 TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) treatmentJson, context);
144 builder.withTreatment(trafficTreatment);
145 }
146
147 // decode nextId
148 JsonNode nextIdJson = json.get(NEXT_ID);
149 if (nextIdJson != null) {
150 builder.nextStep(nextIdJson.asInt());
151 }
152
153 // decode operation
154 String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530155 ForwardingObjective forwardingObjective = null;
Jian Li8bcef8b2016-01-06 11:35:53 -0800156
157 switch (opStr) {
158 case "ADD":
159 forwardingObjective = builder.add();
160 break;
161 case "REMOVE":
162 forwardingObjective = builder.remove();
163 break;
164 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530165 throw new IllegalArgumentException("The requested operation " + opStr +
166 " is not defined for FilteringObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800167 }
168
169 return forwardingObjective;
170 }
171}