blob: afdb29f8ddbeb67660d4ce041ee348fafd8b3f0d [file] [log] [blame]
Charles Chancad338a2016-09-16 18:03:11 -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
Pier Ventre6f630052016-10-18 09:58:41 -070019import org.onlab.packet.VlanId;
Charles Chancad338a2016-09-16 18:03:11 -070020import org.onosproject.net.behaviour.ExtensionTreatmentResolver;
21import org.onosproject.net.driver.AbstractHandlerBehaviour;
22import org.onosproject.net.flow.instructions.ExtensionTreatment;
23import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
24import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
25import org.projectfloodlight.openflow.protocol.OFActionType;
26import org.projectfloodlight.openflow.protocol.OFFactory;
27import org.projectfloodlight.openflow.protocol.action.OFAction;
28import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
29import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
30import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaMplsType;
Pier Ventre6f630052016-10-18 09:58:41 -070031import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaOvid;
Charles Chancad338a2016-09-16 18:03:11 -070032import org.projectfloodlight.openflow.types.U16;
Pier Ventre6f630052016-10-18 09:58:41 -070033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Charles Chancad338a2016-09-16 18:03:11 -070035
36/**
37 * Interpreter for OFDPA3 OpenFlow treatment extensions.
38 */
39public class Ofdpa3ExtensionTreatmentInterpreter extends AbstractHandlerBehaviour
40 implements ExtensionTreatmentInterpreter, ExtensionTreatmentResolver {
Pier Ventre6f630052016-10-18 09:58:41 -070041
42 private final Logger log = LoggerFactory.getLogger(getClass());
43
Charles Chancad338a2016-09-16 18:03:11 -070044 @Override
45 public boolean supported(ExtensionTreatmentType extensionTreatmentType) {
46 if (extensionTreatmentType.equals(
47 ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_TYPE.type())) {
48 return true;
Pier Ventre6f630052016-10-18 09:58:41 -070049 } else if (extensionTreatmentType.equals(
50 ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) {
51 return true;
Charles Chancad338a2016-09-16 18:03:11 -070052 }
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 Ventre6f630052016-10-18 09:58:41 -070063 } 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 Chancad338a2016-09-16 18:03:11 -070070 }
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 Ventre6f630052016-10-18 09:58:41 -070084 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 Chancad338a2016-09-16 18:03:11 -070090 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 Ventre6f630052016-10-18 09:58:41 -0700103 } else if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) {
104 return new Ofdpa3SetOvid();
Charles Chancad338a2016-09-16 18:03:11 -0700105 }
106 throw new UnsupportedOperationException(
107 "Driver does not support extension type " + type.toString());
108 }
109}