blob: 4a9d0ff252277ccc68e06a58701bbc35db9295e9 [file] [log] [blame]
Charles Chan14967c22015-12-07 11:11:50 -08001/*
2 * Copyright 2016 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
17package org.onosproject.driver.extensions;
18
19import org.onlab.packet.VlanId;
20import org.onosproject.net.behaviour.ExtensionSelectorResolver;
21import org.onosproject.net.driver.AbstractHandlerBehaviour;
22import org.onosproject.net.flow.criteria.ExtensionSelector;
23import org.onosproject.net.flow.criteria.ExtensionSelectorType;
24import org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes;
25import org.onosproject.openflow.controller.ExtensionSelectorInterpreter;
26import org.projectfloodlight.openflow.protocol.OFFactory;
27import org.projectfloodlight.openflow.protocol.match.MatchField;
28import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
29import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
30import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVidMasked;
31import org.projectfloodlight.openflow.types.OFVlanVidMatch;
32import org.projectfloodlight.openflow.types.VlanVid;
33
34/**
35 * Interpreter for OFDPA OpenFlow selector extensions.
36 */
37public 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
54 if (vlanId.toShort() == 0x0000) {
55 OFVlanVidMatch vid = OFVlanVidMatch.ofRawVid(vlanId.toShort());
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);
61 } else if (vlanId.equals(VlanId.NONE)) {
62 return factory.oxms().vlanVid(OFVlanVidMatch.NONE);
63 } else {
64 return factory.oxms().vlanVid(OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vlanId.toShort())));
65 }
66 }
67 throw new UnsupportedOperationException(
68 "Unexpected ExtensionSelector: " + extensionSelector.toString());
69 }
70
71 @Override
72 public ExtensionSelector mapOxm(OFOxm<?> oxm) {
73 VlanId vlanId;
74
75 if (oxm.getMatchField().equals(MatchField.VLAN_VID)) {
76 if (oxm.isMasked()) {
77 OFVlanVidMatch vid = ((OFOxmVlanVidMasked) oxm).getValue();
78 OFVlanVidMatch mask = ((OFOxmVlanVidMasked) oxm).getMask();
79
80 if (vid.equals(OFVlanVidMatch.ofRawVid((short) 0))) {
81 vlanId = VlanId.vlanId((short) 0);
82 } else if (vid.equals(OFVlanVidMatch.PRESENT) &&
83 mask.equals(OFVlanVidMatch.PRESENT)) {
84 vlanId = VlanId.ANY;
85 } else {
86 vlanId = VlanId.vlanId(vid.getVlan());
87 }
88 } else {
89 OFVlanVidMatch vid = ((OFOxmVlanVid) oxm).getValue();
90
91 if (!vid.isPresentBitSet()) {
92 vlanId = VlanId.NONE;
93 } else {
94 vlanId = VlanId.vlanId(vid.getVlan());
95 }
96 }
97 return new OfdpaMatchVlanVid(vlanId);
98 }
99 throw new UnsupportedOperationException(
100 "Unexpected OXM: " + oxm.toString());
101 }
102
103 @Override
104 public ExtensionSelector getExtensionSelector(ExtensionSelectorType type) {
105 if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
106 return new OfdpaMatchVlanVid();
107 }
108 throw new UnsupportedOperationException(
109 "Driver does not support extension type " + type.toString());
110 }
111}