blob: ef8968ce9eff3a2e9b597bddf1e7dd8df6143175 [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;
Sho SHIMIZUed7af542015-05-05 19:10:45 -070020import org.onosproject.net.OchSignal;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080021import 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
Sho SHIMIZUed7af542015-05-05 19:10:45 -070056 case OCH:
57 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
58 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
59 OchSignal ochSignal = ochSignalInstruction.lambda();
60 result.put("gridType", ochSignal.gridType().name());
61 result.put("channelSpacing", ochSignal.channelSpacing().name());
62 result.put("spacingMultiplier", ochSignal.spacingMultiplier());
63 result.put("slotGranularity", ochSignal.slotGranularity());
64 break;
65
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080066 default:
67 log.info("Cannot convert L0 subtype of {}", instruction.subtype());
68 }
69 }
70
71 /**
72 * Encode an L2 modification instruction.
73 *
74 * @param result json node that the instruction attributes are added to
75 * @param instruction The L2 instruction
76 * @param context context of the request
77 */
78 private void encodeL2(ObjectNode result,
79 L2ModificationInstruction instruction,
80 CodecContext context) {
81 result.put("subtype", instruction.subtype().name());
82
83 switch (instruction.subtype()) {
84 case ETH_SRC:
85 case ETH_DST:
86 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
87 (L2ModificationInstruction.ModEtherInstruction) instruction;
88 result.put("mac", modEtherInstruction.mac().toString());
89 break;
90
91 case VLAN_ID:
92 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
93 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
94 result.put("vlanId", modVlanIdInstruction.vlanId().toShort());
95 break;
96
97 case VLAN_PCP:
98 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
99 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
100 result.put("vlanPcp", modVlanPcpInstruction.vlanPcp());
101 break;
102
103 case MPLS_LABEL:
104 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
105 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
106 result.put("label", modMplsLabelInstruction.label());
107 break;
108
109 case MPLS_PUSH:
110 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
111 (L2ModificationInstruction.PushHeaderInstructions) instruction;
112
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800113 result.put("ethernetType", pushHeaderInstructions.ethernetType());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800114 break;
115
116 default:
117 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
118 break;
119 }
120 }
121
122 /**
123 * Encode an L3 modification instruction.
124 *
125 * @param result json node that the instruction attributes are added to
126 * @param instruction The L3 instruction
127 */
128 private void encodeL3(ObjectNode result, L3ModificationInstruction instruction) {
129 result.put("subtype", instruction.subtype().name());
130 switch (instruction.subtype()) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800131 case IPV4_SRC:
132 case IPV4_DST:
133 case IPV6_SRC:
134 case IPV6_DST:
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800135 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
136 (L3ModificationInstruction.ModIPInstruction) instruction;
137 result.put("ip", modIPInstruction.ip().toString());
138 break;
139
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800140 case IPV6_FLABEL:
141 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
142 modFlowLabelInstruction =
143 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction;
144 result.put("flowLabel", modFlowLabelInstruction.flowLabel());
145 break;
146
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800147 default:
148 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
149 break;
150 }
151 }
152
153 @Override
154 public ObjectNode encode(Instruction instruction, CodecContext context) {
155 checkNotNull(instruction, "Instruction cannot be null");
156
157 final ObjectNode result = context.mapper().createObjectNode()
158 .put("type", instruction.type().toString());
159
160
161 switch (instruction.type()) {
162 case OUTPUT:
163 final Instructions.OutputInstruction outputInstruction =
164 (Instructions.OutputInstruction) instruction;
Ray Milkeyd03eda02015-01-09 14:58:48 -0800165 result.put("port", outputInstruction.port().toLong());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800166 break;
167
168 case DROP:
169 break;
170
171 case L0MODIFICATION:
172 final L0ModificationInstruction l0ModificationInstruction =
173 (L0ModificationInstruction) instruction;
174 encodeL0(result, l0ModificationInstruction);
175 break;
176
177 case L2MODIFICATION:
178 final L2ModificationInstruction l2ModificationInstruction =
179 (L2ModificationInstruction) instruction;
180 encodeL2(result, l2ModificationInstruction, context);
181 break;
182
183 case L3MODIFICATION:
184 final L3ModificationInstruction l3ModificationInstruction =
185 (L3ModificationInstruction) instruction;
186 encodeL3(result, l3ModificationInstruction);
Ray Milkey4c0da812015-02-02 12:03:44 -0800187 break;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800188
189 default:
190 log.info("Cannot convert instruction type of {}", instruction.type());
191 break;
192 }
193 return result;
194 }
195}