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 | // All (known) ports for a Switch |
| 28 | private Map<Integer, Integer> switchPorts = new TreeMap<Integer, Integer>(); |
| 29 | |
| 30 | /** |
| 31 | * Default constructor. |
| 32 | */ |
| 33 | public TopologyElement() { |
| 34 | elementType = Type.ELEMENT_UNKNOWN; |
| 35 | } |
| 36 | |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Constructor to create a Topology Element for a Switch. |
| 39 | * |
| 40 | * @param switchDpid the Switch DPID. |
| 41 | */ |
| 42 | public TopologyElement(long switchDpid) { |
| 43 | this.elementType = Type.ELEMENT_SWITCH; |
| 44 | this.fromSwitchDpid = switchDpid; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Constructor to create a Topology Element for a Switch Port. |
| 49 | * |
| 50 | * @param switchDpid the Switch DPID. |
| 51 | * @param switchPort the Switch Port. |
| 52 | */ |
| 53 | public TopologyElement(long switchDpid, int switchPort) { |
| 54 | this.elementType = Type.ELEMENT_PORT; |
| 55 | this.fromSwitchDpid = switchDpid; |
| 56 | this.fromSwitchPort = switchPort; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Constructor to create a Topology Element for an unidirectional Link |
| 61 | * between Switch Ports. |
| 62 | * |
| 63 | * @param fromSwitchDpid the Switch DPID the Link begins from. |
| 64 | * @param fromSwitchPort the Switch Port the Link begins from. |
| 65 | * @param toSwitchDpid the Switch DPID the Link ends to. |
| 66 | * @param toSwitchPort the Switch Port the Link ends to. |
| 67 | */ |
| 68 | public TopologyElement(long fromSwitchDpid, int fromSwitchPort, |
| 69 | long toSwitchDpid, int toSwitchPort) { |
| 70 | this.elementType = Type.ELEMENT_LINK; |
| 71 | this.fromSwitchDpid = fromSwitchDpid; |
| 72 | this.fromSwitchPort = fromSwitchPort; |
| 73 | this.toSwitchDpid = toSwitchDpid; |
| 74 | this.toSwitchPort = toSwitchPort; |
| 75 | } |
| 76 | |
| 77 | /** |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 78 | * Get the Element type. |
| 79 | * |
| 80 | * @return the Element type. |
| 81 | */ |
| 82 | public TopologyElement.Type getType() { |
| 83 | return elementType; |
| 84 | } |
| 85 | |
| 86 | /** |
Pavlin Radoslavov | 4dc9904 | 2013-10-27 19:52:39 -0700 | [diff] [blame] | 87 | * Get the Switch DPID. |
| 88 | * |
| 89 | * NOTE: Applies for Type.ELEMENT_SWITCH and Type.ELEMENT_PORT |
| 90 | * |
| 91 | * @return the Switch DPID. |
| 92 | */ |
| 93 | public long getSwitch() { |
| 94 | return fromSwitchDpid; |
| 95 | } |
| 96 | |
| 97 | /** |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 98 | * Get the Switch Ports. |
| 99 | * |
| 100 | * NOTE: Applies for Type.ELEMENT_SWITCH |
| 101 | * |
| 102 | * @return the collection of Switch Ports. |
| 103 | */ |
| 104 | public Map<Integer, Integer> getSwitchPorts() { |
| 105 | return switchPorts; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Add a Switch Port. |
| 110 | * |
| 111 | * NOTE: Applies for Type.ELEMENT_SWITCH |
| 112 | * |
| 113 | * @param switchPort the Switch Port to add. |
| 114 | */ |
| 115 | public void addSwitchPort(int switchPort) { |
| 116 | switchPorts.put(switchPort, switchPort); |
| 117 | } |
| 118 | |
| 119 | /** |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 120 | * Get the Switch Port. |
| 121 | * |
| 122 | * NOTE: Applies for Type.ELEMENT_PORT |
| 123 | * |
| 124 | * @return the Switch Port. |
| 125 | */ |
| 126 | public int getSwitchPort() { |
| 127 | return fromSwitchPort; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the Switch DPID the Link begins from. |
| 132 | * |
| 133 | * NOTE: Applies for Type.ELEMENT_LINK |
| 134 | */ |
| 135 | public long getFromSwitch() { |
| 136 | return fromSwitchDpid; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Get the Switch Port the Link begins from. |
| 141 | * |
| 142 | * NOTE: Applies for Type.ELEMENT_LINK |
| 143 | */ |
| 144 | public int getFromPort() { |
| 145 | return fromSwitchPort; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Get the Switch DPID the Link ends to. |
| 150 | * |
| 151 | * NOTE: Applies for Type.ELEMENT_LINK |
| 152 | */ |
| 153 | public long getToSwitch() { |
| 154 | return toSwitchDpid; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Get the Switch Port the Link ends to. |
| 159 | * |
| 160 | * NOTE: Applies for Type.ELEMENT_LINK |
| 161 | */ |
| 162 | public int getToPort() { |
| 163 | return toSwitchPort; |
| 164 | } |
| 165 | |
| 166 | /** |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 167 | * Get the Topology Element ID. |
| 168 | * |
| 169 | * The Topology Element ID has the following format: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 170 | * - Switch: "Switch=<HexLongDpid>" |
| 171 | * Example: "Switch=101" |
| 172 | * - Switch Port: "Port=<HexLongDpid>/<IntPortId>" |
| 173 | * Example: "Port=102/1" |
| 174 | * - Link: "Link=<FromHexLongDpid>/<FromIntPortId>/<ToHexLongDpid>/<ToIntPortId>" |
| 175 | * Example: "Link=101/2/103/4" |
| 176 | * |
| 177 | * NOTE: The Topology Element ID has no syntax meaning. It is used only to |
| 178 | * uniquely identify a topology element. |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 179 | * |
| 180 | * @return the Topology Element ID. |
| 181 | */ |
| 182 | public String elementId() { |
| 183 | switch (elementType) { |
| 184 | case ELEMENT_SWITCH: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 185 | return "Switch=" + Long.toHexString(fromSwitchDpid); |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 186 | case ELEMENT_PORT: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 187 | return "Port=" + |
| 188 | Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 189 | case ELEMENT_LINK: |
Pavlin Radoslavov | 2a091bf | 2013-10-25 21:37:12 -0700 | [diff] [blame] | 190 | return "Link=" + |
| 191 | Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort + "/" + |
| 192 | Long.toHexString(toSwitchDpid) + "/" + toSwitchPort; |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | assert(false); |
| 196 | return null; |
| 197 | } |
Pavlin Radoslavov | 2a8336e | 2013-10-30 16:08:25 -0700 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Convert the Topology Element to a string. |
| 201 | * |
| 202 | * @return the Topology Element as a string. |
| 203 | */ |
| 204 | @Override |
| 205 | public String toString() { |
| 206 | // For now, we just return the Element ID. |
| 207 | return elementId(); |
| 208 | } |
Pavlin Radoslavov | aaace7f | 2013-10-25 19:42:00 -0700 | [diff] [blame] | 209 | } |