blob: 8ec51fa285307e282454dd045f4032273091b6bf [file] [log] [blame]
Jian Li8bcef8b2016-01-06 11:35:53 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700128 case "EGRESS":
129 builder.withFlag(ForwardingObjective.Flag.EGRESS);
130 break;
Jian Li8bcef8b2016-01-06 11:35:53 -0800131 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530132 throw new IllegalArgumentException("The requested flag " + flagStr +
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700133 " is not defined for ForwardingObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800134 }
135
136 // decode selector
137 JsonNode selectorJson = json.get(SELECTOR);
138 if (selectorJson != null) {
139 TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) selectorJson, context);
140 builder.withSelector(trafficSelector);
141 }
142
143 // decode treatment
144 JsonNode treatmentJson = json.get(TREATMENT);
145 if (treatmentJson != null) {
146 TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) treatmentJson, context);
147 builder.withTreatment(trafficTreatment);
148 }
149
150 // decode nextId
151 JsonNode nextIdJson = json.get(NEXT_ID);
152 if (nextIdJson != null) {
153 builder.nextStep(nextIdJson.asInt());
154 }
155
156 // decode operation
157 String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530158 ForwardingObjective forwardingObjective = null;
Jian Li8bcef8b2016-01-06 11:35:53 -0800159
160 switch (opStr) {
161 case "ADD":
162 forwardingObjective = builder.add();
163 break;
164 case "REMOVE":
165 forwardingObjective = builder.remove();
166 break;
167 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530168 throw new IllegalArgumentException("The requested operation " + opStr +
169 " is not defined for FilteringObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800170 }
171
172 return forwardingObjective;
173 }
174}