blob: 864756a3c20a87fcf2f31456525239a0d3b926c8 [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";
53 private static final String INVALID_FLAG_MESSAGE =
54 "The requested flag {} is not defined in ForwardingObjective.";
55 private static final String INVALID_OP_MESSAGE =
56 "The requested operation {} is not defined for FilteringObjective.";
57
58 public static final String REST_APP_ID = "org.onosproject.rest";
59
60 @Override
61 public ObjectNode encode(ForwardingObjective forwardingObjective, CodecContext context) {
62
63 checkNotNull(forwardingObjective, NOT_NULL_MESSAGE);
64
65 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
66 final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
67
68 // encode common properties
69 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
70 ObjectNode result = och.encode(forwardingObjective, context);
71
72 // encode id
73 result.put(ID, forwardingObjective.id());
74
75 // encode flag
76 result.put(FLAG, forwardingObjective.flag().toString());
77
78 // encode op
79 result.put(OPERATION, forwardingObjective.op().toString());
80
81 // encode selector
82 ObjectNode trafficSelectorNode =
83 trafficSelectorCodec.encode(forwardingObjective.selector(), context);
84 result.set(SELECTOR, trafficSelectorNode);
85
86 // encode nextId
87 if (forwardingObjective.nextId() != null) {
88 result.put(NEXT_ID, forwardingObjective.nextId());
89 }
90
91 // encode treatment
92 if (forwardingObjective.treatment() != null) {
93 ObjectNode trafficTreatmentNode =
94 trafficTreatmentCodec.encode(forwardingObjective.treatment(), context);
95 result.set(TREATMENT, trafficTreatmentNode);
96 }
97
98 return result;
99 }
100
101 @Override
102 public ForwardingObjective decode(ObjectNode json, CodecContext context) {
103 if (json == null || !json.isObject()) {
104 return null;
105 }
106
107 CoreService coreService = context.getService(CoreService.class);
108
109 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
110 final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
111
112 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
113
114 DefaultForwardingObjective.Builder baseBuilder = DefaultForwardingObjective.builder();
115 final DefaultForwardingObjective.Builder builder =
116 (DefaultForwardingObjective.Builder) och.decode(json, baseBuilder, context);
117
118 // application id
Jian Lia424a052016-05-30 21:02:33 +0900119 JsonNode appIdJson = json.get(APP_ID);
120 String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
121 builder.fromApp(coreService.registerApplication(appId));
Jian Li8bcef8b2016-01-06 11:35:53 -0800122
123 // decode flag
124 String flagStr = nullIsIllegal(json.get(FLAG), FLAG + MISSING_MEMBER_MESSAGE).asText();
125 switch (flagStr) {
126 case "SPECIFIC":
127 builder.withFlag(ForwardingObjective.Flag.SPECIFIC);
128 break;
129 case "VERSATILE":
130 builder.withFlag(ForwardingObjective.Flag.VERSATILE);
131 break;
132 default:
133 log.warn(INVALID_FLAG_MESSAGE, flagStr);
134 return null;
135 }
136
137 // decode selector
138 JsonNode selectorJson = json.get(SELECTOR);
139 if (selectorJson != null) {
140 TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) selectorJson, context);
141 builder.withSelector(trafficSelector);
142 }
143
144 // decode treatment
145 JsonNode treatmentJson = json.get(TREATMENT);
146 if (treatmentJson != null) {
147 TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) treatmentJson, context);
148 builder.withTreatment(trafficTreatment);
149 }
150
151 // decode nextId
152 JsonNode nextIdJson = json.get(NEXT_ID);
153 if (nextIdJson != null) {
154 builder.nextStep(nextIdJson.asInt());
155 }
156
157 // decode operation
158 String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
159 ForwardingObjective forwardingObjective;
160
161 switch (opStr) {
162 case "ADD":
163 forwardingObjective = builder.add();
164 break;
165 case "REMOVE":
166 forwardingObjective = builder.remove();
167 break;
168 default:
169 log.warn(INVALID_OP_MESSAGE, opStr);
170 return null;
171 }
172
173 return forwardingObjective;
174 }
175}