Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.driver.extensions; |
| 18 | |
| 19 | import org.onlab.packet.VlanId; |
| 20 | import org.onosproject.net.behaviour.ExtensionSelectorResolver; |
| 21 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 22 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
| 23 | import org.onosproject.net.flow.criteria.ExtensionSelectorType; |
| 24 | import org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes; |
| 25 | import org.onosproject.openflow.controller.ExtensionSelectorInterpreter; |
| 26 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 27 | import org.projectfloodlight.openflow.protocol.match.MatchField; |
| 28 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 29 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid; |
| 30 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVidMasked; |
| 31 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
| 32 | import org.projectfloodlight.openflow.types.VlanVid; |
| 33 | |
| 34 | /** |
| 35 | * Interpreter for OFDPA OpenFlow selector extensions. |
| 36 | */ |
| 37 | public class OfdpaExtensionSelectorInterpreter extends AbstractHandlerBehaviour |
| 38 | implements ExtensionSelectorInterpreter, ExtensionSelectorResolver { |
| 39 | |
| 40 | @Override |
| 41 | public boolean supported(ExtensionSelectorType extensionSelectorType) { |
| 42 | if (extensionSelectorType.equals(ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) { |
| 43 | return true; |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public OFOxm<?> mapSelector(OFFactory factory, ExtensionSelector extensionSelector) { |
| 50 | ExtensionSelectorType type = extensionSelector.type(); |
| 51 | if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) { |
| 52 | VlanId vlanId = ((OfdpaMatchVlanVid) extensionSelector).vlanId(); |
| 53 | // Special VLAN 0x0000/0x1FFF required by OFDPA |
Charles Chan | be8aea4 | 2016-02-24 12:04:47 -0800 | [diff] [blame] | 54 | if (vlanId.equals(VlanId.NONE)) { |
| 55 | OFVlanVidMatch vid = OFVlanVidMatch.ofRawVid((short) 0x0000); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 56 | OFVlanVidMatch mask = OFVlanVidMatch.ofRawVid((short) 0x1FFF); |
| 57 | return factory.oxms().vlanVidMasked(vid, mask); |
| 58 | // Normal case |
| 59 | } else if (vlanId.equals(VlanId.ANY)) { |
| 60 | return factory.oxms().vlanVidMasked(OFVlanVidMatch.PRESENT, OFVlanVidMatch.PRESENT); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 61 | } else { |
| 62 | return factory.oxms().vlanVid(OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vlanId.toShort()))); |
| 63 | } |
| 64 | } |
| 65 | throw new UnsupportedOperationException( |
| 66 | "Unexpected ExtensionSelector: " + extensionSelector.toString()); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public ExtensionSelector mapOxm(OFOxm<?> oxm) { |
| 71 | VlanId vlanId; |
| 72 | |
| 73 | if (oxm.getMatchField().equals(MatchField.VLAN_VID)) { |
| 74 | if (oxm.isMasked()) { |
| 75 | OFVlanVidMatch vid = ((OFOxmVlanVidMasked) oxm).getValue(); |
| 76 | OFVlanVidMatch mask = ((OFOxmVlanVidMasked) oxm).getMask(); |
| 77 | |
| 78 | if (vid.equals(OFVlanVidMatch.ofRawVid((short) 0))) { |
Charles Chan | be8aea4 | 2016-02-24 12:04:47 -0800 | [diff] [blame] | 79 | vlanId = VlanId.NONE; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 80 | } else if (vid.equals(OFVlanVidMatch.PRESENT) && |
| 81 | mask.equals(OFVlanVidMatch.PRESENT)) { |
| 82 | vlanId = VlanId.ANY; |
| 83 | } else { |
| 84 | vlanId = VlanId.vlanId(vid.getVlan()); |
| 85 | } |
| 86 | } else { |
| 87 | OFVlanVidMatch vid = ((OFOxmVlanVid) oxm).getValue(); |
| 88 | |
| 89 | if (!vid.isPresentBitSet()) { |
| 90 | vlanId = VlanId.NONE; |
| 91 | } else { |
| 92 | vlanId = VlanId.vlanId(vid.getVlan()); |
| 93 | } |
| 94 | } |
| 95 | return new OfdpaMatchVlanVid(vlanId); |
| 96 | } |
| 97 | throw new UnsupportedOperationException( |
| 98 | "Unexpected OXM: " + oxm.toString()); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public ExtensionSelector getExtensionSelector(ExtensionSelectorType type) { |
| 103 | if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) { |
| 104 | return new OfdpaMatchVlanVid(); |
| 105 | } |
| 106 | throw new UnsupportedOperationException( |
| 107 | "Driver does not support extension type " + type.toString()); |
| 108 | } |
| 109 | } |