b8f1fd910d5b88330b33641e5aa4c726b5b3d381
[onosfw.git] /
1 /*
2  * Copyright 2015 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
19 import org.onlab.packet.Ip4Address;
20 import org.onosproject.net.behaviour.ExtensionResolver;
21 import org.onosproject.net.driver.AbstractHandlerBehaviour;
22 import org.onosproject.net.flow.instructions.ExtensionInstruction;
23 import org.onosproject.net.flow.instructions.ExtensionType;
24 import org.onosproject.openflow.controller.ExtensionInterpreter;
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.OFOxmTunnelIpv4Dst;
31 import org.projectfloodlight.openflow.types.IPv4Address;
32
33 /**
34  * Interpreter for Nicira OpenFlow extensions.
35  */
36 public class NiciraExtensionInterpreter extends AbstractHandlerBehaviour
37         implements ExtensionInterpreter, ExtensionResolver {
38
39     @Override
40     public boolean supported(ExtensionType extensionType) {
41         if (extensionType.equals(ExtensionType.ExtensionTypes.NICIRA_SET_TUNNEL_DST.type())) {
42             return true;
43         }
44         if (extensionType.equals(ExtensionType.ExtensionTypes.NICIRA_RESUBMIT.type())) {
45             return true;
46         }
47         return false;
48     }
49
50     @Override
51     public OFAction mapInstruction(OFFactory factory, ExtensionInstruction extensionInstruction) {
52         ExtensionType type = extensionInstruction.type();
53         if (type.equals(ExtensionType.ExtensionTypes.NICIRA_SET_TUNNEL_DST.type())) {
54             NiciraSetTunnelDst tunnelDst = (NiciraSetTunnelDst) extensionInstruction;
55             return factory.actions().setField(factory.oxms().tunnelIpv4Dst(
56                     IPv4Address.of(tunnelDst.tunnelDst().toInt())));
57         }
58         if (type.equals(ExtensionType.ExtensionTypes.NICIRA_RESUBMIT.type())) {
59           // TODO this will be implemented later
60         }
61         return null;
62     }
63
64     @Override
65     public ExtensionInstruction mapAction(OFAction action) {
66         if (action.getType().equals(OFActionType.SET_FIELD)) {
67             OFActionSetField setFieldAction = (OFActionSetField) action;
68             OFOxm<?> oxm = setFieldAction.getField();
69             switch (oxm.getMatchField().id) {
70             case TUNNEL_IPV4_DST:
71                 OFOxmTunnelIpv4Dst tunnelIpv4Dst = (OFOxmTunnelIpv4Dst) oxm;
72                 return new NiciraSetTunnelDst(Ip4Address.valueOf(tunnelIpv4Dst.getValue().getInt()));
73             default:
74                 throw new UnsupportedOperationException(
75                         "Driver does not support extension type " + oxm.getMatchField().id);
76             }
77         }
78         return null;
79     }
80
81     @Override
82     public ExtensionInstruction getExtensionInstruction(ExtensionType type) {
83         if (type.equals(ExtensionType.ExtensionTypes.NICIRA_SET_TUNNEL_DST.type())) {
84             return new NiciraSetTunnelDst();
85         }
86         if (type.equals(ExtensionType.ExtensionTypes.NICIRA_RESUBMIT.type())) {
87             return new NiciraResubmit();
88         }
89         throw new UnsupportedOperationException(
90                 "Driver does not support extension type " + type.toString());
91     }
92 }