blob: 1dd339959eb2eb92f951ee6012898454b8d78b78 [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.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;
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070028import org.onosproject.net.flowobjective.NextTreatment;
29import org.onosproject.net.flowobjective.DefaultNextTreatment;
Jian Li8bcef8b2016-01-06 11:35:53 -080030import org.slf4j.Logger;
31
32import java.util.stream.IntStream;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35import static org.onlab.util.Tools.nullIsIllegal;
36import static org.slf4j.LoggerFactory.getLogger;
37
38/**
39 * Next Objective Codec.
40 */
Jian Lia424a052016-05-30 21:02:33 +090041public final class NextObjectiveCodec extends JsonCodec<NextObjective> {
Jian Li8bcef8b2016-01-06 11:35:53 -080042
43 private final Logger log = getLogger(getClass());
44
45 // JSON field names
46 private static final String ID = "id";
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 TYPE = "type";
49 private static final String OPERATION = "operation";
50 private static final String TREATMENTS = "treatments";
51 private static final String META = "meta";
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070052 private static final String WEIGHT = "weight";
Jian Li8bcef8b2016-01-06 11:35:53 -080053
54 // messages to be printed out
55 private static final String MISSING_MEMBER_MESSAGE =
56 " member is required in NextObjective";
57 private static final String NOT_NULL_MESSAGE =
58 "NextObjective cannot be null";
Jian Li8bcef8b2016-01-06 11:35:53 -080059
60 public static final String REST_APP_ID = "org.onosproject.rest";
61
62 @Override
63 public ObjectNode encode(NextObjective nextObjective, CodecContext context) {
64
65 checkNotNull(nextObjective, NOT_NULL_MESSAGE);
66
67 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
68 final JsonCodec<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
69
70 // encode common properties
71 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
72 ObjectNode result = och.encode(nextObjective, context);
73
74 // encode id
75 result.put(ID, nextObjective.id());
76
77 // encode type
78 result.put(TYPE, nextObjective.type().toString());
79
80 // encode operation
81 result.put(OPERATION, nextObjective.op().toString());
82
83 // encode treatments
84 ArrayNode treatments = context.mapper().createArrayNode();
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -070085 nextObjective.nextTreatments().forEach(nt -> {
86 if (nt.type().equals(NextTreatment.Type.TREATMENT)) {
87 TrafficTreatment tt = ((DefaultNextTreatment) nt).treatment();
88 ObjectNode treatmentJson = trafficTreatmentCodec.encode(tt, context);
89 treatmentJson.put(WEIGHT, nt.weight());
90 treatments.add(treatmentJson);
91 }
Jian Li8bcef8b2016-01-06 11:35:53 -080092 });
93 result.set(TREATMENTS, treatments);
94
95 // encode meta
96 if (nextObjective.meta() != null) {
97 ObjectNode trafficSelectorNode = trafficSelectorCodec.encode(nextObjective.meta(), context);
98 result.set(META, trafficSelectorNode);
99 }
100
101 return result;
102 }
103
104 @Override
105 public NextObjective 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<TrafficSelector> trafficSelectorCodec = context.codec(TrafficSelector.class);
113 final JsonCodec<TrafficTreatment> trafficTreatmentCodec = context.codec(TrafficTreatment.class);
114
115 ObjectiveCodecHelper och = new ObjectiveCodecHelper();
116
117 DefaultNextObjective.Builder baseBuilder = DefaultNextObjective.builder();
118 final DefaultNextObjective.Builder builder =
119 (DefaultNextObjective.Builder) och.decode(json, baseBuilder, context);
120
121 // decode id
122 JsonNode idJson = json.get(ID);
123 checkNotNull(idJson);
124 builder.withId(idJson.asInt());
125
126 // decode application id
Jian Lia424a052016-05-30 21:02:33 +0900127 JsonNode appIdJson = json.get(APP_ID);
128 String appId = appIdJson != null ? appIdJson.asText() : REST_APP_ID;
129 builder.fromApp(coreService.registerApplication(appId));
Jian Li8bcef8b2016-01-06 11:35:53 -0800130
131 // decode type
132 String typeStr = nullIsIllegal(json.get(TYPE), TYPE + MISSING_MEMBER_MESSAGE).asText();
133
134 switch (typeStr) {
135 case "HASHED":
136 builder.withType(NextObjective.Type.HASHED);
137 break;
138 case "BROADCAST":
139 builder.withType(NextObjective.Type.BROADCAST);
140 break;
141 case "FAILOVER":
142 builder.withType(NextObjective.Type.FAILOVER);
143 break;
144 case "SIMPLE":
145 builder.withType(NextObjective.Type.SIMPLE);
146 break;
147 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530148 throw new IllegalArgumentException("The requested type " + typeStr +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530149 " is not defined for NextObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800150 }
151
152 // decode treatments
153 JsonNode treatmentsJson = json.get(TREATMENTS);
154 checkNotNull(treatmentsJson);
155 if (treatmentsJson != null) {
156 IntStream.range(0, treatmentsJson.size()).forEach(i -> {
157 ObjectNode treatmentJson = get(treatmentsJson, i);
Niloofar Toorchi8bbe9ca2021-07-30 14:08:04 -0700158 JsonNode weightJson = treatmentJson.get(WEIGHT);
159 int weight = (weightJson != null) ? weightJson.asInt() : NextTreatment.DEFAULT_WEIGHT;
160 builder.addTreatment(DefaultNextTreatment.of(
161 trafficTreatmentCodec.decode(treatmentJson, context), weight));
Jian Li8bcef8b2016-01-06 11:35:53 -0800162 });
163 }
164
165 // decode meta
166 JsonNode metaJson = json.get(META);
167 if (metaJson != null) {
168 TrafficSelector trafficSelector = trafficSelectorCodec.decode((ObjectNode) metaJson, context);
169 builder.withMeta(trafficSelector);
170 }
171
172 // decode operation
173 String opStr = nullIsIllegal(json.get(OPERATION), OPERATION + MISSING_MEMBER_MESSAGE).asText();
174 NextObjective nextObjective;
175
176 switch (opStr) {
177 case "ADD":
178 nextObjective = builder.add();
179 break;
180 case "REMOVE":
181 nextObjective = builder.remove();
182 break;
183 default:
Kavitha Alagesandf352c62016-07-13 09:58:19 +0530184 throw new IllegalArgumentException("The requested operation " + opStr +
Sivachidambaram Subramaniana0efdcc2017-06-22 05:26:46 +0530185 " is not defined for NextObjective.");
Jian Li8bcef8b2016-01-06 11:35:53 -0800186 }
187
188 return nextObjective;
189 }
190}