blob: 05c943c4407bf977a2a83bc40e12cc7814b6fb9c [file] [log] [blame]
Pier Ventre6f630052016-10-18 09:58:41 -07001/*
2 * Copyright 2016-present 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.OFOxmOfdpaOvid;
30import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVid;
31import org.projectfloodlight.openflow.protocol.oxm.OFOxmVlanVidMasked;
32import org.projectfloodlight.openflow.types.OFVlanVidMatch;
33import org.projectfloodlight.openflow.types.U16;
34import org.projectfloodlight.openflow.types.VlanVid;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38/**
39 * Interpreter for OFDPA3 OpenFlow selector extensions.
40 */
41public class Ofdpa3ExtensionSelectorInterpreter extends AbstractHandlerBehaviour
42 implements ExtensionSelectorInterpreter, ExtensionSelectorResolver {
43
44 private final Logger log = LoggerFactory.getLogger(getClass());
45
46 @Override
47 public boolean supported(ExtensionSelectorType extensionSelectorType) {
48 if (extensionSelectorType.equals(ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
49 return true;
50 } else if (extensionSelectorType.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
51 return true;
52 }
53 return false;
54 }
55
56 @Override
57 public OFOxm<?> mapSelector(OFFactory factory, ExtensionSelector extensionSelector) {
58 ExtensionSelectorType type = extensionSelector.type();
59 if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
60 VlanId vlanId = ((OfdpaMatchVlanVid) extensionSelector).vlanId();
61 // Special VLAN 0x0000/0x1FFF required by OFDPA
62 if (vlanId.equals(VlanId.NONE)) {
63 OFVlanVidMatch vid = OFVlanVidMatch.ofRawVid((short) 0x0000);
64 OFVlanVidMatch mask = OFVlanVidMatch.ofRawVid((short) 0x1FFF);
65 return factory.oxms().vlanVidMasked(vid, mask);
66 // Normal case
67 } else if (vlanId.equals(VlanId.ANY)) {
68 return factory.oxms().vlanVidMasked(OFVlanVidMatch.PRESENT, OFVlanVidMatch.PRESENT);
69 } else {
70 return factory.oxms().vlanVid(OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vlanId.toShort())));
71 }
72 } else if (type.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
73 VlanId vlanId = ((Ofdpa3MatchOvid) extensionSelector).vlanId();
74 if (vlanId.equals(VlanId.NONE)) {
75 throw new UnsupportedOperationException(
76 "Unexpected ExtensionSelector: " + extensionSelector.toString());
77 } else if (vlanId.equals(VlanId.ANY)) {
78 throw new UnsupportedOperationException(
79 "Unexpected ExtensionSelector: " + extensionSelector.toString());
80 } else {
81 short mask = (short) 0x1000;
82 short oVid = (short) (mask | vlanId.toShort());
83 return factory.oxms().ofdpaOvid(U16.ofRaw(oVid));
84 }
85 }
86 throw new UnsupportedOperationException(
87 "Unexpected ExtensionSelector: " + extensionSelector.toString());
88 }
89
90 @Override
91 public ExtensionSelector mapOxm(OFOxm<?> oxm) {
92
93 if (oxm.getMatchField().equals(MatchField.VLAN_VID)) {
94 VlanId vlanId;
95 if (oxm.isMasked()) {
96 OFVlanVidMatch vid = ((OFOxmVlanVidMasked) oxm).getValue();
97 OFVlanVidMatch mask = ((OFOxmVlanVidMasked) oxm).getMask();
98
99 if (vid.equals(OFVlanVidMatch.ofRawVid((short) 0))) {
100 vlanId = VlanId.NONE;
101 } else if (vid.equals(OFVlanVidMatch.PRESENT) &&
102 mask.equals(OFVlanVidMatch.PRESENT)) {
103 vlanId = VlanId.ANY;
104 } else {
105 vlanId = VlanId.vlanId(vid.getVlan());
106 }
107 } else {
108 OFVlanVidMatch vid = ((OFOxmVlanVid) oxm).getValue();
109
110 if (!vid.isPresentBitSet()) {
111 vlanId = VlanId.NONE;
112 } else {
113 vlanId = VlanId.vlanId(vid.getVlan());
114 }
115 }
116 return new OfdpaMatchVlanVid(vlanId);
117 } else if (oxm.getMatchField().equals(MatchField.OFDPA_OVID)) {
118 VlanId vlanId;
119 if (oxm.isMasked()) {
120 throw new UnsupportedOperationException(
121 "Unexpected OXM: " + oxm.toString());
122 } else {
123 OFOxmOfdpaOvid ovid = ((OFOxmOfdpaOvid) oxm);
124 short mask = (short) 0x0FFF;
125 short oVid = (short) (mask & ovid.getValue().getRaw());
126 vlanId = VlanId.vlanId(oVid);
127 }
128 return new Ofdpa3MatchOvid(vlanId);
129 }
130 throw new UnsupportedOperationException(
131 "Unexpected OXM: " + oxm.toString());
132 }
133
134 @Override
135 public ExtensionSelector getExtensionSelector(ExtensionSelectorType type) {
136 if (type.equals(ExtensionSelectorType.ExtensionSelectorTypes.OFDPA_MATCH_VLAN_VID.type())) {
137 return new OfdpaMatchVlanVid();
138 } else if (type.equals(ExtensionSelectorTypes.OFDPA_MATCH_OVID.type())) {
139 return new Ofdpa3MatchOvid();
140 }
141 throw new UnsupportedOperationException(
142 "Driver does not support extension type " + type.toString());
143 }
144}