blob: b198fa30f8b17c8423d9b019d019707b4d256e94 [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;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.core.CoreService;
24import org.onosproject.net.flow.TrafficSelector;
25import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.flowobjective.DefaultForwardingObjective;
27import org.onosproject.net.flowobjective.ForwardingObjective;
28import org.slf4j.Logger;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31import static org.onlab.util.Tools.nullIsIllegal;
32import static org.slf4j.LoggerFactory.getLogger;
33
34/**
35 * Forwarding Objective Codec.
36 */
37public class ForwardingObjectiveCodec extends JsonCodec<ForwardingObjective> {
38 private final Logger log = getLogger(getClass());
39
40 // JSON field names
41 private static final String ID = "id";
42 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
119 ApplicationId appId = coreService.registerApplication(REST_APP_ID);
120 builder.fromApp(appId);
121
122 // decode flag
123 String flagStr = nullIsIllegal(json.get(FLAG), FLAG + MISSING_MEMBER_MESSAGE).asText();
124 switch (flagStr) {
125 case "SPECIFIC":
126 builder.withFlag(ForwardingObjective.Flag.SPECIFIC);
127 break;
128 case "VERSATILE":
129 builder.withFlag(ForwardingObjective.Flag.VERSATILE);
130 break;
131 default:
132 log.warn(INVALID_FLAG_MESSAGE, flagStr);
133 return null;
134 }
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();
158 ForwardingObjective forwardingObjective;
159
160 switch (opStr) {
161 case "ADD":
162 forwardingObjective = builder.add();
163 break;
164 case "REMOVE":
165 forwardingObjective = builder.remove();
166 break;
167 default:
168 log.warn(INVALID_OP_MESSAGE, opStr);
169 return null;
170 }
171
172 return forwardingObjective;
173 }
174}