Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 1 | package net.onrc.onos.datastore.topology; |
| 2 | |
| 3 | import java.nio.ByteBuffer; |
| 4 | import java.util.Arrays; |
| 5 | import java.util.Collection; |
| 6 | import java.util.Collections; |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 7 | import java.util.Iterator; |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 8 | import java.util.Map; |
| 9 | import java.util.HashMap; |
| 10 | import java.util.Set; |
| 11 | import java.util.TreeSet; |
| 12 | |
| 13 | import net.onrc.onos.datastore.RCObject; |
| 14 | import net.onrc.onos.datastore.RCTable; |
| 15 | import net.onrc.onos.datastore.utils.ByteArrayComparator; |
| 16 | |
| 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
| 20 | import com.esotericsoftware.kryo.Kryo; |
| 21 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 22 | import edu.stanford.ramcloud.JRamCloud; |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 23 | import edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException; |
| 24 | import edu.stanford.ramcloud.JRamCloud.ObjectExistsException; |
| 25 | import edu.stanford.ramcloud.JRamCloud.WrongVersionException; |
| 26 | |
| 27 | /** |
| 28 | * Switch Object in RC. |
| 29 | * |
| 30 | * @note This class will not maintain invariants. e.g. It will NOT automatically |
| 31 | * remove Ports on Switch, when deleting a Switch. |
| 32 | * |
| 33 | */ |
| 34 | public class RCSwitch extends RCObject { |
| 35 | private static final Logger log = LoggerFactory.getLogger(RCSwitch.class); |
| 36 | |
| 37 | private static final ThreadLocal<Kryo> switchKryo = new ThreadLocal<Kryo>() { |
| 38 | @Override |
| 39 | protected Kryo initialValue() { |
| 40 | Kryo kryo = new Kryo(); |
| 41 | kryo.setRegistrationRequired(true); |
| 42 | kryo.setReferences(false); |
| 43 | kryo.register(byte[].class); |
| 44 | kryo.register(byte[][].class); |
| 45 | kryo.register(HashMap.class); |
| 46 | // TODO check if we should explicitly specify EnumSerializer |
| 47 | kryo.register(STATUS.class); |
| 48 | return kryo; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | public static final String GLOBAL_SWITCH_TABLE_NAME = "G:Switch"; |
| 53 | |
| 54 | // FIXME these should be Enum or some number, not String |
| 55 | private static final String PROP_DPID = "dpid"; |
| 56 | private static final String PROP_STATUS = "status"; |
| 57 | private static final String PROP_PORT_IDS = "port-ids"; |
| 58 | |
| 59 | // must not re-order enum members, ordinal will be sent over wire |
| 60 | public enum STATUS { |
| 61 | INACTIVE, ACTIVE; |
| 62 | } |
| 63 | |
| 64 | private final Long dpid; |
| 65 | private STATUS status; |
| 66 | private TreeSet<byte[]> portIds; |
| 67 | transient private boolean isPortIdsModified; |
| 68 | |
| 69 | public static final int SWITCHID_BYTES = 2 + 8; |
| 70 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 71 | public static byte[] getSwitchID(Long dpid) { |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 72 | if (dpid == null) { |
| 73 | throw new IllegalArgumentException("dpid cannot be null"); |
| 74 | } |
| 75 | return ByteBuffer.allocate(SWITCHID_BYTES).putChar('S').putLong(dpid) |
| 76 | .array(); |
| 77 | } |
| 78 | |
| 79 | public static long getDpidFromKey(byte[] key) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 80 | return getDpidFromKey(ByteBuffer.wrap(key)); |
| 81 | } |
| 82 | |
| 83 | public static long getDpidFromKey(ByteBuffer keyBuf) { |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 84 | if (keyBuf.getChar() != 'S') { |
| 85 | throw new IllegalArgumentException("Invalid Switch key"); |
| 86 | } |
| 87 | return keyBuf.getLong(); |
| 88 | } |
| 89 | |
| 90 | // FIXME specify DPID here, or Should caller specify the key it self? |
| 91 | // In other words, should layer above have the control of the ID? |
| 92 | public RCSwitch(Long dpid) { |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 93 | super(RCTable.getTable(GLOBAL_SWITCH_TABLE_NAME), getSwitchID(dpid)); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 94 | |
| 95 | this.dpid = dpid; |
| 96 | this.status = STATUS.INACTIVE; |
| 97 | this.portIds = new TreeSet<>(ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 98 | this.isPortIdsModified = true; |
| 99 | } |
| 100 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 101 | /** |
| 102 | * Get an instance from Key. |
| 103 | * |
| 104 | * @note You need to call `read()` to get the DB content. |
| 105 | * @param key |
| 106 | * @return RCSwitch instance |
| 107 | */ |
| 108 | public static <SW extends RCObject> SW createFromKey(byte[] key) { |
| 109 | @SuppressWarnings("unchecked") |
| 110 | SW sw = (SW) new RCSwitch(getDpidFromKey(key)); |
| 111 | return sw; |
| 112 | } |
| 113 | |
| 114 | public static Iterable<RCSwitch> getAllSwitches() { |
| 115 | return new SwitchEnumerator(); |
| 116 | } |
| 117 | |
| 118 | public static class SwitchEnumerator implements Iterable<RCSwitch> { |
| 119 | |
| 120 | @Override |
| 121 | public Iterator<RCSwitch> iterator() { |
| 122 | return new SwitchIterator(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | public static class SwitchIterator extends ObjectIterator<RCSwitch> { |
| 127 | |
| 128 | public SwitchIterator() { |
| 129 | super(RCTable.getTable(GLOBAL_SWITCH_TABLE_NAME)); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public RCSwitch next() { |
| 134 | JRamCloud.Object o = enumerator.next(); |
| 135 | RCSwitch e = RCSwitch.createFromKey(o.key); |
| 136 | e.setValueAndDeserialize(o.value, o.version); |
| 137 | return e; |
| 138 | } |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | public STATUS getStatus() { |
| 142 | return status; |
| 143 | } |
| 144 | |
| 145 | public void setStatus(STATUS status) { |
| 146 | this.status = status; |
| 147 | getObjectMap().put(PROP_STATUS, status); |
| 148 | } |
| 149 | |
| 150 | public Long getDpid() { |
| 151 | return dpid; |
| 152 | } |
| 153 | |
| 154 | public byte[] getId() { |
| 155 | return getKey(); |
| 156 | } |
| 157 | |
| 158 | public void addPortId(byte[] portId) { |
| 159 | // TODO: Should we copy portId, or reference is OK. |
| 160 | isPortIdsModified |= portIds.add(portId); |
| 161 | } |
| 162 | |
| 163 | public void removePortId(byte[] portId) { |
| 164 | isPortIdsModified |= portIds.remove(portId); |
| 165 | } |
| 166 | |
| 167 | public void emptyPortIds() { |
| 168 | portIds.clear(); |
| 169 | this.isPortIdsModified = true; |
| 170 | } |
| 171 | |
| 172 | public void addAllToPortIds(Collection<byte[]> portIds) { |
| 173 | // TODO: Should we copy portId, or reference is OK. |
| 174 | isPortIdsModified |= this.portIds.addAll(portIds); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * |
| 179 | * @return Unmodifiable Set view of all the PortIds; |
| 180 | */ |
| 181 | public Set<byte[]> getAllPortIds() { |
| 182 | return Collections.unmodifiableSet(portIds); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void serializeAndSetValue() { |
| 187 | Map<Object, Object> map = getObjectMap(); |
| 188 | |
| 189 | map.put(PROP_DPID, this.dpid); |
| 190 | if (isPortIdsModified) { |
| 191 | byte[] portIdArray[] = new byte[portIds.size()][]; |
| 192 | map.put(PROP_PORT_IDS, portIds.toArray(portIdArray)); |
| 193 | isPortIdsModified = false; |
| 194 | } |
| 195 | |
| 196 | serializeAndSetValue(switchKryo.get(), map); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public Map<Object, Object> deserializeObjectFromValue() { |
| 201 | Map<Object, Object> map = deserializeObjectFromValue(switchKryo.get()); |
| 202 | |
| 203 | this.status = (STATUS) map.get(PROP_STATUS); |
| 204 | |
| 205 | if (this.portIds == null) { |
| 206 | this.portIds = new TreeSet<>( |
| 207 | ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 208 | } |
| 209 | byte[] portIdArray[] = (byte[][]) map.get(PROP_PORT_IDS); |
| 210 | if (portIdArray != null) { |
| 211 | this.portIds.clear(); |
| 212 | this.portIds.addAll(Arrays.asList(portIdArray)); |
| 213 | isPortIdsModified = false; |
| 214 | } else { |
| 215 | // trigger write on next serialize |
| 216 | isPortIdsModified = true; |
| 217 | } |
| 218 | return map; |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public String toString() { |
| 223 | // TODO OUTPUT ALL? |
| 224 | return "[RCSwitch 0x" + Long.toHexString(dpid) + " STATUS:" + status |
| 225 | + "]"; |
| 226 | } |
| 227 | |
| 228 | public static void main(String argv[]) { |
| 229 | // create active switch 0x1 with 2 ports |
| 230 | RCSwitch sw = new RCSwitch(0x1L); |
| 231 | sw.setStatus(STATUS.ACTIVE); |
| 232 | sw.addPortId("SW0x0001P001".getBytes()); |
| 233 | sw.addPortId("SW0x0001P002".getBytes()); |
| 234 | |
| 235 | try { |
| 236 | sw.create(); |
| 237 | } catch (ObjectExistsException e) { |
| 238 | log.debug("Create Switch Failed", e); |
| 239 | e.printStackTrace(); |
| 240 | } |
| 241 | |
| 242 | // read switch 0x1 |
| 243 | RCSwitch swRead = new RCSwitch(0x1L); |
| 244 | try { |
| 245 | swRead.read(); |
| 246 | } catch (ObjectDoesntExistException e) { |
| 247 | log.debug("Reading Switch Failed", e); |
| 248 | } |
| 249 | assert (swRead.getStatus() == STATUS.ACTIVE); |
| 250 | for (byte[] portId : swRead.getAllPortIds()) { |
| 251 | // bad example code, portId is not expected to be ASCII string |
| 252 | log.debug("PortId: {}", new String(portId)); |
| 253 | } |
| 254 | assert (swRead.getAllPortIds().size() == 2); |
| 255 | |
| 256 | // update 0x1 |
| 257 | swRead.setStatus(STATUS.INACTIVE); |
| 258 | swRead.removePortId("SW0x0001P001".getBytes()); |
| 259 | try { |
| 260 | swRead.update(); |
| 261 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 262 | log.debug("Updating Switch Failed", e); |
| 263 | } |
| 264 | |
| 265 | // read 0x1 again and delete |
| 266 | RCSwitch swRead2 = new RCSwitch(0x1L); |
| 267 | try { |
| 268 | swRead2.read(); |
| 269 | } catch (ObjectDoesntExistException e) { |
| 270 | log.debug("Reading Switch Again Failed", e); |
| 271 | } |
| 272 | assert (swRead2.getStatus() == STATUS.INACTIVE); |
| 273 | for (byte[] portId : swRead2.getAllPortIds()) { |
| 274 | // bad example code, portId is not expected to be ASCII string |
| 275 | log.debug("PortId: {}", new String(portId)); |
| 276 | } |
| 277 | assert (swRead2.getAllPortIds().size() == 1); |
| 278 | try { |
| 279 | swRead2.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 280 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 281 | log.debug("Deleting Switch Failed", e); |
| 282 | } |
| 283 | |
| 284 | RCSwitch swRead3 = new RCSwitch(0x1L); |
| 285 | try { |
| 286 | swRead3.read(); |
| 287 | } catch (ObjectDoesntExistException e) { |
| 288 | log.debug("Switch not found as expected"); |
| 289 | } |
| 290 | |
| 291 | topology_setup(); |
| 292 | topology_walk(); |
| 293 | topology_delete(); |
| 294 | } |
| 295 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 296 | @Deprecated |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 297 | private static void topology_setup() { |
| 298 | log.debug("topology_setup start."); |
| 299 | |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 300 | // d1 - s1p1 - s1 - s1p2 - s2p1 - s2 - s2p2 |
| 301 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 302 | RCSwitch sw1 = new RCSwitch(0x1L); |
| 303 | sw1.setStatus(STATUS.ACTIVE); |
| 304 | try { |
| 305 | sw1.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 306 | log.debug("Create {}", sw1); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 307 | } catch (ObjectExistsException e) { |
| 308 | log.error("Switch creation failed", e); |
| 309 | } |
| 310 | |
| 311 | RCPort sw1p1 = new RCPort(0x1L, 1L); |
| 312 | sw1p1.setStatus(RCPort.STATUS.ACTIVE); |
| 313 | RCPort sw1p2 = new RCPort(0x1L, 2L); |
| 314 | sw1p2.setStatus(RCPort.STATUS.ACTIVE); |
| 315 | try { |
| 316 | sw1p1.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 317 | log.debug("Create {}", sw1p1); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 318 | sw1p2.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 319 | log.debug("Create {}", sw1p2); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 320 | } catch (ObjectExistsException e) { |
| 321 | log.error("Port creation failed", e); |
| 322 | } |
| 323 | |
| 324 | sw1.emptyPortIds(); |
| 325 | sw1.addPortId(sw1p1.getId()); |
| 326 | sw1.addPortId(sw1p2.getId()); |
| 327 | try { |
| 328 | sw1.update(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 329 | log.debug("Update {} - {}", sw1, sw1.getAllPortIds()); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 330 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 331 | log.error("Switch update failed", e); |
| 332 | } |
| 333 | |
| 334 | RCDevice d1 = new RCDevice(new byte[] { 0, 1, 2, 3, 4, 5, 6 }); |
| 335 | d1.addPortId(sw1p1.getId()); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 336 | sw1p1.addDeviceId(d1.getId()); |
| 337 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 338 | try { |
| 339 | d1.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 340 | log.debug("Create {}", d1); |
| 341 | try { |
| 342 | sw1p1.update(); |
| 343 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 344 | log.error("Link update failed", e); |
| 345 | } |
| 346 | log.debug("Create {}", sw1p1); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 347 | } catch (ObjectExistsException e) { |
| 348 | log.error("Device creation failed", e); |
| 349 | } |
| 350 | |
| 351 | RCSwitch sw2 = new RCSwitch(0x2L); |
| 352 | sw2.setStatus(STATUS.ACTIVE); |
| 353 | RCPort sw2p1 = new RCPort(0x2L, 1L); |
| 354 | sw2p1.setStatus(RCPort.STATUS.ACTIVE); |
| 355 | RCPort sw2p2 = new RCPort(0x2L, 2L); |
| 356 | sw2p2.setStatus(RCPort.STATUS.ACTIVE); |
| 357 | |
| 358 | sw2.addPortId(sw2p1.getId()); |
| 359 | sw2.addPortId(sw2p2.getId()); |
| 360 | sw2.addAllToPortIds(Arrays.asList(sw2p1.getId(), sw2p2.getId())); |
| 361 | assert (sw2.getAllPortIds().size() == 2); |
| 362 | |
| 363 | RCDevice d2 = new RCDevice(new byte[] { 6, 5, 4, 3, 2, 1, 0 }); |
| 364 | d2.addPortId(sw2p2.getId()); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 365 | sw2p2.addDeviceId(d2.getId()); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 366 | |
| 367 | try { |
| 368 | sw2.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 369 | log.debug("Create {}", sw2); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 370 | sw2p1.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 371 | log.debug("Create {}", sw2p1); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 372 | sw2p2.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 373 | log.debug("Create {}", sw2p2); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 374 | d2.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 375 | log.debug("Create {}", d2); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 376 | } catch (ObjectExistsException e) { |
| 377 | log.error("One of Switch/Port/Device creation failed", e); |
| 378 | } |
| 379 | |
| 380 | RCLink l1 = new RCLink(0x1L, 2L, 0x2L, 1L); |
| 381 | l1.setStatus(RCLink.STATUS.ACTIVE); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 382 | |
| 383 | sw1p2.addLinkId(l1.getId()); |
| 384 | sw2p1.addLinkId(l1.getId()); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 385 | try { |
| 386 | l1.create(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 387 | log.debug("Create {}", l1); |
| 388 | try { |
| 389 | sw1p2.update(); |
| 390 | log.debug("Update {}", sw1p2); |
| 391 | sw2p1.update(); |
| 392 | log.debug("Update {}", sw2p1); |
| 393 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 394 | log.error("Port update failed", e); |
| 395 | } |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 396 | } catch (ObjectExistsException e) { |
| 397 | log.error("Link creation failed", e); |
| 398 | } |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 399 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 400 | log.debug("topology_setup end."); |
| 401 | } |
| 402 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 403 | @Deprecated |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 404 | private static void topology_walk() { |
| 405 | log.debug("topology_walk start."); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 406 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 407 | Iterable<RCSwitch> swIt = RCSwitch.getAllSwitches(); |
| 408 | log.debug("Enumerating Switches start"); |
| 409 | for (RCSwitch sw : swIt) { |
| 410 | log.debug("{}", sw); |
| 411 | } |
| 412 | log.debug("Enumerating Switches end"); |
| 413 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 414 | RCSwitch sw1 = new RCSwitch(0x1L); |
| 415 | try { |
| 416 | sw1.read(); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 417 | log.debug("{}", sw1); |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 418 | } catch (ObjectDoesntExistException e) { |
| 419 | log.error("Reading switch failed", e); |
| 420 | } |
| 421 | |
| 422 | assert (sw1.getDpid() == 0x1L); |
| 423 | assert (sw1.getStatus() == STATUS.ACTIVE); |
| 424 | assert (sw1.getAllPortIds().size() == 2); |
| 425 | for (byte[] portId : sw1.getAllPortIds()) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 426 | RCPort port = RCPort.createFromKey(portId); |
| 427 | try { |
| 428 | port.read(); |
| 429 | assert (port.getDpid() == 0x1L); |
| 430 | log.debug("Port 0x1:{} - LinkIDs:{} DeviceIDs:{}", |
| 431 | port.getNumber(), port.getAllLinkIds(), |
| 432 | port.getAllDeviceIds()); |
| 433 | |
| 434 | for (byte[] deviceId : port.getAllDeviceIds()) { |
| 435 | RCDevice device = RCDevice.createFromKey(deviceId); |
| 436 | try { |
| 437 | device.read(); |
| 438 | log.debug("Device {} - PortIDs:{}", device.getMac(), |
| 439 | device.getAllPortIds()); |
| 440 | } catch (ObjectDoesntExistException e) { |
| 441 | log.error("Reading Device failed", e); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | for (byte[] linkId : port.getAllLinkIds()) { |
| 446 | RCLink link = RCLink.createFromKey(linkId); |
| 447 | try { |
| 448 | link.read(); |
| 449 | log.debug("Link {}", link); |
| 450 | } catch (ObjectDoesntExistException e) { |
| 451 | log.error("Reading Link failed", e); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | } catch (ObjectDoesntExistException e) { |
| 456 | log.error("Reading Port failed", e); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | RCSwitch sw2 = new RCSwitch(0x1L); |
| 461 | try { |
| 462 | sw2.read(); |
| 463 | log.debug("{}", sw2); |
| 464 | } catch (ObjectDoesntExistException e) { |
| 465 | log.error("Reading switch failed", e); |
| 466 | } |
| 467 | |
| 468 | assert (sw2.getDpid() == 0x2L); |
| 469 | assert (sw2.getStatus() == STATUS.ACTIVE); |
| 470 | assert (sw2.getAllPortIds().size() == 2); |
| 471 | for (byte[] portId : sw2.getAllPortIds()) { |
| 472 | RCPort port = RCPort.createFromKey(portId); |
| 473 | try { |
| 474 | port.read(); |
| 475 | assert (port.getDpid() == 0x1L); |
| 476 | log.debug("Port 0x2:{} - LinkIDs:{} DeviceIDs:{}", |
| 477 | port.getNumber(), port.getAllLinkIds(), |
| 478 | port.getAllDeviceIds()); |
| 479 | |
| 480 | for (byte[] deviceId : port.getAllDeviceIds()) { |
| 481 | RCDevice device = RCDevice.createFromKey(deviceId); |
| 482 | try { |
| 483 | device.read(); |
| 484 | log.debug("Device {} - PortIDs:{}", device, |
| 485 | device.getAllPortIds()); |
| 486 | } catch (ObjectDoesntExistException e) { |
| 487 | log.error("Reading Device failed", e); |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | for (byte[] linkId : port.getAllLinkIds()) { |
| 492 | RCLink link = RCLink.createFromKey(linkId); |
| 493 | try { |
| 494 | link.read(); |
| 495 | log.debug("Link {}", link); |
| 496 | } catch (ObjectDoesntExistException e) { |
| 497 | log.error("Reading Link failed", e); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | } catch (ObjectDoesntExistException e) { |
| 502 | log.error("Reading Port failed", e); |
| 503 | } |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | log.debug("topology_walk end."); |
| 507 | } |
| 508 | |
Yuta HIGUCHI | 10eebea | 2014-02-03 10:41:41 -0800 | [diff] [blame^] | 509 | @Deprecated |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 510 | private static void topology_delete() { |
| 511 | log.debug("topology_delete start."); |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 512 | |
| 513 | // TODO implement get all kind of API |
| 514 | RCSwitch sw1 = new RCSwitch(0x1L); |
| 515 | RCPort sw1p1 = new RCPort(0x1L, 1L); |
| 516 | RCPort sw1p2 = new RCPort(0x1L, 2L); |
| 517 | RCDevice d1 = new RCDevice(new byte[] { 0, 1, 2, 3, 4, 5, 6 }); |
| 518 | RCLink l1 = new RCLink(0x1L, 2L, 0x2L, 1L); |
| 519 | RCSwitch sw2 = new RCSwitch(0x2L); |
| 520 | RCPort sw2p1 = new RCPort(0x2L, 1L); |
| 521 | RCPort sw2p2 = new RCPort(0x2L, 2L); |
| 522 | RCDevice d2 = new RCDevice(new byte[] { 6, 5, 4, 3, 2, 1, 0 }); |
| 523 | |
| 524 | try { |
| 525 | sw1.read(); |
| 526 | sw1.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 527 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 528 | log.debug("Delete Switch Failed", e); |
| 529 | } |
| 530 | try { |
| 531 | sw1p1.read(); |
| 532 | sw1p1.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 533 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 534 | log.debug("Delete Port Failed", e); |
| 535 | } |
| 536 | try { |
| 537 | sw1p2.read(); |
| 538 | sw1p2.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 539 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 540 | log.debug("Delete Port Failed", e); |
| 541 | } |
| 542 | try { |
| 543 | d1.read(); |
| 544 | d1.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 545 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 546 | log.debug("Delete Device Failed", e); |
| 547 | } |
| 548 | |
| 549 | try { |
| 550 | l1.read(); |
| 551 | l1.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 552 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 553 | log.debug("Delete Link Failed", e); |
| 554 | } |
| 555 | |
| 556 | try { |
| 557 | sw2.read(); |
| 558 | sw2.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 559 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 560 | log.debug("Delete Switch Failed", e); |
| 561 | } |
| 562 | try { |
| 563 | sw2p1.read(); |
| 564 | sw2p1.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 565 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 566 | log.debug("Delete Port Failed", e); |
| 567 | } |
| 568 | try { |
| 569 | sw2p2.read(); |
| 570 | sw2p2.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 571 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 572 | log.debug("Delete Port Failed", e); |
| 573 | } |
| 574 | try { |
| 575 | d2.read(); |
| 576 | d2.delete(); |
Yuta HIGUCHI | c9ca4ac | 2014-01-31 19:48:31 -0800 | [diff] [blame] | 577 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
Yuta HIGUCHI | c9d06ef | 2014-01-31 15:55:12 -0800 | [diff] [blame] | 578 | log.debug("Delete Device Failed", e); |
| 579 | } |
| 580 | |
Yuta HIGUCHI | 1ef85c4 | 2014-01-29 17:23:21 -0800 | [diff] [blame] | 581 | log.debug("topology_delete end."); |
| 582 | } |
| 583 | |
| 584 | } |