blob: 673e4c8b19be30f5e201ef333b5a0a182372fe9f [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.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
Jian Li8bcef8b2016-01-06 11:35:53 -080023import org.onosproject.core.CoreService;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.flow.criteria.Criterion;
26import org.onosproject.net.flowobjective.DefaultFilteringObjective;
27import org.onosproject.net.flowobjective.FilteringObjective;
28import org.slf4j.Logger;
29
30import java.util.stream.IntStream;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static org.onlab.util.Tools.nullIsIllegal;
34import static org.slf4j.LoggerFactory.getLogger;
35
36/**
37 * Filtering Objective Codec.
38 */
Jian Lia424a052016-05-30 21:02:33 +090039public final class FilteringObjectiveCodec extends JsonCodec<FilteringObjective> {
Jian Li8bcef8b2016-01-06 11:35:53 -080040 private final Logger log = getLogger(getClass());
41
42 // JSON field names
43 private static final String ID = "id";
44 private static final String TYPE = "type";
45 private static final String KEY = "key";
46 private static final String META = "meta";
Jian Lia424a052016-05-30 21:02:33 +090047 private static final String APP_ID = "appId";
Jian Li8bcef8b2016-01-06 11:35:53 -080048 private static final String OPERATION = "operation";
49 private static final String CONDITIONS = "conditions";
50
51 // messages to be printed out
52 private static final String MISSING_MEMBER_MESSAGE =
53 " member is required in FilteringObjective";
54 private static final String NOT_NULL_MESSAGE =
55 "FilteringObjective cannot be null";
56 private static final String INVALID_TYPE_MESSAGE =
57 "The requested type {} is not defined in FilteringObjective.";
58 private static final String INVALID_OP_MESSAGE =
59 "The requested operation {} is not defined for FilteringObjective.";
60
61 public static final String REST_APP_ID = "org.onosproject.rest";
62
63 @Override
64 public ObjectNode encode(FilteringObjective filteringObjective, CodecContext context) {
65
66 checkNotNull(filteringObjective, NOT_NULL_MESSAGE);
67
68 final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class);
69 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
70
71 // encode common properties
72 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
73 ObjectNode result = och.encode(filteringObjective, context);
74
75 // encode id
76 result.put(ID, filteringObjective.id());
77
78 // encode type
79 result.put(TYPE, filteringObjective.type().toString());
80
81 // encode key
82 if (filteringObjective.key() != null) {
83 ObjectNode criterionNode = criterionCodec.encode(filteringObjective.key(), context);
84 result.set(KEY, criterionNode);
85 }
86
87 // encode meta
88 if (filteringObjective.meta() != null) {
89 ObjectNode trafficTreatmentNode = trafficTreatmentCodec.encode(filteringObjective.meta(), context);
90 result.set(META, trafficTreatmentNode);
91 }
92
93 // encode conditions
94 ArrayNode conditions = context.mapper().createArrayNode();
95 filteringObjective.conditions().forEach(c -> {
96 ObjectNode criterionJson = criterionCodec.encode(c, context);
97 conditions.add(criterionJson);
98 });
99 result.set(CONDITIONS, conditions);
100
101 return result;
102 }
103
104 @Override
105 public FilteringObjective decode(ObjectNode json, CodecContext context) {
106 if (json == null || !json.isObject()) {
107 return null;
108 }
109
110 CoreService coreService = context.getService(CoreService.class);
111
112 final JsonCodec<Criterion> criterionCodec = context.codec(Criterion.class);
113 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
114
115 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
116
117 DefaultFilteringObjective.Builder baseBuilder = DefaultFilteringObjective.builder();
118 final DefaultFilteringObjective.Builder builder =
119 (DefaultFilteringObjective.Builder) och.decode(json, baseBuilder, context);
120
Jian Lia424a052016-05-30 21:02:33 +0900121
122
Jian Li8bcef8b2016-01-06 11:35:53 -0800123 // application id
Jian Lia424a052016-05-30 21:02:33 +0900124 JsonNode appIdJson = json.get(APP_ID);
125 String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
126 builder.fromApp(coreService.registerApplication(appId));
Jian Li8bcef8b2016-01-06 11:35:53 -0800127
128 // decode type
129 String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
130
131 switch (typeStr) {
132 case "PERMIT":
133 builder.permit();
134 break;
135 case "DENY":
136 builder.deny();
137 break;
138 default:
139 log.warn(INVALID_TYPE_MESSAGE, typeStr);
140 return null;
141 }
142
143 // decode key
144 JsonNode keyJson = json.get(KEY);
145 if (keyJson != null) {
146 Criterion key = criterionCodec.decode((ObjectNode) keyJson, context);
147 builder.withKey(key);
148 }
149
150 // decode conditions
151 JsonNode conditionsJson = json.get(CONDITIONS);
152 checkNotNull(conditionsJson);
153 if (conditionsJson != null) {
154 IntStream.range(0, conditionsJson.size()).forEach(i -> {
155 ObjectNode conditionJson = get(conditionsJson, i);
156 builder.addCondition(criterionCodec.decode(conditionJson, context));
157 });
158 }
159
160 // decode meta
161 JsonNode metaJson = json.get(META);
162 if (metaJson != null) {
163 TrafficTreatment trafficTreatment = trafficTreatmentCodec.decode((ObjectNode) metaJson, context);
164 builder.withMeta(trafficTreatment);
165 }
166
167 // decode operation
168 String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
169 FilteringObjective filteringObjective;
170
171 switch (opStr) {
172 case "ADD":
173 filteringObjective = builder.add();
174 break;
175 case "REMOVE":
176 filteringObjective = builder.remove();
177 break;
178 default:
179 log.warn(INVALID_OP_MESSAGE, opStr);
180 return null;
181 }
182
183 return filteringObjective;
184 }
185}