blob: fe846540ffe11262372aac604ee7273e56b70691 [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 // 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 Radoslavovaaace7f2013-10-25 19:42:00 -070037 /**
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 Radoslavov2a091bf2013-10-25 21:37:12 -070078 * Get the Element type.
79 *
80 * @return the Element type.
81 */
82 public TopologyElement.Type getType() {
83 return elementType;
84 }
85
86 /**
Pavlin Radoslavov4dc99042013-10-27 19:52:39 -070087 * 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 Radoslavov2a091bf2013-10-25 21:37:12 -070098 * 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 Radoslavov2a091bf2013-10-25 21:37:12 -0700120 * 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 Radoslavovaaace7f2013-10-25 19:42:00 -0700167 * Get the Topology Element ID.
168 *
169 * The Topology Element ID has the following format:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700170 * - 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 Radoslavovaaace7f2013-10-25 19:42:00 -0700179 *
180 * @return the Topology Element ID.
181 */
182 public String elementId() {
183 switch (elementType) {
184 case ELEMENT_SWITCH:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700185 return "Switch=" + Long.toHexString(fromSwitchDpid);
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700186 case ELEMENT_PORT:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700187 return "Port=" +
188 Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700189 case ELEMENT_LINK:
Pavlin Radoslavov2a091bf2013-10-25 21:37:12 -0700190 return "Link=" +
191 Long.toHexString(fromSwitchDpid) + "/" + fromSwitchPort + "/" +
192 Long.toHexString(toSwitchDpid) + "/" + toSwitchPort;
Pavlin Radoslavovaaace7f2013-10-25 19:42:00 -0700193 }
194
195 assert(false);
196 return null;
197 }
198}