blob: 69276808e1b8b75f4338df370ccebcd741912c9b [file] [log] [blame]
Ray Milkeyd43fe452015-05-29 09:35:12 -07001/*
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.onosproject.codec.CodecContext;
19import org.onosproject.net.OchSignal;
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
30/**
31 * JSON encoding of Instructions.
32 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070033public final class EncodeInstructionCodecHelper {
34 protected static final Logger log = LoggerFactory.getLogger(EncodeInstructionCodecHelper.class);
Ray Milkeyd43fe452015-05-29 09:35:12 -070035 private final Instruction instruction;
36 private final CodecContext context;
37
38 /**
39 * Creates an instruction object encoder.
40 *
41 * @param instruction instruction to encode
42 * @param context codec context for the encoding
43 */
Ray Milkey6d7968e2015-07-06 14:30:02 -070044 public EncodeInstructionCodecHelper(Instruction instruction, CodecContext context) {
Ray Milkeyd43fe452015-05-29 09:35:12 -070045 this.instruction = instruction;
46 this.context = context;
47 }
48
49
50 /**
51 * Encode an L0 modification instruction.
52 *
53 * @param result json node that the instruction attributes are added to
54 */
55 private void encodeL0(ObjectNode result) {
56 L0ModificationInstruction instruction =
57 (L0ModificationInstruction) this.instruction;
58 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
59
60 switch (instruction.subtype()) {
61 case LAMBDA:
62 final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
63 (L0ModificationInstruction.ModLambdaInstruction) instruction;
64 result.put(InstructionCodec.LAMBDA, modLambdaInstruction.lambda());
65 break;
66
67 case OCH:
68 L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction =
69 (L0ModificationInstruction.ModOchSignalInstruction) instruction;
70 OchSignal ochSignal = ochSignalInstruction.lambda();
71 result.put(InstructionCodec.GRID_TYPE, ochSignal.gridType().name());
72 result.put(InstructionCodec.CHANNEL_SPACING, ochSignal.channelSpacing().name());
73 result.put(InstructionCodec.SPACING_MULTIPLIER, ochSignal.spacingMultiplier());
74 result.put(InstructionCodec.SLOT_GRANULARITY, ochSignal.slotGranularity());
75 break;
76
77 default:
78 log.info("Cannot convert L0 subtype of {}", instruction.subtype());
79 }
80 }
81
82 /**
83 * Encode an L2 modification instruction.
84 *
85 * @param result json node that the instruction attributes are added to
86 */
87 private void encodeL2(ObjectNode result) {
88 L2ModificationInstruction instruction =
89 (L2ModificationInstruction) this.instruction;
90 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
91
92 switch (instruction.subtype()) {
93 case ETH_SRC:
94 case ETH_DST:
95 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
96 (L2ModificationInstruction.ModEtherInstruction) instruction;
97 result.put(InstructionCodec.MAC, modEtherInstruction.mac().toString());
98 break;
99
100 case VLAN_ID:
101 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
102 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
103 result.put(InstructionCodec.VLAN_ID, modVlanIdInstruction.vlanId().toShort());
104 break;
105
106 case VLAN_PCP:
107 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
108 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
109 result.put(InstructionCodec.VLAN_PCP, modVlanPcpInstruction.vlanPcp());
110 break;
111
112 case MPLS_LABEL:
113 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
114 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
115 result.put(InstructionCodec.MPLS_LABEL, modMplsLabelInstruction.label());
116 break;
117
118 case MPLS_PUSH:
119 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
120 (L2ModificationInstruction.PushHeaderInstructions) instruction;
121
alshabib7b808c52015-06-26 14:22:24 -0700122 result.put(InstructionCodec.ETHERNET_TYPE,
123 pushHeaderInstructions.ethernetType().toShort());
Ray Milkeyd43fe452015-05-29 09:35:12 -0700124 break;
125
Hyunsun Moon7080a0d2015-08-14 19:18:48 -0700126 case TUNNEL_ID:
127 final L2ModificationInstruction.ModTunnelIdInstruction modTunnelIdInstruction =
128 (L2ModificationInstruction.ModTunnelIdInstruction) instruction;
129 result.put(InstructionCodec.TUNNEL_ID, modTunnelIdInstruction.tunnelId());
130 break;
131
Ray Milkeyd43fe452015-05-29 09:35:12 -0700132 default:
133 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
134 break;
135 }
136 }
137
138 /**
139 * Encode an L3 modification instruction.
140 *
141 * @param result json node that the instruction attributes are added to
142 */
143 private void encodeL3(ObjectNode result) {
144 L3ModificationInstruction instruction =
145 (L3ModificationInstruction) this.instruction;
146 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
147 switch (instruction.subtype()) {
148 case IPV4_SRC:
149 case IPV4_DST:
150 case IPV6_SRC:
151 case IPV6_DST:
152 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
153 (L3ModificationInstruction.ModIPInstruction) instruction;
154 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
155 break;
156
157 case IPV6_FLABEL:
158 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
159 modFlowLabelInstruction =
160 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction;
161 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
162 break;
163
164 default:
165 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
166 break;
167 }
168 }
169
170 /**
171 * Encodes the given instruction into JSON.
172 *
173 * @return JSON object node representing the instruction
174 */
175 public ObjectNode encode() {
176 final ObjectNode result = context.mapper().createObjectNode()
177 .put(InstructionCodec.TYPE, instruction.type().toString());
178
179 switch (instruction.type()) {
180 case OUTPUT:
181 final Instructions.OutputInstruction outputInstruction =
182 (Instructions.OutputInstruction) instruction;
183 result.put(InstructionCodec.PORT, outputInstruction.port().toLong());
184 break;
185
186 case DROP:
187 break;
188
189 case L0MODIFICATION:
190 encodeL0(result);
191 break;
192
193 case L2MODIFICATION:
194 encodeL2(result);
195 break;
196
197 case L3MODIFICATION:
198 encodeL3(result);
199 break;
200
201 default:
202 log.info("Cannot convert instruction type of {}", instruction.type());
203 break;
204 }
205 return result;
206 }
207
208}