blob: 6b2ad899e727e4228685dddd26c795d40b871e18 [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.TrafficSelector;
25import org.onosproject.net.flow.TrafficTreatment;
26import org.onosproject.net.flowobjective.DefaultNextObjective;
27import org.onosproject.net.flowobjective.NextObjective;
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 * Next Objective Codec.
38 */
Jian Lia424a052016-05-30 21:02:33 +090039public final class NextObjectiveCodec extends JsonCodec<NextObjective> {
Jian Li8bcef8b2016-01-06 11:35:53 -080040
41 private final Logger log = getLogger(getClass());
42
43 // JSON field names
44 private static final String ID = "id";
Jian Lia424a052016-05-30 21:02:33 +090045 private static final String APP_ID = "appId";
Jian Li8bcef8b2016-01-06 11:35:53 -080046 private static final String TYPE = "type";
47 private static final String OPERATION = "operation";
48 private static final String TREATMENTS = "treatments";
49 private static final String META = "meta";
50
51 // messages to be printed out
52 private static final String MISSING_MEMBER_MESSAGE =
53 " member is required in NextObjective";
54 private static final String NOT_NULL_MESSAGE =
55 "NextObjective cannot be null";
Jian Li8bcef8b2016-01-06 11:35:53 -080056
57 public static final String REST_APP_ID = "org.onosproject.rest";
58
59 @Override
60 public ObjectNode encode(NextObjective nextObjective, CodecContext context) {
61
62 checkNotNull(nextObjective, NOT_NULL_MESSAGE);
63
64 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
65 final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
66
67 // encode common properties
68 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
69 ObjectNode result = och.encode(nextObjective, context);
70
71 // encode id
72 result.put(ID, nextObjective.id());
73
74 // encode type
75 result.put(TYPE, nextObjective.type().toString());
76
77 // encode operation
78 result.put(OPERATION, nextObjective.op().toString());
79
80 // encode treatments
81 ArrayNode treatments = context.mapper().createArrayNode();
82 nextObjective.next().forEach(t -> {
83 ObjectNode treatmentJson = trafficTreatmentCodec.encode(t, context);
84 treatments.add(treatmentJson);
85 });
86 result.set(TREATMENTS, treatments);
87
88 // encode meta
89 if (nextObjective.meta() != null) {
90 ObjectNode trafficSelectorNode = trafficSelectorCodec.encode(nextObjective.meta(), context);
91 result.set(META, trafficSelectorNode);
92 }
93
94 return result;
95 }
96
97 @Override
98 public NextObjective 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<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
106 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
107
108 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
109
110 DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder();
111 final DefaultNextObjective.Builder builder =
112 (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context);
113
114 // decode id
115 JsonNode idJson = json.get(ID);
116 checkNotNull(idJson);
117 builder.withId(idJson.asInt());
118
119 // decode application id
Jian Lia424a052016-05-30 21:02:33 +0900120 JsonNode appIdJson = json.get(APP_ID);
121 String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
122 builder.fromApp(coreService.registerApplication(appId));
Jian Li8bcef8b2016-01-06 11:35:53 -0800123
124 // decode type
125 String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
126
127 switch (typeStr) {
128 case "HASHED":
129 builder.withType(NextObjective.Type.HASHED);
130 break;
131 case "BROADCAST":
132 builder.withType(NextObjective.Type.BROADCAST);
133 break;
134 case "FAILOVER":
135 builder.withType(NextObjective.Type.FAILOVER);
136 break;
137 case "SIMPLE":
138 builder.withType(NextObjective.Type.SIMPLE);
139 break;
140 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530141 throw new IllegalArgumentException("The requested type " + typeStr +
142 " is not defined for FilteringObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800143 }
144
145 // decode treatments
146 JsonNode treatmentsJson = json.get(TREATMENTS);
147 checkNotNull(treatmentsJson);
148 if (treatmentsJson != null) {
149 IntStream.range(0, treatmentsJson.size()).forEach(i -> {
150 ObjectNode treatmentJson = get(treatmentsJson, i);
151 builder.addTreatment(trafficTreatmentCodec.decode(treatmentJson, context));
152 });
153 }
154
155 // decode meta
156 JsonNode metaJson = json.get(META);
157 if (metaJson != null) {
158 TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context);
159 builder.withMeta(trafficSelector);
160 }
161
162 // decode operation
163 String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
164 NextObjective nextObjective;
165
166 switch (opStr) {
167 case "ADD":
168 nextObjective = builder.add();
169 break;
170 case "REMOVE":
171 nextObjective = builder.remove();
172 break;
173 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530174 throw new IllegalArgumentException("The requested operation " + opStr +
175 " is not defined for FilteringObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800176 }
177
178 return nextObjective;
179 }
180}