blob: 913fc8b42b29a8fa4813a5676a3cfb8ef979259c [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;
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
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Instruction codec.
34 */
Ray Milkey540b2ce2015-02-04 17:50:20 -080035public final class InstructionCodec extends JsonCodec<Instruction> {
Ray Milkeyc95bb9d2015-01-06 10:28:24 -080036
37 protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class);
38
39 /**
40 * Encode an L0 modification instruction.
41 *
42 * @param result json node that the instruction attributes are added to
43 * @param instruction The L0 instruction
44 */
45 private void encodeL0(ObjectNode result, L0ModificationInstruction instruction) {
46 result.put("subtype", instruction.subtype().name());
47
48 switch (instruction.subtype()) {
49 case LAMBDA:
50 final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction =
51 (L0ModificationInstruction.ModLambdaInstruction) instruction;
52 result.put("lambda", modLambdaInstruction.lambda());
53 break;
54
55 default:
56 log.info("Cannot convert L0 subtype of {}", instruction.subtype());
57 }
58 }
59
60 /**
61 * Encode an L2 modification instruction.
62 *
63 * @param result json node that the instruction attributes are added to
64 * @param instruction The L2 instruction
65 * @param context context of the request
66 */
67 private void encodeL2(ObjectNode result,
68 L2ModificationInstruction instruction,
69 CodecContext context) {
70 result.put("subtype", instruction.subtype().name());
71
72 switch (instruction.subtype()) {
73 case ETH_SRC:
74 case ETH_DST:
75 final L2ModificationInstruction.ModEtherInstruction modEtherInstruction =
76 (L2ModificationInstruction.ModEtherInstruction) instruction;
77 result.put("mac", modEtherInstruction.mac().toString());
78 break;
79
80 case VLAN_ID:
81 final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction =
82 (L2ModificationInstruction.ModVlanIdInstruction) instruction;
83 result.put("vlanId", modVlanIdInstruction.vlanId().toShort());
84 break;
85
86 case VLAN_PCP:
87 final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction =
88 (L2ModificationInstruction.ModVlanPcpInstruction) instruction;
89 result.put("vlanPcp", modVlanPcpInstruction.vlanPcp());
90 break;
91
92 case MPLS_LABEL:
93 final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction =
94 (L2ModificationInstruction.ModMplsLabelInstruction) instruction;
95 result.put("label", modMplsLabelInstruction.label());
96 break;
97
98 case MPLS_PUSH:
99 final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions =
100 (L2ModificationInstruction.PushHeaderInstructions) instruction;
101
Yuta HIGUCHI32a53c52015-02-08 01:25:40 -0800102 result.put("ethernetType", pushHeaderInstructions.ethernetType());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800103 break;
104
105 default:
106 log.info("Cannot convert L2 subtype of {}", instruction.subtype());
107 break;
108 }
109 }
110
111 /**
112 * Encode an L3 modification instruction.
113 *
114 * @param result json node that the instruction attributes are added to
115 * @param instruction The L3 instruction
116 */
117 private void encodeL3(ObjectNode result, L3ModificationInstruction instruction) {
118 result.put("subtype", instruction.subtype().name());
119 switch (instruction.subtype()) {
120 case IP_SRC:
121 case IP_DST:
122 final L3ModificationInstruction.ModIPInstruction modIPInstruction =
123 (L3ModificationInstruction.ModIPInstruction) instruction;
124 result.put("ip", modIPInstruction.ip().toString());
125 break;
126
127 default:
128 log.info("Cannot convert L3 subtype of {}", instruction.subtype());
129 break;
130 }
131 }
132
133 @Override
134 public ObjectNode encode(Instruction instruction, CodecContext context) {
135 checkNotNull(instruction, "Instruction cannot be null");
136
137 final ObjectNode result = context.mapper().createObjectNode()
138 .put("type", instruction.type().toString());
139
140
141 switch (instruction.type()) {
142 case OUTPUT:
143 final Instructions.OutputInstruction outputInstruction =
144 (Instructions.OutputInstruction) instruction;
Ray Milkeyd03eda02015-01-09 14:58:48 -0800145 result.put("port", outputInstruction.port().toLong());
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800146 break;
147
148 case DROP:
149 break;
150
151 case L0MODIFICATION:
152 final L0ModificationInstruction l0ModificationInstruction =
153 (L0ModificationInstruction) instruction;
154 encodeL0(result, l0ModificationInstruction);
155 break;
156
157 case L2MODIFICATION:
158 final L2ModificationInstruction l2ModificationInstruction =
159 (L2ModificationInstruction) instruction;
160 encodeL2(result, l2ModificationInstruction, context);
161 break;
162
163 case L3MODIFICATION:
164 final L3ModificationInstruction l3ModificationInstruction =
165 (L3ModificationInstruction) instruction;
166 encodeL3(result, l3ModificationInstruction);
Ray Milkey4c0da812015-02-02 12:03:44 -0800167 break;
Ray Milkeyc95bb9d2015-01-06 10:28:24 -0800168
169 default:
170 log.info("Cannot convert instruction type of {}", instruction.type());
171 break;
172 }
173 return result;
174 }
175}