61d6730042e95b8d1e07182330242a8ed2116102
[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.NetUtils;
18 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
19
20 /**
21  * Abstract class which provides the utilities for converting
22  * the Openflow port number to the equivalent NodeConnector and vice versa
23  *
24  *
25  *
26  */
27 public abstract class PortConverter {
28     private static final int maxOFPhysicalPort =
29                 NetUtils.getUnsignedShort(OFPort.OFPP_MAX.getValue());
30
31     /**
32      * Converts the Openflow port number to the equivalent NodeConnector.
33      */
34     public static NodeConnector toNodeConnector(short ofPort, Node node) {
35         // Restore original OF unsigned 16 bits value for the comparison
36         int unsignedOFPort = NetUtils.getUnsignedShort(ofPort);
37
38         if (unsignedOFPort > maxOFPhysicalPort) {
39             if (ofPort == OFPort.OFPP_LOCAL.getValue()) {
40                 return NodeConnectorCreator.createNodeConnector(
41                         NodeConnectorIDType.SWSTACK,
42                         NodeConnector.SPECIALNODECONNECTORID, node);
43             } else if (ofPort == OFPort.OFPP_NORMAL.getValue()) {
44                 return NodeConnectorCreator.createNodeConnector(
45                         NodeConnectorIDType.HWPATH,
46                         NodeConnector.SPECIALNODECONNECTORID, node);
47             } else if (ofPort == OFPort.OFPP_CONTROLLER.getValue()) {
48                 return NodeConnectorCreator.createNodeConnector(
49                         NodeConnectorIDType.CONTROLLER,
50                         NodeConnector.SPECIALNODECONNECTORID, node);
51             }
52         }
53         return NodeConnectorCreator.createNodeConnector(ofPort, node);
54     }
55
56     /**
57      * Converts the NodeConnector to the equivalent Openflow port number
58      */
59     public static short toOFPort(NodeConnector salPort) {
60         if (salPort.getType().equals(NodeConnectorIDType.SWSTACK)) {
61             return OFPort.OFPP_LOCAL.getValue();
62         } else if (salPort.getType().equals(
63                 NodeConnectorIDType.HWPATH)) {
64             return OFPort.OFPP_NORMAL.getValue();
65         } else if (salPort.getType().equals(
66                 NodeConnectorIDType.CONTROLLER)) {
67             return OFPort.OFPP_CONTROLLER.getValue();
68         }
69         return (Short) salPort.getID();
70     }
71 }