2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.codec.impl;
18 import org.onosproject.codec.CodecContext;
19 import org.onosproject.net.OchSignal;
20 import org.onosproject.net.flow.instructions.Instruction;
21 import org.onosproject.net.flow.instructions.Instructions;
22 import org.onosproject.net.flow.instructions.L0ModificationInstruction;
23 import org.onosproject.net.flow.instructions.L2ModificationInstruction;
24 import org.onosproject.net.flow.instructions.L3ModificationInstruction;
25 import org.onosproject.net.flow.instructions.L4ModificationInstruction;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
32 * JSON encoding of Instructions.
34 public final class EncodeInstructionCodecHelper {
35 protected static final Logger log = LoggerFactory.getLogger(EncodeInstructionCodecHelper.class);
36 private final Instruction instruction;
37 private final CodecContext context;
40 * Creates an instruction object encoder.
42 * @param instruction instruction to encode
43 * @param context codec context for the encoding
45 public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) {
46 this.instruction = instruction;
47 this.context = context;
52 * Encode an L0 modification instruction.
54 * @param result json node that the instruction attributes are added to
56 private void encodeL0(ObjectNode result) {
57 L0ModificationInstruction instruction =
58 (L0ModificationInstruction) this.instruction;
59 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
61 switch (instruction.subtype()) {
63 final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
64 (L0ModificationInstruction.ModLambdaInstruction) instruction;
65 result.put(InstructionCodec.LAMBDA, modLambdaInstruction.lambda());
69 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
70 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
71 OchSignal ochSignal = ochSignalInstruction.lambda();
72 result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name());
73 result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
74 result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier());
75 result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
79 log.info("Cannot convert L0 subtype of {}", instruction.subtype());
84 * Encode an L2 modification instruction.
86 * @param result json node that the instruction attributes are added to
88 private void encodeL2(ObjectNode result) {
89 L2ModificationInstruction instruction =
90 (L2ModificationInstruction) this.instruction;
91 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
93 switch (instruction.subtype()) {
96 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
97 (L2ModificationInstruction.ModEtherInstruction) instruction;
98 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
102 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
103 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
104 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
108 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
109 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
110 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
114 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
115 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
116 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.mplsLabel().toInt());
120 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
121 (L2ModificationInstruction.PushHeaderInstructions) instruction;
123 result.put(InstructionCodec.ETHERNET_TYPE,
124 pushHeaderInstructions.ethernetType().toShort());
128 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
129 (L2ModificationInstruction.ModTunnelIdInstruction) instruction;
130 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
134 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
140 * Encode an L3 modification instruction.
142 * @param result json node that the instruction attributes are added to
144 private void encodeL3(ObjectNode result) {
145 L3ModificationInstruction instruction =
146 (L3ModificationInstruction) this.instruction;
147 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
148 switch (instruction.subtype()) {
153 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
154 (L3ModificationInstruction.ModIPInstruction) instruction;
155 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
159 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
160 modFlowLabelInstruction =
161 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction;
162 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
166 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
172 * Encode a L4 modification instruction.
174 * @param result json node that the instruction attributes are added to
176 private void encodeL4(ObjectNode result) {
177 L4ModificationInstruction instruction =
178 (L4ModificationInstruction) this.instruction;
179 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
180 switch (instruction.subtype()) {
183 final L4ModificationInstruction.ModTransportPortInstruction modTcpPortInstruction =
184 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
185 result.put(InstructionCodec.TCP_PORT, modTcpPortInstruction.port().toInt());
190 final L4ModificationInstruction.ModTransportPortInstruction modUdpPortInstruction =
191 (L4ModificationInstruction.ModTransportPortInstruction) instruction;
192 result.put(InstructionCodec.UDP_PORT, modUdpPortInstruction.port().toInt());
196 log.info("Cannot convert L4 subtype of {}", instruction.subtype());
202 * Encodes the given instruction into JSON.
204 * @return JSON object node representing the instruction
206 public ObjectNode encode() {
207 final ObjectNode result = context.mapper().createObjectNode()
208 .put(InstructionCodec.TYPE, instruction.type().toString());
210 switch (instruction.type()) {
212 final Instructions.OutputInstruction outputInstruction =
213 (Instructions.OutputInstruction) instruction;
214 result.put(InstructionCodec.PORT, outputInstruction.port().toLong());
238 log.info("Cannot convert instruction type of {}", instruction.type());