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