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