OpenDaylight Controller functional modules.
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / internal / PortConverter.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.internal;
11
12 import org.openflow.protocol.OFPort;
13
14 import org.opendaylight.controller.sal.core.Node;
15 import org.opendaylight.controller.sal.core.NodeConnector;
16 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
17 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
18
19 /**
20  * Abstract class which provides the utilities for converting
21  * the Openflow port number to the equivalent NodeConnector and vice versa
22  *
23  *
24  *
25  */
26 public abstract class PortConverter {
27     private static final int maxOFPhysicalPort = (OFPort.OFPP_MAX.getValue() & 0x7FFF) | 0x8000;
28
29     /**
30      * Converts the Openflow port number to the equivalent NodeConnector.
31      */
32     public static NodeConnector toNodeConnector(short ofPort, Node node) {
33         // Restore original OF unsigned 16 bits value for the comparison
34         int unsignedOFPort = (ofPort & 0x7FFF) | 0x8000;
35
36         if (unsignedOFPort > maxOFPhysicalPort) {
37             if (ofPort == OFPort.OFPP_LOCAL.getValue()) {
38                 return NodeConnectorCreator.createNodeConnector(
39                         NodeConnectorIDType.SWSTACK,
40                         NodeConnector.SPECIALNODECONNECTORID, node);
41             } else if (ofPort == OFPort.OFPP_NORMAL.getValue()) {
42                 return NodeConnectorCreator.createNodeConnector(
43                         NodeConnectorIDType.HWPATH,
44                         NodeConnector.SPECIALNODECONNECTORID, node);
45             } else if (ofPort == OFPort.OFPP_CONTROLLER.getValue()) {
46                 return NodeConnectorCreator.createNodeConnector(
47                         NodeConnectorIDType.CONTROLLER,
48                         NodeConnector.SPECIALNODECONNECTORID, node);
49             }
50         }
51         return NodeConnectorCreator.createNodeConnector(ofPort, node);
52     }
53
54     /**
55      * Converts the NodeConnector to the equivalent Openflow port number
56      */
57     public static short toOFPort(NodeConnector salPort) {
58         if (salPort.getType().equals(NodeConnectorIDType.SWSTACK)) {
59             return OFPort.OFPP_LOCAL.getValue();
60         } else if (salPort.getType().equals(
61                 NodeConnectorIDType.HWPATH)) {
62             return OFPort.OFPP_NORMAL.getValue();
63         } else if (salPort.getType().equals(
64                 NodeConnectorIDType.CONTROLLER)) {
65             return OFPort.OFPP_CONTROLLER.getValue();
66         }
67         return (Short) salPort.getID();
68     }
69 }