blob: 175209d84c227c47bb5798d65eca2468a90eefe7 [file] [log] [blame]
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -08001package net.onrc.onos.datastore.topology;
2
3import java.nio.ByteBuffer;
4import java.util.Arrays;
5import java.util.Collection;
6import java.util.Collections;
7import java.util.Map;
8import java.util.HashMap;
9import java.util.Set;
10import java.util.TreeSet;
11
12import net.onrc.onos.datastore.RCObject;
13import net.onrc.onos.datastore.RCTable;
14import net.onrc.onos.datastore.utils.ByteArrayComparator;
15
16import org.slf4j.Logger;
17import org.slf4j.LoggerFactory;
18
19import com.esotericsoftware.kryo.Kryo;
20
21import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException;
22import edu.stanford.ramcloud.JRamCloud.ObjectExistsException;
23import edu.stanford.ramcloud.JRamCloud.WrongVersionException;
24
25/**
26 * Switch Object in RC.
27 *
28 * @note This class will not maintain invariants. e.g. It will NOT automatically
29 * remove Ports on Switch, when deleting a Switch.
30 *
31 */
32public class RCSwitch extends RCObject {
33 private static final Logger log = LoggerFactory.getLogger(RCSwitch.class);
34
35 private static final ThreadLocal<Kryo> switchKryo = new ThreadLocal<Kryo>() {
36 @Override
37 protected Kryo initialValue() {
38 Kryo kryo = new Kryo();
39 kryo.setRegistrationRequired(true);
40 kryo.setReferences(false);
41 kryo.register(byte[].class);
42 kryo.register(byte[][].class);
43 kryo.register(HashMap.class);
44 // TODO check if we should explicitly specify EnumSerializer
45 kryo.register(STATUS.class);
46 return kryo;
47 }
48 };
49
50 public static final String GLOBAL_SWITCH_TABLE_NAME = "G:Switch";
51
52 // FIXME these should be Enum or some number, not String
53 private static final String PROP_DPID = "dpid";
54 private static final String PROP_STATUS = "status";
55 private static final String PROP_PORT_IDS = "port-ids";
56
57 // must not re-order enum members, ordinal will be sent over wire
58 public enum STATUS {
59 INACTIVE, ACTIVE;
60 }
61
62 private final Long dpid;
63 private STATUS status;
64 private TreeSet<byte[]> portIds;
65 transient private boolean isPortIdsModified;
66
67 public static final int SWITCHID_BYTES = 2 + 8;
68
69 public static byte[] getSwichID(Long dpid) {
70 if (dpid == null) {
71 throw new IllegalArgumentException("dpid cannot be null");
72 }
73 return ByteBuffer.allocate(SWITCHID_BYTES).putChar('S').putLong(dpid)
74 .array();
75 }
76
77 public static long getDpidFromKey(byte[] key) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -080078 return getDpidFromKey(ByteBuffer.wrap(key));
79 }
80
81 public static long getDpidFromKey(ByteBuffer keyBuf) {
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080082 if (keyBuf.getChar() != 'S') {
83 throw new IllegalArgumentException("Invalid Switch key");
84 }
85 return keyBuf.getLong();
86 }
87
88 // FIXME specify DPID here, or Should caller specify the key it self?
89 // In other words, should layer above have the control of the ID?
90 public RCSwitch(Long dpid) {
91 super(RCTable.getTable(GLOBAL_SWITCH_TABLE_NAME), getSwichID(dpid));
92
93 this.dpid = dpid;
94 this.status = STATUS.INACTIVE;
95 this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR);
96 this.isPortIdsModified = true;
97 }
98
99 public static RCSwitch createFromKey(byte[] key) {
100 return new RCSwitch(getDpidFromKey(key));
101 }
102
103 public STATUS getStatus() {
104 return status;
105 }
106
107 public void setStatus(STATUS status) {
108 this.status = status;
109 getObjectMap().put(PROP_STATUS, status);
110 }
111
112 public Long getDpid() {
113 return dpid;
114 }
115
116 public byte[] getId() {
117 return getKey();
118 }
119
120 public void addPortId(byte[] portId) {
121 // TODO: Should we copy portId, or reference is OK.
122 isPortIdsModified |= portIds.add(portId);
123 }
124
125 public void removePortId(byte[] portId) {
126 isPortIdsModified |= portIds.remove(portId);
127 }
128
129 public void emptyPortIds() {
130 portIds.clear();
131 this.isPortIdsModified = true;
132 }
133
134 public void addAllToPortIds(Collection<byte[]> portIds) {
135 // TODO: Should we copy portId, or reference is OK.
136 isPortIdsModified |= this.portIds.addAll(portIds);
137 }
138
139 /**
140 *
141 * @return Unmodifiable Set view of all the PortIds;
142 */
143 public Set<byte[]> getAllPortIds() {
144 return Collections.unmodifiableSet(portIds);
145 }
146
147 @Override
148 public void serializeAndSetValue() {
149 Map<Object, Object> map = getObjectMap();
150
151 map.put(PROP_DPID, this.dpid);
152 if (isPortIdsModified) {
153 byte[] portIdArray[] = new byte[portIds.size()][];
154 map.put(PROP_PORT_IDS, portIds.toArray(portIdArray));
155 isPortIdsModified = false;
156 }
157
158 serializeAndSetValue(switchKryo.get(), map);
159 }
160
161 @Override
162 public Map<Object, Object> deserializeObjectFromValue() {
163 Map<Object, Object> map = deserializeObjectFromValue(switchKryo.get());
164
165 this.status = (STATUS) map.get(PROP_STATUS);
166
167 if (this.portIds == null) {
168 this.portIds = new TreeSet<>(
169 ByteArrayComparator.BYTEARRAY_COMPARATOR);
170 }
171 byte[] portIdArray[] = (byte[][]) map.get(PROP_PORT_IDS);
172 if (portIdArray != null) {
173 this.portIds.clear();
174 this.portIds.addAll(Arrays.asList(portIdArray));
175 isPortIdsModified = false;
176 } else {
177 // trigger write on next serialize
178 isPortIdsModified = true;
179 }
180 return map;
181 }
182
183 @Override
184 public String toString() {
185 // TODO OUTPUT ALL?
186 return "[RCSwitch 0x" + Long.toHexString(dpid) + " STATUS:" + status
187 + "]";
188 }
189
190 public static void main(String argv[]) {
191 // create active switch 0x1 with 2 ports
192 RCSwitch sw = new RCSwitch(0x1L);
193 sw.setStatus(STATUS.ACTIVE);
194 sw.addPortId("SW0x0001P001".getBytes());
195 sw.addPortId("SW0x0001P002".getBytes());
196
197 try {
198 sw.create();
199 } catch (ObjectExistsException e) {
200 log.debug("Create Switch Failed", e);
201 e.printStackTrace();
202 }
203
204 // read switch 0x1
205 RCSwitch swRead = new RCSwitch(0x1L);
206 try {
207 swRead.read();
208 } catch (ObjectDoesntExistException e) {
209 log.debug("Reading Switch Failed", e);
210 }
211 assert (swRead.getStatus() == STATUS.ACTIVE);
212 for (byte[] portId : swRead.getAllPortIds()) {
213 // bad example code, portId is not expected to be ASCII string
214 log.debug("PortId: {}", new String(portId));
215 }
216 assert (swRead.getAllPortIds().size() == 2);
217
218 // update 0x1
219 swRead.setStatus(STATUS.INACTIVE);
220 swRead.removePortId("SW0x0001P001".getBytes());
221 try {
222 swRead.update();
223 } catch (ObjectDoesntExistException | WrongVersionException e) {
224 log.debug("Updating Switch Failed", e);
225 }
226
227 // read 0x1 again and delete
228 RCSwitch swRead2 = new RCSwitch(0x1L);
229 try {
230 swRead2.read();
231 } catch (ObjectDoesntExistException e) {
232 log.debug("Reading Switch Again Failed", e);
233 }
234 assert (swRead2.getStatus() == STATUS.INACTIVE);
235 for (byte[] portId : swRead2.getAllPortIds()) {
236 // bad example code, portId is not expected to be ASCII string
237 log.debug("PortId: {}", new String(portId));
238 }
239 assert (swRead2.getAllPortIds().size() == 1);
240 try {
241 swRead2.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800242 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800243 log.debug("Deleting Switch Failed", e);
244 }
245
246 RCSwitch swRead3 = new RCSwitch(0x1L);
247 try {
248 swRead3.read();
249 } catch (ObjectDoesntExistException e) {
250 log.debug("Switch not found as expected");
251 }
252
253 topology_setup();
254 topology_walk();
255 topology_delete();
256 }
257
258 private static void topology_setup() {
259 log.debug("topology_setup start.");
260
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800261 // d1 - s1p1 - s1 - s1p2 - s2p1 - s2 - s2p2
262
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800263 RCSwitch sw1 = new RCSwitch(0x1L);
264 sw1.setStatus(STATUS.ACTIVE);
265 try {
266 sw1.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800267 log.debug("Create {}", sw1);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800268 } catch (ObjectExistsException e) {
269 log.error("Switch creation failed", e);
270 }
271
272 RCPort sw1p1 = new RCPort(0x1L, 1L);
273 sw1p1.setStatus(RCPort.STATUS.ACTIVE);
274 RCPort sw1p2 = new RCPort(0x1L, 2L);
275 sw1p2.setStatus(RCPort.STATUS.ACTIVE);
276 try {
277 sw1p1.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800278 log.debug("Create {}", sw1p1);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800279 sw1p2.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800280 log.debug("Create {}", sw1p2);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800281 } catch (ObjectExistsException e) {
282 log.error("Port creation failed", e);
283 }
284
285 sw1.emptyPortIds();
286 sw1.addPortId(sw1p1.getId());
287 sw1.addPortId(sw1p2.getId());
288 try {
289 sw1.update();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800290 log.debug("Update {} - {}", sw1, sw1.getAllPortIds());
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800291 } catch (ObjectDoesntExistException | WrongVersionException e) {
292 log.error("Switch update failed", e);
293 }
294
295 RCDevice d1 = new RCDevice(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
296 d1.addPortId(sw1p1.getId());
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800297 sw1p1.addDeviceId(d1.getId());
298
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800299 try {
300 d1.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800301 log.debug("Create {}", d1);
302 try {
303 sw1p1.update();
304 } catch (ObjectDoesntExistException | WrongVersionException e) {
305 log.error("Link update failed", e);
306 }
307 log.debug("Create {}", sw1p1);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800308 } catch (ObjectExistsException e) {
309 log.error("Device creation failed", e);
310 }
311
312 RCSwitch sw2 = new RCSwitch(0x2L);
313 sw2.setStatus(STATUS.ACTIVE);
314 RCPort sw2p1 = new RCPort(0x2L, 1L);
315 sw2p1.setStatus(RCPort.STATUS.ACTIVE);
316 RCPort sw2p2 = new RCPort(0x2L, 2L);
317 sw2p2.setStatus(RCPort.STATUS.ACTIVE);
318
319 sw2.addPortId(sw2p1.getId());
320 sw2.addPortId(sw2p2.getId());
321 sw2.addAllToPortIds(Arrays.asList(sw2p1.getId(), sw2p2.getId()));
322 assert (sw2.getAllPortIds().size() == 2);
323
324 RCDevice d2 = new RCDevice(new byte[] { 6, 5, 4, 3, 2, 1, 0 });
325 d2.addPortId(sw2p2.getId());
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800326 sw2p2.addDeviceId(d2.getId());
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800327
328 try {
329 sw2.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800330 log.debug("Create {}", sw2);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800331 sw2p1.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800332 log.debug("Create {}", sw2p1);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800333 sw2p2.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800334 log.debug("Create {}", sw2p2);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800335 d2.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800336 log.debug("Create {}", d2);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800337 } catch (ObjectExistsException e) {
338 log.error("One of Switch/Port/Device creation failed", e);
339 }
340
341 RCLink l1 = new RCLink(0x1L, 2L, 0x2L, 1L);
342 l1.setStatus(RCLink.STATUS.ACTIVE);
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800343
344 sw1p2.addLinkId(l1.getId());
345 sw2p1.addLinkId(l1.getId());
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800346 try {
347 l1.create();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800348 log.debug("Create {}", l1);
349 try {
350 sw1p2.update();
351 log.debug("Update {}", sw1p2);
352 sw2p1.update();
353 log.debug("Update {}", sw2p1);
354 } catch (ObjectDoesntExistException | WrongVersionException e) {
355 log.error("Port update failed", e);
356 }
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800357 } catch (ObjectExistsException e) {
358 log.error("Link creation failed", e);
359 }
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800360
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800361 log.debug("topology_setup end.");
362 }
363
364 private static void topology_walk() {
365 log.debug("topology_walk start.");
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800366
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800367 RCSwitch sw1 = new RCSwitch(0x1L);
368 try {
369 sw1.read();
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800370 log.debug("{}", sw1);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800371 } catch (ObjectDoesntExistException e) {
372 log.error("Reading switch failed", e);
373 }
374
375 assert (sw1.getDpid() == 0x1L);
376 assert (sw1.getStatus() == STATUS.ACTIVE);
377 assert (sw1.getAllPortIds().size() == 2);
378 for (byte[] portId : sw1.getAllPortIds()) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800379 RCPort port = RCPort.createFromKey(portId);
380 try {
381 port.read();
382 assert (port.getDpid() == 0x1L);
383 log.debug("Port 0x1:{} - LinkIDs:{} DeviceIDs:{}",
384 port.getNumber(), port.getAllLinkIds(),
385 port.getAllDeviceIds());
386
387 for (byte[] deviceId : port.getAllDeviceIds()) {
388 RCDevice device = RCDevice.createFromKey(deviceId);
389 try {
390 device.read();
391 log.debug("Device {} - PortIDs:{}", device.getMac(),
392 device.getAllPortIds());
393 } catch (ObjectDoesntExistException e) {
394 log.error("Reading Device failed", e);
395 }
396 }
397
398 for (byte[] linkId : port.getAllLinkIds()) {
399 RCLink link = RCLink.createFromKey(linkId);
400 try {
401 link.read();
402 log.debug("Link {}", link);
403 } catch (ObjectDoesntExistException e) {
404 log.error("Reading Link failed", e);
405 }
406 }
407
408 } catch (ObjectDoesntExistException e) {
409 log.error("Reading Port failed", e);
410 }
411 }
412
413 RCSwitch sw2 = new RCSwitch(0x1L);
414 try {
415 sw2.read();
416 log.debug("{}", sw2);
417 } catch (ObjectDoesntExistException e) {
418 log.error("Reading switch failed", e);
419 }
420
421 assert (sw2.getDpid() == 0x2L);
422 assert (sw2.getStatus() == STATUS.ACTIVE);
423 assert (sw2.getAllPortIds().size() == 2);
424 for (byte[] portId : sw2.getAllPortIds()) {
425 RCPort port = RCPort.createFromKey(portId);
426 try {
427 port.read();
428 assert (port.getDpid() == 0x1L);
429 log.debug("Port 0x2:{} - LinkIDs:{} DeviceIDs:{}",
430 port.getNumber(), port.getAllLinkIds(),
431 port.getAllDeviceIds());
432
433 for (byte[] deviceId : port.getAllDeviceIds()) {
434 RCDevice device = RCDevice.createFromKey(deviceId);
435 try {
436 device.read();
437 log.debug("Device {} - PortIDs:{}", device,
438 device.getAllPortIds());
439 } catch (ObjectDoesntExistException e) {
440 log.error("Reading Device failed", e);
441 }
442 }
443
444 for (byte[] linkId : port.getAllLinkIds()) {
445 RCLink link = RCLink.createFromKey(linkId);
446 try {
447 link.read();
448 log.debug("Link {}", link);
449 } catch (ObjectDoesntExistException e) {
450 log.error("Reading Link failed", e);
451 }
452 }
453
454 } catch (ObjectDoesntExistException e) {
455 log.error("Reading Port failed", e);
456 }
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800457 }
458
459 log.debug("topology_walk end.");
460 }
461
462 private static void topology_delete() {
463 log.debug("topology_delete start.");
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800464
465 // TODO implement get all kind of API
466 RCSwitch sw1 = new RCSwitch(0x1L);
467 RCPort sw1p1 = new RCPort(0x1L, 1L);
468 RCPort sw1p2 = new RCPort(0x1L, 2L);
469 RCDevice d1 = new RCDevice(new byte[] { 0, 1, 2, 3, 4, 5, 6 });
470 RCLink l1 = new RCLink(0x1L, 2L, 0x2L, 1L);
471 RCSwitch sw2 = new RCSwitch(0x2L);
472 RCPort sw2p1 = new RCPort(0x2L, 1L);
473 RCPort sw2p2 = new RCPort(0x2L, 2L);
474 RCDevice d2 = new RCDevice(new byte[] { 6, 5, 4, 3, 2, 1, 0 });
475
476 try {
477 sw1.read();
478 sw1.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800479 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800480 log.debug("Delete Switch Failed", e);
481 }
482 try {
483 sw1p1.read();
484 sw1p1.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800485 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800486 log.debug("Delete Port Failed", e);
487 }
488 try {
489 sw1p2.read();
490 sw1p2.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800491 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800492 log.debug("Delete Port Failed", e);
493 }
494 try {
495 d1.read();
496 d1.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800497 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800498 log.debug("Delete Device Failed", e);
499 }
500
501 try {
502 l1.read();
503 l1.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800504 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800505 log.debug("Delete Link Failed", e);
506 }
507
508 try {
509 sw2.read();
510 sw2.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800511 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800512 log.debug("Delete Switch Failed", e);
513 }
514 try {
515 sw2p1.read();
516 sw2p1.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800517 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800518 log.debug("Delete Port Failed", e);
519 }
520 try {
521 sw2p2.read();
522 sw2p2.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800523 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800524 log.debug("Delete Port Failed", e);
525 }
526 try {
527 d2.read();
528 d2.delete();
Yuta HIGUCHIc9ca4ac2014-01-31 19:48:31 -0800529 } catch (ObjectDoesntExistException | WrongVersionException e) {
Yuta HIGUCHIc9d06ef2014-01-31 15:55:12 -0800530 log.debug("Delete Device Failed", e);
531 }
532
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -0800533 log.debug("topology_delete end.");
534 }
535
536}