blob: 0fefa3a59e3070dc0b3aed84898458d7850b12f9 [file] [log] [blame]
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -07001package net.onrc.onos.ofcontroller.topology;
2
3/**
4 * Class for storing information about a Topology Element: Switch, Port or
5 * Link.
6 */
7public class TopologyElement {
8 /**
9 * The Element Type.
10 */
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -070011 public enum Type {
12 ELEMENT_UNKNOWN, // Unknown element
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070013 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 Radoslavov2a091bf2013-10-25 21:37:12 -070024 /**
25 * Default constructor.
26 */
27 public TopologyElement() {
28 elementType = Type.ELEMENT_UNKNOWN;
29 }
30
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -070031 /**
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 Radoslavov2a091bf2013-10-25 21:37:12 -070072 * Get the Element type.
73 *
74 * @return the Element type.
75 */
76 public TopologyElement.Type getType() {
77 return elementType;
78 }
79
80 /**
Pavlin Radoslavov4dc99042013-10-27 19:52:39 -070081 * 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 Radoslavov2a091bf2013-10-25 21:37:12 -070092 * 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 Radoslavovaaace7f2013-10-25 19:42:00 -0700139 * Get the Topology Element ID.
140 *
141 * The Topology Element ID has the following format:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700142 * - 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 Radoslavovaaace7f2013-10-25 19:42:00 -0700151 *
152 * @return the Topology Element ID.
153 */
154 public String elementId() {
155 switch (elementType) {
156 case ELEMENT_SWITCH:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700157 return "Switch=" + Long.toHexString(fromSwitchDpid);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700158 case ELEMENT_PORT:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700159 return "Port=" +
160 Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700161 case ELEMENT_LINK:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700162 return "Link=" +
163 Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort + "/" +
164 Long.toHexString(toSwitchDpid) + "/" + toSwitchPort;
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800165 case ELEMENT_UNKNOWN:
166 return "Element=UNKNOWN";
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700167 }
Pavlin Radoslavov4839f6d2013-12-11 12:49:45 -0800168
169 assert(false);
170 return null;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700171 }
Pavlin Radoslavov2a8336e2013-10-30 16:08:25 -0700172
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 Radoslavovaaace7f2013-10-25 19:42:00 -0700183}