blob: c44970285daf7657c600ca2e8aaf7c5ea26f74c8 [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;
Pier Ventre9cf536b2016-10-21 13:30:18 -070030import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaMplsL2Port;
Charles Chancad338a2016-09-16 18:03:11 -070031import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaMplsType;
Pier Ventre6f630052016-10-18 09:58:41 -070032import org.projectfloodlight.openflow.protocol.oxm.OFOxmOfdpaOvid;
Charles Chancad338a2016-09-16 18:03:11 -070033import org.projectfloodlight.openflow.types.U16;
Pier Ventre9cf536b2016-10-21 13:30:18 -070034import org.projectfloodlight.openflow.types.U32;
Pier Ventre6f630052016-10-18 09:58:41 -070035import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
Charles Chancad338a2016-09-16 18:03:11 -070037
38/**
39 * Interpreter for OFDPA3 OpenFlow treatment extensions.
40 */
41public class Ofdpa3ExtensionTreatmentInterpreter extends AbstractHandlerBehaviour
42 implements ExtensionTreatmentInterpreter, ExtensionTreatmentResolver {
Pier Ventre6f630052016-10-18 09:58:41 -070043
44 private final Logger log = LoggerFactory.getLogger(getClass());
45
Charles Chancad338a2016-09-16 18:03:11 -070046 @Override
47 public boolean supported(ExtensionTreatmentType extensionTreatmentType) {
48 if (extensionTreatmentType.equals(
49 ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_TYPE.type())) {
50 return true;
Pier Ventre6f630052016-10-18 09:58:41 -070051 } else if (extensionTreatmentType.equals(
52 ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) {
53 return true;
Pier Ventre9cf536b2016-10-21 13:30:18 -070054 } else if (extensionTreatmentType.equals(
55 ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_L2_PORT.type())) {
56 return true;
Charles Chancad338a2016-09-16 18:03:11 -070057 }
58 return false;
59 }
60
61 @Override
62 public OFAction mapInstruction(OFFactory factory, ExtensionTreatment extensionTreatment) {
63 ExtensionTreatmentType type = extensionTreatment.type();
64 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_TYPE.type())) {
65 short mplsType = ((Ofdpa3SetMplsType) extensionTreatment).mplsType();
66 return factory.actions().setField(factory.oxms().ofdpaMplsType(
67 U16.ofRaw(mplsType)));
Pier Ventre6f630052016-10-18 09:58:41 -070068 } else if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) {
69 // OFDPA requires isPresent bit set to 1 for OVID.
70 VlanId vlanId = ((Ofdpa3SetOvid) extensionTreatment).vlanId();
71 short mask = (short) 0x1000;
72 short oVid = (short) (mask | vlanId.toShort());
73 return factory.actions().setField(factory.oxms().ofdpaOvid(
74 U16.ofRaw(oVid)));
Pier Ventre9cf536b2016-10-21 13:30:18 -070075 } else if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_L2_PORT.type())) {
76 Integer mplsL2Port = ((Ofdpa3SetMplsL2Port) extensionTreatment).mplsL2Port();
77 /*
78 * 0x0000XXXX UNI Interface.
79 * 0x0002XXXX NNI Interface
80 */
81 if ((mplsL2Port >= 0 && mplsL2Port <= 0x0000FFFF) ||
82 (mplsL2Port >= 0x00020000 && mplsL2Port <= 0x0002FFFF)) {
83 return factory.actions().setField(
84 factory.oxms().ofdpaMplsL2Port(U32.ofRaw(mplsL2Port))
85 );
86 }
87 throw new UnsupportedOperationException(
88 "Unexpected ExtensionTreatment: " + extensionTreatment.toString());
Charles Chancad338a2016-09-16 18:03:11 -070089 }
90 throw new UnsupportedOperationException(
91 "Unexpected ExtensionTreatment: " + extensionTreatment.toString());
92 }
93
94 @Override
95 public ExtensionTreatment mapAction(OFAction action) throws UnsupportedOperationException {
96 if (action.getType().equals(OFActionType.SET_FIELD)) {
97 OFActionSetField setFieldAction = (OFActionSetField) action;
98 OFOxm<?> oxm = setFieldAction.getField();
99 switch (oxm.getMatchField().id) {
100 case OFDPA_MPLS_TYPE:
101 OFOxmOfdpaMplsType mplsType = (OFOxmOfdpaMplsType) oxm;
102 return new Ofdpa3SetMplsType(mplsType.getValue().getRaw());
Pier Ventre6f630052016-10-18 09:58:41 -0700103 case OFDPA_OVID:
104 OFOxmOfdpaOvid ovid = ((OFOxmOfdpaOvid) oxm);
105 short mask = (short) 0x0FFF;
106 short oVid = (short) (mask & ovid.getValue().getRaw());
107 VlanId vlanId = VlanId.vlanId(oVid);
108 return new Ofdpa3SetOvid(vlanId);
Pier Ventre9cf536b2016-10-21 13:30:18 -0700109 case OFDPA_MPLS_L2_PORT:
110 OFOxmOfdpaMplsL2Port mplsl2Port = ((OFOxmOfdpaMplsL2Port) oxm);
111 Integer mplsL2Port = mplsl2Port.getValue().getRaw();
112 if ((mplsL2Port >= 0 && mplsL2Port <= 0x0000FFFF) ||
113 (mplsL2Port >= 0x00020000 && mplsL2Port <= 0x0002FFFF)) {
114 return new Ofdpa3SetMplsL2Port(mplsL2Port);
115 }
Charles Chancad338a2016-09-16 18:03:11 -0700116 default:
117 throw new UnsupportedOperationException(
118 "Driver does not support extension type " + oxm.getMatchField().id);
119 }
120 }
121 throw new UnsupportedOperationException(
122 "Unexpected OFAction: " + action.toString());
123 }
124
125 @Override
126 public ExtensionTreatment getExtensionInstruction(ExtensionTreatmentType type) {
127 if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_TYPE.type())) {
128 return new Ofdpa3SetMplsType();
Pier Ventre6f630052016-10-18 09:58:41 -0700129 } else if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_OVID.type())) {
130 return new Ofdpa3SetOvid();
Pier Ventre9cf536b2016-10-21 13:30:18 -0700131 } else if (type.equals(ExtensionTreatmentType.ExtensionTreatmentTypes.OFDPA_SET_MPLS_L2_PORT.type())) {
132 return new Ofdpa3SetMplsL2Port();
Charles Chancad338a2016-09-16 18:03:11 -0700133 }
134 throw new UnsupportedOperationException(
135 "Driver does not support extension type " + type.toString());
136 }
137}