Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.topology; |
| 2 | |
| 3 | /** |
| 4 | * Class for storing information about a Topology Element: Switch, Port or |
| 5 | * Link. |
| 6 | */ |
| 7 | public class TopologyElement { |
| 8 | /** |
| 9 | * The Element Type. |
| 10 | */ |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 11 | public enum Type { |
| 12 | ELEMENT_UNKNOWN, // Unknown element |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 13 | ELEMENT_SWITCH, // Network Switch |
| 14 | ELEMENT_PORT, // Switch Port |
| 15 | ELEMENT_LINK // Unidirectional Link between Switch Ports |
| 16 | } |
| 17 | |
| 18 | private Type elementType; // The element type |
| 19 | private long fromSwitchDpid = 0; // The Switch DPID |
| 20 | private int fromSwitchPort = 0; // The Switch Port |
| 21 | private long toSwitchDpid = 0; // The Neighbor Switch DPID |
| 22 | private int toSwitchPort = 0; // The Neighbor Switch Port |
| 23 | |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Default constructor. |
| 26 | */ |
| 27 | public TopologyElement() { |
| 28 | elementType = Type.ELEMENT_UNKNOWN; |
| 29 | } |
| 30 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Constructor to create a Topology Element for a Switch. |
| 33 | * |
| 34 | * @param switchDpid the Switch DPID. |
| 35 | */ |
| 36 | public TopologyElement(long switchDpid) { |
| 37 | this.elementType = Type.ELEMENT_SWITCH; |
| 38 | this.fromSwitchDpid = switchDpid; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructor to create a Topology Element for a Switch Port. |
| 43 | * |
| 44 | * @param switchDpid the Switch DPID. |
| 45 | * @param switchPort the Switch Port. |
| 46 | */ |
| 47 | public TopologyElement(long switchDpid, int switchPort) { |
| 48 | this.elementType = Type.ELEMENT_PORT; |
| 49 | this.fromSwitchDpid = switchDpid; |
| 50 | this.fromSwitchPort = switchPort; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Constructor to create a Topology Element for an unidirectional Link |
| 55 | * between Switch Ports. |
| 56 | * |
| 57 | * @param fromSwitchDpid the Switch DPID the Link begins from. |
| 58 | * @param fromSwitchPort the Switch Port the Link begins from. |
| 59 | * @param toSwitchDpid the Switch DPID the Link ends to. |
| 60 | * @param toSwitchPort the Switch Port the Link ends to. |
| 61 | */ |
| 62 | public TopologyElement(long fromSwitchDpid, int fromSwitchPort, |
| 63 | long toSwitchDpid, int toSwitchPort) { |
| 64 | this.elementType = Type.ELEMENT_LINK; |
| 65 | this.fromSwitchDpid = fromSwitchDpid; |
| 66 | this.fromSwitchPort = fromSwitchPort; |
| 67 | this.toSwitchDpid = toSwitchDpid; |
| 68 | this.toSwitchPort = toSwitchPort; |
| 69 | } |
| 70 | |
| 71 | /** |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 72 | * Get the Element type. |
| 73 | * |
| 74 | * @return the Element type. |
| 75 | */ |
| 76 | public TopologyElement.Type getType() { |
| 77 | return elementType; |
| 78 | } |
| 79 | |
| 80 | /** |
Pavlin Radoslavov | 4dc9904 | 2013-10-27 19:52:39 -0700 | [diff] [blame] | 81 | * Get the Switch DPID. |
| 82 | * |
| 83 | * NOTE: Applies for Type.ELEMENT_SWITCH and Type.ELEMENT_PORT |
| 84 | * |
| 85 | * @return the Switch DPID. |
| 86 | */ |
| 87 | public long getSwitch() { |
| 88 | return fromSwitchDpid; |
| 89 | } |
| 90 | |
| 91 | /** |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 92 | * Get the Switch Port. |
| 93 | * |
| 94 | * NOTE: Applies for Type.ELEMENT_PORT |
| 95 | * |
| 96 | * @return the Switch Port. |
| 97 | */ |
| 98 | public int getSwitchPort() { |
| 99 | return fromSwitchPort; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get the Switch DPID the Link begins from. |
| 104 | * |
| 105 | * NOTE: Applies for Type.ELEMENT_LINK |
| 106 | */ |
| 107 | public long getFromSwitch() { |
| 108 | return fromSwitchDpid; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the Switch Port the Link begins from. |
| 113 | * |
| 114 | * NOTE: Applies for Type.ELEMENT_LINK |
| 115 | */ |
| 116 | public int getFromPort() { |
| 117 | return fromSwitchPort; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get the Switch DPID the Link ends to. |
| 122 | * |
| 123 | * NOTE: Applies for Type.ELEMENT_LINK |
| 124 | */ |
| 125 | public long getToSwitch() { |
| 126 | return toSwitchDpid; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get the Switch Port the Link ends to. |
| 131 | * |
| 132 | * NOTE: Applies for Type.ELEMENT_LINK |
| 133 | */ |
| 134 | public int getToPort() { |
| 135 | return toSwitchPort; |
| 136 | } |
| 137 | |
| 138 | /** |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 139 | * Get the Topology Element ID. |
| 140 | * |
| 141 | * The Topology Element ID has the following format: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 142 | * - Switch: "Switch=<HexLongDpid>" |
| 143 | * Example: "Switch=101" |
| 144 | * - Switch Port: "Port=<HexLongDpid>/<IntPortId>" |
| 145 | * Example: "Port=102/1" |
| 146 | * - Link: "Link=<FromHexLongDpid>/<FromIntPortId>/<ToHexLongDpid>/<ToIntPortId>" |
| 147 | * Example: "Link=101/2/103/4" |
| 148 | * |
| 149 | * NOTE: The Topology Element ID has no syntax meaning. It is used only to |
| 150 | * uniquely identify a topology element. |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 151 | * |
| 152 | * @return the Topology Element ID. |
| 153 | */ |
| 154 | public String elementId() { |
| 155 | switch (elementType) { |
| 156 | case ELEMENT_SWITCH: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 157 | return "Switch=" + Long.toHexString(fromSwitchDpid); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 158 | case ELEMENT_PORT: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 159 | return "Port=" + |
| 160 | Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 161 | case ELEMENT_LINK: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 162 | return "Link=" + |
| 163 | Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort + "/" + |
| 164 | Long.toHexString(toSwitchDpid) + "/" + toSwitchPort; |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 165 | case ELEMENT_UNKNOWN: |
| 166 | return "Element=UNKNOWN"; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 167 | } |
Pavlin Radoslavov | 4839f6d | 2013-12-11 12:49:45 -0800 | [diff] [blame] | 168 | |
| 169 | assert(false); |
| 170 | return null; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 171 | } |
Pavlin Radoslavov | 2a8336e | 2013-10-30 16:08:25 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * Convert the Topology Element to a string. |
| 175 | * |
| 176 | * @return the Topology Element as a string. |
| 177 | */ |
| 178 | @Override |
| 179 | public String toString() { |
| 180 | // For now, we just return the Element ID. |
| 181 | return elementId(); |
| 182 | } |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 183 | } |