blob: b01c7d3d07bb573d3a9f52f3d2518c62d1061b3a [file] [log] [blame]
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -07001package net.onrc.onos.ofcontroller.topology;
2
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -07003import java.util.Map;
4import java.util.TreeMap;
5
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -07006/**
7 * Class for storing information about a Topology Element: Switch, Port or
8 * Link.
9 */
10public class TopologyElement {
11 /**
12 * The Element Type.
13 */
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -070014 public enum Type {
15 ELEMENT_UNKNOWN, // Unknown element
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070016 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 Radoslavov2a091bf2013-10-25 21:37:12 -070027 /**
28 * Default constructor.
29 */
30 public TopologyElement() {
31 elementType = Type.ELEMENT_UNKNOWN;
32 }
33
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070034 /**
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 Radoslavov2a091bf2013-10-25 21:37:12 -070075 * Get the Element type.
76 *
77 * @return the Element type.
78 */
79 public TopologyElement.Type getType() {
80 return elementType;
81 }
82
83 /**
Pavlin Radoslavov4dc99042013-10-27 19:52:39 -070084 * 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 Radoslavov2a091bf2013-10-25 21:37:12 -070095 * 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 Radoslavovaaace7f2013-10-25 19:42:00 -0700142 * Get the Topology Element ID.
143 *
144 * The Topology Element ID has the following format:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700145 * - 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 Radoslavovaaace7f2013-10-25 19:42:00 -0700154 *
155 * @return the Topology Element ID.
156 */
157 public String elementId() {
158 switch (elementType) {
159 case ELEMENT_SWITCH:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700160 return "Switch=" + Long.toHexString(fromSwitchDpid);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700161 case ELEMENT_PORT:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700162 return "Port=" +
163 Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700164 case ELEMENT_LINK:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700165 return "Link=" +
166 Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort + "/" +
167 Long.toHexString(toSwitchDpid) + "/" + toSwitchPort;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700168 }
169
170 assert(false);
171 return null;
172 }
Pavlin Radoslavov2a8336e2013-10-30 16:08:25 -0700173
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 Radoslavovaaace7f2013-10-25 19:42:00 -0700184}