082c5a6dc20722d05680aeced12669350be81ac1
[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)) {
42             return true;
43         }
44
45         return false;
46     }
47
48     @Override
49     public OFAction mapInstruction(OFFactory factory, ExtensionInstruction extensionInstruction) {
50         ExtensionType type = extensionInstruction.type();
51         if (type.equals(ExtensionType.ExtensionTypes.NICIRA_SET_TUNNEL_DST.type())) {
52             NiciraSetTunnelDst tunnelDst = (NiciraSetTunnelDst) extensionInstruction;
53             return factory.actions().setField(factory.oxms().tunnelIpv4Dst(
54                     IPv4Address.of(tunnelDst.tunnelDst().toInt())));
55         }
56         return null;
57     }
58
59     @Override
60     public ExtensionInstruction mapAction(OFAction action) {
61         if (action.getType().equals(OFActionType.SET_FIELD)) {
62             OFActionSetField setFieldAction = (OFActionSetField) action;
63             OFOxm<?> oxm = setFieldAction.getField();
64             switch (oxm.getMatchField().id) {
65             case TUNNEL_IPV4_DST:
66                 OFOxmTunnelIpv4Dst tunnelIpv4Dst = (OFOxmTunnelIpv4Dst) oxm;
67                 return new NiciraSetTunnelDst(Ip4Address.valueOf(tunnelIpv4Dst.getValue().getInt()));
68             default:
69                 throw new UnsupportedOperationException(
70                         "Driver does not support extension type " + oxm.getMatchField().id);
71             }
72         }
73         return null;
74     }
75
76     @Override
77     public ExtensionInstruction getExtensionInstruction(ExtensionType type) {
78         if (type.equals(ExtensionType.ExtensionTypes.NICIRA_SET_TUNNEL_DST.type())) {
79             return new NiciraSetTunnelDst();
80         }
81         throw new UnsupportedOperationException(
82                 "Driver does not support extension type " + type.toString());
83     }
84 }