blob: 098aef2a77d1ae0a4add37b7b5486e6d2c6a95df [file] [log] [blame]
Ray Milkey4f5de002014-12-17 19:26:11 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
26
Jonathan Hartaa178db2017-04-19 17:11:48 -040027import java.util.stream.IntStream;
Ray Milkey4f5de002014-12-17 19:26:11 -080028
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Traffic treatment codec.
33 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080034public final class TrafficTreatmentCodec extends JsonCodec<TrafficTreatment> {
Ray Milkeyd43fe452015-05-29 09:35:12 -070035 private static final String INSTRUCTIONS = "instructions";
Saurav Das1bec2be2017-03-06 13:52:48 -080036 private static final String DEFERRED = "deferred";
Jonathan Hartaa178db2017-04-19 17:11:48 -040037 private static final String CLEAR_DEFERRED = "clearDeferred";
Ray Milkeyd43fe452015-05-29 09:35:12 -070038
Ray Milkey4f5de002014-12-17 19:26:11 -080039 @Override
40 public ObjectNode encode(TrafficTreatment treatment, CodecContext context) {
41 checkNotNull(treatment, "Traffic treatment cannot be null");
42
43 final ObjectNode result = context.mapper().createObjectNode();
Ray Milkeyd43fe452015-05-29 09:35:12 -070044 final ArrayNode jsonInstructions = result.putArray(INSTRUCTIONS);
Ray Milkey4f5de002014-12-17 19:26:11 -080045
Ray Milkey42507352015-03-20 15:16:10 -070046 final JsonCodec<Instruction> instructionCodec =
47 context.codec(Instruction.class);
48
49 for (final Instruction instruction : treatment.immediate()) {
50 jsonInstructions.add(instructionCodec.encode(instruction, context));
51 }
52
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -050053 if (treatment.metered() != null) {
54 jsonInstructions.add(instructionCodec.encode(treatment.metered(), context));
55 }
56 if (treatment.tableTransition() != null) {
57 jsonInstructions.add(instructionCodec.encode(treatment.tableTransition(), context));
58 }
Jonathan Hartaa178db2017-04-19 17:11:48 -040059 if (treatment.clearedDeferred()) {
60 result.put(CLEAR_DEFERRED, true);
61 }
Konstantinos Kanonakisc5d93e62016-04-12 18:51:20 -050062
Jonathan Hartaa178db2017-04-19 17:11:48 -040063 final ArrayNode jsonDeferred = result.putArray(DEFERRED);
Ray Milkey42507352015-03-20 15:16:10 -070064
65 for (final Instruction instruction : treatment.deferred()) {
66 jsonDeferred.add(instructionCodec.encode(instruction, context));
Ray Milkey4f5de002014-12-17 19:26:11 -080067 }
68
69 return result;
70 }
Ray Milkeyd43fe452015-05-29 09:35:12 -070071
72 @Override
73 public TrafficTreatment decode(ObjectNode json, CodecContext context) {
74 final JsonCodec<Instruction> instructionsCodec =
75 context.codec(Instruction.class);
76
77 JsonNode instructionsJson = json.get(INSTRUCTIONS);
78 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
79 if (instructionsJson != null) {
80 IntStream.range(0, instructionsJson.size())
81 .forEach(i -> builder.add(
Ray Milkeyb82c42b2015-06-30 09:42:20 -070082 instructionsCodec.decode(get(instructionsJson, i),
Ray Milkeyd43fe452015-05-29 09:35:12 -070083 context)));
84 }
Saurav Das1bec2be2017-03-06 13:52:48 -080085
Jonathan Hartaa178db2017-04-19 17:11:48 -040086 JsonNode clearDeferred = json.get(CLEAR_DEFERRED);
87 if (clearDeferred != null && clearDeferred.asBoolean(false)) {
88 builder.wipeDeferred();
89 }
90
Saurav Das1bec2be2017-03-06 13:52:48 -080091 JsonNode deferredJson = json.get(DEFERRED);
92 if (deferredJson != null) {
93 IntStream.range(0, deferredJson.size())
94 .forEach(i -> builder.deferred().add(
95 instructionsCodec.decode(get(deferredJson, i),
96 context)));
97 }
98
Ray Milkeyd43fe452015-05-29 09:35:12 -070099 return builder.build();
100 }
Ray Milkey4f5de002014-12-17 19:26:11 -0800101}