Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.codec.impl; |
| 17 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 18 | import org.onosproject.codec.CodecContext; |
| 19 | import org.onosproject.codec.JsonCodec; |
| 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.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Instruction codec. |
| 34 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 35 | public final class InstructionCodec extends JsonCodec<Instruction> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 36 | |
| 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 HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame^] | 102 | result.put("ethernetType", pushHeaderInstructions.ethernetType()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 103 | 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 Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 145 | result.put("port", outputInstruction.port().toLong()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 146 | 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 Milkey | 4c0da81 | 2015-02-02 12:03:44 -0800 | [diff] [blame] | 167 | break; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 168 | |
| 169 | default: |
| 170 | log.info("Cannot convert instruction type of {}", instruction.type()); |
| 171 | break; |
| 172 | } |
| 173 | return result; |
| 174 | } |
| 175 | } |