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