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; |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 20 | import org.onosproject.net.OchSignal; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 21 | import org.onosproject.net.flow.instructions.Instruction; |
| 22 | import org.onosproject.net.flow.instructions.Instructions; |
| 23 | import org.onosproject.net.flow.instructions.L0ModificationInstruction; |
| 24 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 25 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * Instruction codec. |
| 35 | */ |
Ray Milkey | 540b2ce | 2015-02-04 17:50:20 -0800 | [diff] [blame] | 36 | public final class InstructionCodec extends JsonCodec<Instruction> { |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 37 | |
| 38 | protected static final Logger log = LoggerFactory.getLogger(InstructionCodec.class); |
| 39 | |
| 40 | /** |
| 41 | * Encode an L0 modification instruction. |
| 42 | * |
| 43 | * @param result json node that the instruction attributes are added to |
| 44 | * @param instruction The L0 instruction |
| 45 | */ |
| 46 | private void encodeL0(ObjectNode result, L0ModificationInstruction instruction) { |
| 47 | result.put("subtype", instruction.subtype().name()); |
| 48 | |
| 49 | switch (instruction.subtype()) { |
| 50 | case LAMBDA: |
| 51 | final L0ModificationInstruction.ModLambdaInstruction modLambdaInstruction = |
| 52 | (L0ModificationInstruction.ModLambdaInstruction) instruction; |
| 53 | result.put("lambda", modLambdaInstruction.lambda()); |
| 54 | break; |
| 55 | |
Sho SHIMIZU | ed7af54 | 2015-05-05 19:10:45 -0700 | [diff] [blame] | 56 | case OCH: |
| 57 | L0ModificationInstruction.ModOchSignalInstruction ochSignalInstruction = |
| 58 | (L0ModificationInstruction.ModOchSignalInstruction) instruction; |
| 59 | OchSignal ochSignal = ochSignalInstruction.lambda(); |
| 60 | result.put("gridType", ochSignal.gridType().name()); |
| 61 | result.put("channelSpacing", ochSignal.channelSpacing().name()); |
| 62 | result.put("spacingMultiplier", ochSignal.spacingMultiplier()); |
| 63 | result.put("slotGranularity", ochSignal.slotGranularity()); |
| 64 | break; |
| 65 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 66 | default: |
| 67 | log.info("Cannot convert L0 subtype of {}", instruction.subtype()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Encode an L2 modification instruction. |
| 73 | * |
| 74 | * @param result json node that the instruction attributes are added to |
| 75 | * @param instruction The L2 instruction |
| 76 | * @param context context of the request |
| 77 | */ |
| 78 | private void encodeL2(ObjectNode result, |
| 79 | L2ModificationInstruction instruction, |
| 80 | CodecContext context) { |
| 81 | result.put("subtype", instruction.subtype().name()); |
| 82 | |
| 83 | switch (instruction.subtype()) { |
| 84 | case ETH_SRC: |
| 85 | case ETH_DST: |
| 86 | final L2ModificationInstruction.ModEtherInstruction modEtherInstruction = |
| 87 | (L2ModificationInstruction.ModEtherInstruction) instruction; |
| 88 | result.put("mac", modEtherInstruction.mac().toString()); |
| 89 | break; |
| 90 | |
| 91 | case VLAN_ID: |
| 92 | final L2ModificationInstruction.ModVlanIdInstruction modVlanIdInstruction = |
| 93 | (L2ModificationInstruction.ModVlanIdInstruction) instruction; |
| 94 | result.put("vlanId", modVlanIdInstruction.vlanId().toShort()); |
| 95 | break; |
| 96 | |
| 97 | case VLAN_PCP: |
| 98 | final L2ModificationInstruction.ModVlanPcpInstruction modVlanPcpInstruction = |
| 99 | (L2ModificationInstruction.ModVlanPcpInstruction) instruction; |
| 100 | result.put("vlanPcp", modVlanPcpInstruction.vlanPcp()); |
| 101 | break; |
| 102 | |
| 103 | case MPLS_LABEL: |
| 104 | final L2ModificationInstruction.ModMplsLabelInstruction modMplsLabelInstruction = |
| 105 | (L2ModificationInstruction.ModMplsLabelInstruction) instruction; |
| 106 | result.put("label", modMplsLabelInstruction.label()); |
| 107 | break; |
| 108 | |
| 109 | case MPLS_PUSH: |
| 110 | final L2ModificationInstruction.PushHeaderInstructions pushHeaderInstructions = |
| 111 | (L2ModificationInstruction.PushHeaderInstructions) instruction; |
| 112 | |
Yuta HIGUCHI | 32a53c5 | 2015-02-08 01:25:40 -0800 | [diff] [blame] | 113 | result.put("ethernetType", pushHeaderInstructions.ethernetType()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 114 | break; |
| 115 | |
| 116 | default: |
| 117 | log.info("Cannot convert L2 subtype of {}", instruction.subtype()); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Encode an L3 modification instruction. |
| 124 | * |
| 125 | * @param result json node that the instruction attributes are added to |
| 126 | * @param instruction The L3 instruction |
| 127 | */ |
| 128 | private void encodeL3(ObjectNode result, L3ModificationInstruction instruction) { |
| 129 | result.put("subtype", instruction.subtype().name()); |
| 130 | switch (instruction.subtype()) { |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 131 | case IPV4_SRC: |
| 132 | case IPV4_DST: |
| 133 | case IPV6_SRC: |
| 134 | case IPV6_DST: |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 135 | final L3ModificationInstruction.ModIPInstruction modIPInstruction = |
| 136 | (L3ModificationInstruction.ModIPInstruction) instruction; |
| 137 | result.put("ip", modIPInstruction.ip().toString()); |
| 138 | break; |
| 139 | |
Pavlin Radoslavov | febe82c | 2015-02-11 19:08:15 -0800 | [diff] [blame] | 140 | case IPV6_FLABEL: |
| 141 | final L3ModificationInstruction.ModIPv6FlowLabelInstruction |
| 142 | modFlowLabelInstruction = |
| 143 | (L3ModificationInstruction.ModIPv6FlowLabelInstruction) instruction; |
| 144 | result.put("flowLabel", modFlowLabelInstruction.flowLabel()); |
| 145 | break; |
| 146 | |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 147 | default: |
| 148 | log.info("Cannot convert L3 subtype of {}", instruction.subtype()); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public ObjectNode encode(Instruction instruction, CodecContext context) { |
| 155 | checkNotNull(instruction, "Instruction cannot be null"); |
| 156 | |
| 157 | final ObjectNode result = context.mapper().createObjectNode() |
| 158 | .put("type", instruction.type().toString()); |
| 159 | |
| 160 | |
| 161 | switch (instruction.type()) { |
| 162 | case OUTPUT: |
| 163 | final Instructions.OutputInstruction outputInstruction = |
| 164 | (Instructions.OutputInstruction) instruction; |
Ray Milkey | d03eda0 | 2015-01-09 14:58:48 -0800 | [diff] [blame] | 165 | result.put("port", outputInstruction.port().toLong()); |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | case DROP: |
| 169 | break; |
| 170 | |
| 171 | case L0MODIFICATION: |
| 172 | final L0ModificationInstruction l0ModificationInstruction = |
| 173 | (L0ModificationInstruction) instruction; |
| 174 | encodeL0(result, l0ModificationInstruction); |
| 175 | break; |
| 176 | |
| 177 | case L2MODIFICATION: |
| 178 | final L2ModificationInstruction l2ModificationInstruction = |
| 179 | (L2ModificationInstruction) instruction; |
| 180 | encodeL2(result, l2ModificationInstruction, context); |
| 181 | break; |
| 182 | |
| 183 | case L3MODIFICATION: |
| 184 | final L3ModificationInstruction l3ModificationInstruction = |
| 185 | (L3ModificationInstruction) instruction; |
| 186 | encodeL3(result, l3ModificationInstruction); |
Ray Milkey | 4c0da81 | 2015-02-02 12:03:44 -0800 | [diff] [blame] | 187 | break; |
Ray Milkey | c95bb9d | 2015-01-06 10:28:24 -0800 | [diff] [blame] | 188 | |
| 189 | default: |
| 190 | log.info("Cannot convert instruction type of {}", instruction.type()); |
| 191 | break; |
| 192 | } |
| 193 | return result; |
| 194 | } |
| 195 | } |