blob: 20de446205e2d456f91fc0bb2cdb6f486e3a6fc8 [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 */
33public final class EncodeInstructionCodec {
34 protected static final Logger log = LoggerFactory.getLogger(EncodeInstructionCodec.class);
35 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 */
44 public EncodeInstructionCodec(Instruction instruction, CodecContext context) {
45 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
126 default:
127 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
128 break;
129 }
130 }
131
132 /**
133 * Encode an L3 modification instruction.
134 *
135 * @param result json node that the instruction attributes are added to
136 */
137 private void encodeL3(ObjectNode result) {
138 L3ModificationInstruction instruction =
139 (L3ModificationInstruction) this.instruction;
140 result.put(InstructionCodec.SUBTYPE, instruction.subtype().name());
141 switch (instruction.subtype()) {
142 case IPV4_SRC:
143 case IPV4_DST:
144 case IPV6_SRC:
145 case IPV6_DST:
146 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
147 (L3ModificationInstruction.ModIPInstruction) instruction;
148 result.put(InstructionCodec.IP, modIPInstruction.ip().toString());
149 break;
150
151 case IPV6_FLABEL:
152 final L3ModificationInstruction.ModIPv6FlowLabelInstruction
153 modFlowLabelInstruction =
154 (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction;
155 result.put(InstructionCodec.FLOW_LABEL, modFlowLabelInstruction.flowLabel());
156 break;
157
158 default:
159 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
160 break;
161 }
162 }
163
164 /**
165 * Encodes the given instruction into JSON.
166 *
167 * @return JSON object node representing the instruction
168 */
169 public ObjectNode encode() {
170 final ObjectNode result = context.mapper().createObjectNode()
171 .put(InstructionCodec.TYPE, instruction.type().toString());
172
173 switch (instruction.type()) {
174 case OUTPUT:
175 final Instructions.OutputInstruction outputInstruction =
176 (Instructions.OutputInstruction) instruction;
177 result.put(InstructionCodec.PORT, outputInstruction.port().toLong());
178 break;
179
180 case DROP:
181 break;
182
183 case L0MODIFICATION:
184 encodeL0(result);
185 break;
186
187 case L2MODIFICATION:
188 encodeL2(result);
189 break;
190
191 case L3MODIFICATION:
192 encodeL3(result);
193 break;
194
195 default:
196 log.info("Cannot convert instruction type of {}", instruction.type());
197 break;
198 }
199 return result;
200 }
201
202}