Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.driver.extensions; |
| 18 | |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 19 | import org.onlab.packet.VlanId; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 20 | import org.onosproject.net.behaviour.ExtensionTreatmentResolver; |
| 21 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 22 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 23 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 24 | import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter; |
| 25 | import org.projectfloodlight.openflow.protocol.OFActionType; |
| 26 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 27 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 28 | import org.projectfloodlight.openflow.protocol.action.OFActionSetField; |
| 29 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 30 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaMplsType; |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 31 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaOvid; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 32 | import org.projectfloodlight.openflow.types.U16; |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Interpreter for OFDPA3 OpenFlow treatment extensions. |
| 38 | */ |
| 39 | public class Ofdpa3ExtensionTreatmentInterpreter extends AbstractHandlerBehaviour |
| 40 | implements ExtensionTreatmentInterpreter, ExtensionTreatmentResolver { |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 41 | |
| 42 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 43 | |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 44 | @Override |
| 45 | public boolean supported(ExtensionTreatmentType extensionTreatmentType) { |
| 46 | if (extensionTreatmentType.equals( |
| 47 | ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_TYPE.type())) { |
| 48 | return true; |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 49 | } else if (extensionTreatmentType.equals( |
| 50 | ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) { |
| 51 | return true; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public OFAction mapInstruction(OFFactory factory, ExtensionTreatment extensionTreatment) { |
| 58 | ExtensionTreatmentType type = extensionTreatment.type(); |
| 59 | if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_TYPE.type())) { |
| 60 | short mplsType = ((Ofdpa3SetMplsType) extensionTreatment).mplsType(); |
| 61 | return factory.actions().setField(factory.oxms().ofdpaMplsType( |
| 62 | U16.ofRaw(mplsType))); |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 63 | } else if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) { |
| 64 | // OFDPA requires isPresent bit set to 1 for OVID. |
| 65 | VlanId vlanId = ((Ofdpa3SetOvid) extensionTreatment).vlanId(); |
| 66 | short mask = (short) 0x1000; |
| 67 | short oVid = (short) (mask | vlanId.toShort()); |
| 68 | return factory.actions().setField(factory.oxms().ofdpaOvid( |
| 69 | U16.ofRaw(oVid))); |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 70 | } |
| 71 | throw new UnsupportedOperationException( |
| 72 | "Unexpected ExtensionTreatment: " + extensionTreatment.toString()); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public ExtensionTreatment mapAction(OFAction action) throws UnsupportedOperationException { |
| 77 | if (action.getType().equals(OFActionType.SET_FIELD)) { |
| 78 | OFActionSetField setFieldAction = (OFActionSetField) action; |
| 79 | OFOxm<?> oxm = setFieldAction.getField(); |
| 80 | switch (oxm.getMatchField().id) { |
| 81 | case OFDPA_MPLS_TYPE: |
| 82 | OFOxmOfdpaMplsType mplsType = (OFOxmOfdpaMplsType) oxm; |
| 83 | return new Ofdpa3SetMplsType(mplsType.getValue().getRaw()); |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 84 | case OFDPA_OVID: |
| 85 | OFOxmOfdpaOvid ovid = ((OFOxmOfdpaOvid) oxm); |
| 86 | short mask = (short) 0x0FFF; |
| 87 | short oVid = (short) (mask & ovid.getValue().getRaw()); |
| 88 | VlanId vlanId = VlanId.vlanId(oVid); |
| 89 | return new Ofdpa3SetOvid(vlanId); |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 90 | default: |
| 91 | throw new UnsupportedOperationException( |
| 92 | "Driver does not support extension type " + oxm.getMatchField().id); |
| 93 | } |
| 94 | } |
| 95 | throw new UnsupportedOperationException( |
| 96 | "Unexpected OFAction: " + action.toString()); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public ExtensionTreatment getExtensionInstruction(ExtensionTreatmentType type) { |
| 101 | if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_TYPE.type())) { |
| 102 | return new Ofdpa3SetMplsType(); |
Pier Ventre | 6f63005 | 2016-10-18 09:58:41 -0700 | [diff] [blame] | 103 | } else if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) { |
| 104 | return new Ofdpa3SetOvid(); |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 105 | } |
| 106 | throw new UnsupportedOperationException( |
| 107 | "Driver does not support extension type " + type.toString()); |
| 108 | } |
| 109 | } |