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