blob: 57f95bd6f5ec82b91dd07e15d27ea0d8e6741529 [file] [log] [blame]
Ray Milkeyc95bb9d2015-01-06 10:28:24 -08001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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 org.onlab.packet.Ethernet;
19import org.onosproject.codec.CodecContext;
20import org.onosproject.codec.JsonCodec;
21import org.onosproject.net.flow.instructions.Instruction;
22import org.onosproject.net.flow.instructions.Instructions;
23import org.onosproject.net.flow.instructions.L0ModificationInstruction;
24import org.onosproject.net.flow.instructions.L2ModificationInstruction;
25import org.onosproject.net.flow.instructions.L3ModificationInstruction;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import com.fasterxml.jackson.databind.node.ObjectNode;
30
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Instruction codec.
35 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080036public final class InstructionCodec extends JsonCodec<Instruction> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080037
38 protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class);
39
40 /**
41 * Encode an L0 modification instruction.
42 *
43 * @param result json node that the instruction attributes are added to
44 * @param instruction The L0 instruction
45 */
46 private void encodeL0(ObjectNode result, L0ModificationInstruction instruction) {
47 result.put("subtype", instruction.subtype().name());
48
49 switch (instruction.subtype()) {
50 case LAMBDA:
51 final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
52 (L0ModificationInstruction.ModLambdaInstruction) instruction;
53 result.put("lambda", modLambdaInstruction.lambda());
54 break;
55
56 default:
57 log.info("Cannot convert L0 subtype of {}", instruction.subtype());
58 }
59 }
60
61 /**
62 * Encode an L2 modification instruction.
63 *
64 * @param result json node that the instruction attributes are added to
65 * @param instruction The L2 instruction
66 * @param context context of the request
67 */
68 private void encodeL2(ObjectNode result,
69 L2ModificationInstruction instruction,
70 CodecContext context) {
71 result.put("subtype", instruction.subtype().name());
72
73 switch (instruction.subtype()) {
74 case ETH_SRC:
75 case ETH_DST:
76 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
77 (L2ModificationInstruction.ModEtherInstruction) instruction;
78 result.put("mac", modEtherInstruction.mac().toString());
79 break;
80
81 case VLAN_ID:
82 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
83 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
84 result.put("vlanId", modVlanIdInstruction.vlanId().toShort());
85 break;
86
87 case VLAN_PCP:
88 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
89 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
90 result.put("vlanPcp", modVlanPcpInstruction.vlanPcp());
91 break;
92
93 case MPLS_LABEL:
94 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
95 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
96 result.put("label", modMplsLabelInstruction.label());
97 break;
98
99 case MPLS_PUSH:
100 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
101 (L2ModificationInstruction.PushHeaderInstructions) instruction;
102
103 final JsonCodec<Ethernet> ethernetCodec =
104 context.codec(Ethernet.class);
105 result.set("ethernetType",
106 ethernetCodec.encode(pushHeaderInstructions.ethernetType(),
107 context));
108 break;
109
110 default:
111 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
112 break;
113 }
114 }
115
116 /**
117 * Encode an L3 modification instruction.
118 *
119 * @param result json node that the instruction attributes are added to
120 * @param instruction The L3 instruction
121 */
122 private void encodeL3(ObjectNode result, L3ModificationInstruction instruction) {
123 result.put("subtype", instruction.subtype().name());
124 switch (instruction.subtype()) {
125 case IP_SRC:
126 case IP_DST:
127 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
128 (L3ModificationInstruction.ModIPInstruction) instruction;
129 result.put("ip", modIPInstruction.ip().toString());
130 break;
131
132 default:
133 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
134 break;
135 }
136 }
137
138 @Override
139 public ObjectNode encode(Instruction instruction, CodecContext context) {
140 checkNotNull(instruction, "Instruction cannot be null");
141
142 final ObjectNode result = context.mapper().createObjectNode()
143 .put("type", instruction.type().toString());
144
145
146 switch (instruction.type()) {
147 case OUTPUT:
148 final Instructions.OutputInstruction outputInstruction =
149 (Instructions.OutputInstruction) instruction;
Ray Milkeyd03eda02015-01-09 14:58:48 -0800150 result.put("port", outputInstruction.port().toLong());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800151 break;
152
153 case DROP:
154 break;
155
156 case L0MODIFICATION:
157 final L0ModificationInstruction l0ModificationInstruction =
158 (L0ModificationInstruction) instruction;
159 encodeL0(result, l0ModificationInstruction);
160 break;
161
162 case L2MODIFICATION:
163 final L2ModificationInstruction l2ModificationInstruction =
164 (L2ModificationInstruction) instruction;
165 encodeL2(result, l2ModificationInstruction, context);
166 break;
167
168 case L3MODIFICATION:
169 final L3ModificationInstruction l3ModificationInstruction =
170 (L3ModificationInstruction) instruction;
171 encodeL3(result, l3ModificationInstruction);
Ray Milkey4c0da812015-02-02 12:03:44 -0800172 break;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800173
174 default:
175 log.info("Cannot convert instruction type of {}", instruction.type());
176 break;
177 }
178 return result;
179 }
180}