blob: 4a2bc92a5b1295353aa7dd449d2b6a7498bd846c [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkey4f5de002014-12-17 19:26:11 -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
Jonathan Hartaa178db2017-04-19 17:11:48 -040018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey4f5de002014-12-17 19:26:11 -080021import org.onosproject.codec.CodecContext;
22import org.onosproject.codec.JsonCodec;
Ray Milkeyd43fe452015-05-29 09:35:12 -070023import org.onosproject.net.flow.DefaultTrafficTreatment;
Ray Milkey4f5de002014-12-17 19:26:11 -080024import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.flow.instructions.Instruction;
cansu.toprak409289d2017-10-27 10:04:05 +030026import org.onosproject.net.flow.instructions.Instructions;
Ray Milkey4f5de002014-12-17 19:26:11 -080027
Jonathan Hartaa178db2017-04-19 17:11:48 -040028import java.util.stream.IntStream;
Ray Milkey4f5de002014-12-17 19:26:11 -080029
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Traffic treatment codec.
34 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080035public final class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> {
Ray Milkeyd43fe452015-05-29 09:35:12 -070036 private static final String INSTRUCTIONS = "instructions";
Saurav Das1bec2be2017-03-06 13:52:48 -080037 private static final String DEFERRED = "deferred";
Jonathan Hartaa178db2017-04-19 17:11:48 -040038 private static final String CLEAR_DEFERRED = "clearDeferred";
Ray Milkeyd43fe452015-05-29 09:35:12 -070039
Ray Milkey4f5de002014-12-17 19:26:11 -080040 @Override
41 public ObjectNode encode(TrafficTreatment treatment, CodecContext context) {
42 checkNotNull(treatment, "Traffic treatment cannot be null");
43
44 final ObjectNode result = context.mapper().createObjectNode();
Ray Milkeyd43fe452015-05-29 09:35:12 -070045 final ArrayNode jsonInstructions = result.putArray(INSTRUCTIONS);
Ray Milkey4f5de002014-12-17 19:26:11 -080046
Ray Milkey42507352015-03-20 15:16:10 -070047 final JsonCodec<Instruction> instructionCodec =
48 context.codec(Instruction.class);
49
50 for (final Instruction instruction : treatment.immediate()) {
51 jsonInstructions.add(instructionCodec.encode(instruction, context));
52 }
53
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -050054 if (treatment.metered() != null) {
cansu.toprak409289d2017-10-27 10:04:05 +030055 for (Instructions.MeterInstruction instruction : treatment.meters()) {
56 jsonInstructions.add(instructionCodec.encode(instruction, context));
57 }
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -050058 }
59 if (treatment.tableTransition() != null) {
60 jsonInstructions.add(instructionCodec.encode(treatment.tableTransition(), context));
61 }
Jonathan Hartaa178db2017-04-19 17:11:48 -040062 if (treatment.clearedDeferred()) {
63 result.put(CLEAR_DEFERRED, true);
64 }
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -050065
Jonathan Hartaa178db2017-04-19 17:11:48 -040066 final ArrayNode jsonDeferred = result.putArray(DEFERRED);
Ray Milkey42507352015-03-20 15:16:10 -070067
68 for (final Instruction instruction : treatment.deferred()) {
69 jsonDeferred.add(instructionCodec.encode(instruction, context));
Ray Milkey4f5de002014-12-17 19:26:11 -080070 }
71
72 return result;
73 }
Ray Milkeyd43fe452015-05-29 09:35:12 -070074
75 @Override
76 public TrafficTreatment decode(ObjectNode json, CodecContext context) {
77 final JsonCodec<Instruction> instructionsCodec =
78 context.codec(Instruction.class);
79
80 JsonNode instructionsJson = json.get(INSTRUCTIONS);
81 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
82 if (instructionsJson != null) {
83 IntStream.range(0, instructionsJson.size())
84 .forEach(i -> builder.add(
Ray Milkeyb82c42b2015-06-30 09:42:20 -070085 instructionsCodec.decode(get(instructionsJson, i),
Ray Milkeyd43fe452015-05-29 09:35:12 -070086 context)));
87 }
Saurav Das1bec2be2017-03-06 13:52:48 -080088
Jonathan Hartaa178db2017-04-19 17:11:48 -040089 JsonNode clearDeferred = json.get(CLEAR_DEFERRED);
90 if (clearDeferred != null && clearDeferred.asBoolean(false)) {
91 builder.wipeDeferred();
92 }
93
Saurav Das1bec2be2017-03-06 13:52:48 -080094 JsonNode deferredJson = json.get(DEFERRED);
95 if (deferredJson != null) {
96 IntStream.range(0, deferredJson.size())
97 .forEach(i -> builder.deferred().add(
98 instructionsCodec.decode(get(deferredJson, i),
99 context)));
100 }
101
Ray Milkeyd43fe452015-05-29 09:35:12 -0700102 return builder.build();
103 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800104}