samuel

[ONOS-2159]Add to query devices by type api in DeviceService and

DeviceStore interfaces

Change-Id: Ifa7e94e08eb150fb4d71248a50a390832d1549df
...@@ -44,6 +44,15 @@ public interface DeviceService { ...@@ -44,6 +44,15 @@ public interface DeviceService {
44 Iterable<Device> getDevices(); 44 Iterable<Device> getDevices();
45 45
46 /** 46 /**
47 + * Returns a collection of the currently known infrastructure
48 + * devices by device type.
49 + *
50 + * @param type device type
51 + * @return collection of devices
52 + */
53 + Iterable<Device> getDevices(Device.Type type);
54 +
55 + /**
47 * Returns an iterable collection of all devices 56 * Returns an iterable collection of all devices
48 * currently available to the system. 57 * currently available to the system.
49 * 58 *
...@@ -52,6 +61,14 @@ public interface DeviceService { ...@@ -52,6 +61,14 @@ public interface DeviceService {
52 Iterable<Device> getAvailableDevices(); 61 Iterable<Device> getAvailableDevices();
53 62
54 /** 63 /**
64 + * Returns an iterable collection of all devices currently available to the system by device type.
65 + *
66 + * @param type device type
67 + * @return device collection
68 + */
69 + Iterable<Device> getAvailableDevices(Device.Type type);
70 +
71 + /**
55 * Returns the device with the specified identifier. 72 * Returns the device with the specified identifier.
56 * 73 *
57 * @param deviceId device identifier 74 * @param deviceId device identifier
......
...@@ -19,6 +19,7 @@ import com.google.common.base.Predicate; ...@@ -19,6 +19,7 @@ import com.google.common.base.Predicate;
19 import com.google.common.collect.FluentIterable; 19 import com.google.common.collect.FluentIterable;
20 20
21 import org.onosproject.net.Device; 21 import org.onosproject.net.Device;
22 +import org.onosproject.net.Device.Type;
22 import org.onosproject.net.DeviceId; 23 import org.onosproject.net.DeviceId;
23 import org.onosproject.net.MastershipRole; 24 import org.onosproject.net.MastershipRole;
24 import org.onosproject.net.Port; 25 import org.onosproject.net.Port;
...@@ -91,4 +92,14 @@ public class DeviceServiceAdapter implements DeviceService { ...@@ -91,4 +92,14 @@ public class DeviceServiceAdapter implements DeviceService {
91 public void removeListener(DeviceListener listener) { 92 public void removeListener(DeviceListener listener) {
92 } 93 }
93 94
95 + @Override
96 + public Iterable<Device> getDevices(Type type) {
97 + return Collections.emptyList();
98 + }
99 +
100 + @Override
101 + public Iterable<Device> getAvailableDevices(Type type) {
102 + return Collections.emptyList();
103 + }
104 +
94 } 105 }
......
...@@ -15,7 +15,25 @@ ...@@ -15,7 +15,25 @@
15 */ 15 */
16 package org.onosproject.net.device.impl; 16 package org.onosproject.net.device.impl;
17 17
18 -import com.google.common.collect.Lists; 18 +import static com.google.common.base.Preconditions.checkNotNull;
19 +import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
20 +import static org.onlab.util.Tools.groupedThreads;
21 +import static org.onosproject.net.MastershipRole.MASTER;
22 +import static org.onosproject.net.MastershipRole.NONE;
23 +import static org.onosproject.net.MastershipRole.STANDBY;
24 +import static org.onosproject.security.AppGuard.checkPermission;
25 +import static org.slf4j.LoggerFactory.getLogger;
26 +
27 +import java.util.Collection;
28 +import java.util.HashSet;
29 +import java.util.List;
30 +import java.util.Objects;
31 +import java.util.Set;
32 +import java.util.concurrent.CompletableFuture;
33 +import java.util.concurrent.ExecutionException;
34 +import java.util.concurrent.ScheduledExecutorService;
35 +import java.util.concurrent.TimeUnit;
36 +
19 import org.apache.felix.scr.annotations.Activate; 37 import org.apache.felix.scr.annotations.Activate;
20 import org.apache.felix.scr.annotations.Component; 38 import org.apache.felix.scr.annotations.Component;
21 import org.apache.felix.scr.annotations.Deactivate; 39 import org.apache.felix.scr.annotations.Deactivate;
...@@ -25,14 +43,15 @@ import org.apache.felix.scr.annotations.Service; ...@@ -25,14 +43,15 @@ import org.apache.felix.scr.annotations.Service;
25 import org.onosproject.cluster.ClusterService; 43 import org.onosproject.cluster.ClusterService;
26 import org.onosproject.cluster.NodeId; 44 import org.onosproject.cluster.NodeId;
27 import org.onosproject.core.Permission; 45 import org.onosproject.core.Permission;
28 -import org.onosproject.event.ListenerRegistry;
29 import org.onosproject.event.EventDeliveryService; 46 import org.onosproject.event.EventDeliveryService;
47 +import org.onosproject.event.ListenerRegistry;
30 import org.onosproject.mastership.MastershipEvent; 48 import org.onosproject.mastership.MastershipEvent;
31 import org.onosproject.mastership.MastershipListener; 49 import org.onosproject.mastership.MastershipListener;
32 import org.onosproject.mastership.MastershipService; 50 import org.onosproject.mastership.MastershipService;
33 import org.onosproject.mastership.MastershipTerm; 51 import org.onosproject.mastership.MastershipTerm;
34 import org.onosproject.mastership.MastershipTermService; 52 import org.onosproject.mastership.MastershipTermService;
35 import org.onosproject.net.Device; 53 import org.onosproject.net.Device;
54 +import org.onosproject.net.Device.Type;
36 import org.onosproject.net.DeviceId; 55 import org.onosproject.net.DeviceId;
37 import org.onosproject.net.MastershipRole; 56 import org.onosproject.net.MastershipRole;
38 import org.onosproject.net.Port; 57 import org.onosproject.net.Port;
...@@ -56,21 +75,7 @@ import org.onosproject.net.provider.AbstractProviderRegistry; ...@@ -56,21 +75,7 @@ import org.onosproject.net.provider.AbstractProviderRegistry;
56 import org.onosproject.net.provider.AbstractProviderService; 75 import org.onosproject.net.provider.AbstractProviderService;
57 import org.slf4j.Logger; 76 import org.slf4j.Logger;
58 77
59 -import java.util.Collection; 78 +import com.google.common.collect.Lists;
60 -import java.util.List;
61 -import java.util.Objects;
62 -import java.util.concurrent.CompletableFuture;
63 -import java.util.concurrent.ExecutionException;
64 -import java.util.concurrent.ScheduledExecutorService;
65 -import java.util.concurrent.TimeUnit;
66 -
67 -import static com.google.common.base.Preconditions.checkNotNull;
68 -import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
69 -import static org.onlab.util.Tools.groupedThreads;
70 -import static org.onosproject.net.MastershipRole.*;
71 -import static org.slf4j.LoggerFactory.getLogger;
72 -import static org.onosproject.security.AppGuard.checkPermission;
73 -
74 79
75 /** 80 /**
76 * Provides implementation of the device SB &amp; NB APIs. 81 * Provides implementation of the device SB &amp; NB APIs.
...@@ -88,8 +93,7 @@ public class DeviceManager ...@@ -88,8 +93,7 @@ public class DeviceManager
88 93
89 private final Logger log = getLogger(getClass()); 94 private final Logger log = getLogger(getClass());
90 95
91 - protected final ListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry = 96 + protected final ListenerRegistry<DeviceEvent, DeviceListener> listenerRegistry = new ListenerRegistry<>();
92 - new ListenerRegistry<>();
93 97
94 private final DeviceStoreDelegate delegate = new InternalStoreDelegate(); 98 private final DeviceStoreDelegate delegate = new InternalStoreDelegate();
95 99
...@@ -118,7 +122,8 @@ public class DeviceManager ...@@ -118,7 +122,8 @@ public class DeviceManager
118 122
119 @Activate 123 @Activate
120 public void activate() { 124 public void activate() {
121 - backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device", "manager-background")); 125 + backgroundService = newSingleThreadScheduledExecutor(groupedThreads("onos/device",
126 + "manager-background"));
122 localNodeId = clusterService.getLocalNode().id(); 127 localNodeId = clusterService.getLocalNode().id();
123 128
124 store.setDelegate(delegate); 129 store.setDelegate(delegate);
...@@ -258,8 +263,7 @@ public class DeviceManager ...@@ -258,8 +263,7 @@ public class DeviceManager
258 } 263 }
259 264
260 @Override 265 @Override
261 - protected DeviceProviderService createProviderService( 266 + protected DeviceProviderService createProviderService(DeviceProvider provider) {
262 - DeviceProvider provider) {
263 return new InternalDeviceProviderService(provider); 267 return new InternalDeviceProviderService(provider);
264 } 268 }
265 269
...@@ -280,7 +284,8 @@ public class DeviceManager ...@@ -280,7 +284,8 @@ public class DeviceManager
280 continue; 284 continue;
281 } 285 }
282 286
283 - log.info("{} is reachable but did not have a valid role, reasserting", deviceId); 287 + log.info("{} is reachable but did not have a valid role, reasserting",
288 + deviceId);
284 289
285 // isReachable but was not MASTER or STANDBY, get a role and apply 290 // isReachable but was not MASTER or STANDBY, get a role and apply
286 // Note: NONE triggers request to MastershipService 291 // Note: NONE triggers request to MastershipService
...@@ -300,20 +305,21 @@ public class DeviceManager ...@@ -300,20 +305,21 @@ public class DeviceManager
300 /** 305 /**
301 * Apply role in reaction to provider event. 306 * Apply role in reaction to provider event.
302 * 307 *
303 - * @param deviceId device identifier 308 + * @param deviceId device identifier
304 - * @param newRole new role to apply to the device 309 + * @param newRole new role to apply to the device
305 * @return true if the request was sent to provider 310 * @return true if the request was sent to provider
306 */ 311 */
307 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) { 312 private boolean applyRole(DeviceId deviceId, MastershipRole newRole) {
308 313
309 if (newRole.equals(MastershipRole.NONE)) { 314 if (newRole.equals(MastershipRole.NONE)) {
310 - //no-op 315 + // no-op
311 return true; 316 return true;
312 } 317 }
313 318
314 DeviceProvider provider = provider(); 319 DeviceProvider provider = provider();
315 if (provider == null) { 320 if (provider == null) {
316 - log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); 321 + log.warn("Provider for {} was not found. Cannot apply role {}",
322 + deviceId, newRole);
317 return false; 323 return false;
318 } 324 }
319 provider.roleChanged(deviceId, newRole); 325 provider.roleChanged(deviceId, newRole);
...@@ -322,7 +328,6 @@ public class DeviceManager ...@@ -322,7 +328,6 @@ public class DeviceManager
322 return true; 328 return true;
323 } 329 }
324 330
325 -
326 @Override 331 @Override
327 public void deviceConnected(DeviceId deviceId, 332 public void deviceConnected(DeviceId deviceId,
328 DeviceDescription deviceDescription) { 333 DeviceDescription deviceDescription) {
...@@ -332,14 +337,16 @@ public class DeviceManager ...@@ -332,14 +337,16 @@ public class DeviceManager
332 337
333 log.info("Device {} connected", deviceId); 338 log.info("Device {} connected", deviceId);
334 // check my Role 339 // check my Role
335 - CompletableFuture<MastershipRole> role = mastershipService.requestRoleFor(deviceId); 340 + CompletableFuture<MastershipRole> role = mastershipService
341 + .requestRoleFor(deviceId);
336 try { 342 try {
337 // Device subsystem must wait for role assignment 343 // Device subsystem must wait for role assignment
338 // to avoid losing Device information. 344 // to avoid losing Device information.
339 // (This node could be the only Node connected to the Device.) 345 // (This node could be the only Node connected to the Device.)
340 role.get(); 346 role.get();
341 } catch (InterruptedException e) { 347 } catch (InterruptedException e) {
342 - log.warn("Interrupted while waiting role-assignment for {}", deviceId); 348 + log.warn("Interrupted while waiting role-assignment for {}",
349 + deviceId);
343 Thread.currentThread().interrupt(); 350 Thread.currentThread().interrupt();
344 } catch (ExecutionException e) { 351 } catch (ExecutionException e) {
345 log.error("Exception thrown while waiting role-assignment for {}", 352 log.error("Exception thrown while waiting role-assignment for {}",
...@@ -358,7 +365,8 @@ public class DeviceManager ...@@ -358,7 +365,8 @@ public class DeviceManager
358 } 365 }
359 366
360 DeviceEvent event = store.createOrUpdateDevice(provider().id(), 367 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
361 - deviceId, deviceDescription); 368 + deviceId,
369 + deviceDescription);
362 370
363 if (event != null) { 371 if (event != null) {
364 log.trace("event: {} {}", event.type(), event); 372 log.trace("event: {} {}", event.type(), event);
...@@ -375,10 +383,8 @@ public class DeviceManager ...@@ -375,10 +383,8 @@ public class DeviceManager
375 383
376 List<Port> ports = store.getPorts(deviceId); 384 List<Port> ports = store.getPorts(deviceId);
377 List<PortDescription> descs = Lists.newArrayList(); 385 List<PortDescription> descs = Lists.newArrayList();
378 - ports.forEach(port -> 386 + ports.forEach(port -> descs.add(new DefaultPortDescription(port
379 - descs.add(new DefaultPortDescription(port.number(), 387 + .number(), false, port.type(), port.portSpeed())));
380 - false, port.type(),
381 - port.portSpeed())));
382 store.updatePorts(this.provider().id(), deviceId, descs); 388 store.updatePorts(this.provider().id(), deviceId, descs);
383 try { 389 try {
384 if (mastershipService.getLocalRole(deviceId) == MASTER) { 390 if (mastershipService.getLocalRole(deviceId) == MASTER) {
...@@ -387,37 +393,49 @@ public class DeviceManager ...@@ -387,37 +393,49 @@ public class DeviceManager
387 } catch (IllegalStateException e) { 393 } catch (IllegalStateException e) {
388 log.warn("Failed to mark {} offline", deviceId); 394 log.warn("Failed to mark {} offline", deviceId);
389 // only the MASTER should be marking off-line in normal cases, 395 // only the MASTER should be marking off-line in normal cases,
390 - // but if I was the last STANDBY connection, etc. and no one else 396 + // but if I was the last STANDBY connection, etc. and no one
391 - // was there to mark the device offline, this instance may need to 397 + // else
398 + // was there to mark the device offline, this instance may need
399 + // to
392 // temporarily request for Master Role and mark offline. 400 // temporarily request for Master Role and mark offline.
393 401
394 - //there are times when this node will correctly have mastership, BUT 402 + // there are times when this node will correctly have
395 - //that isn't reflected in the ClockManager before the device disconnects. 403 + // mastership, BUT
396 - //we want to let go of the device anyways, so make sure this happens. 404 + // that isn't reflected in the ClockManager before the device
405 + // disconnects.
406 + // we want to let go of the device anyways, so make sure this
407 + // happens.
397 408
398 // FIXME: Store semantics leaking out as IllegalStateException. 409 // FIXME: Store semantics leaking out as IllegalStateException.
399 - // Consider revising store API to handle this scenario. 410 + // Consider revising store API to handle this scenario.
400 - CompletableFuture<MastershipRole> roleFuture = mastershipService.requestRoleFor(deviceId); 411 + CompletableFuture<MastershipRole> roleFuture = mastershipService
412 + .requestRoleFor(deviceId);
401 roleFuture.whenComplete((role, error) -> { 413 roleFuture.whenComplete((role, error) -> {
402 - MastershipTerm term = termService.getMastershipTerm(deviceId); 414 + MastershipTerm term = termService
403 - // TODO: Move this type of check inside device clock manager, etc. 415 + .getMastershipTerm(deviceId);
404 - if (term != null && localNodeId.equals(term.master())) { 416 + // TODO: Move this type of check inside device clock
405 - log.info("Retry marking {} offline", deviceId); 417 + // manager, etc.
406 - deviceClockProviderService.setMastershipTerm(deviceId, term); 418 + if (term != null && localNodeId.equals(term.master())) {
407 - post(store.markOffline(deviceId)); 419 + log.info("Retry marking {} offline", deviceId);
408 - } else { 420 + deviceClockProviderService
409 - log.info("Failed again marking {} offline. {}", deviceId, role); 421 + .setMastershipTerm(deviceId, term);
410 - } 422 + post(store.markOffline(deviceId));
411 - }); 423 + } else {
424 + log.info("Failed again marking {} offline. {}",
425 + deviceId, role);
426 + }
427 + });
412 } finally { 428 } finally {
413 try { 429 try {
414 - //relinquish master role and ability to be backup. 430 + // relinquish master role and ability to be backup.
415 mastershipService.relinquishMastership(deviceId).get(); 431 mastershipService.relinquishMastership(deviceId).get();
416 } catch (InterruptedException e) { 432 } catch (InterruptedException e) {
417 - log.warn("Interrupted while reliquishing role for {}", deviceId); 433 + log.warn("Interrupted while reliquishing role for {}",
434 + deviceId);
418 Thread.currentThread().interrupt(); 435 Thread.currentThread().interrupt();
419 } catch (ExecutionException e) { 436 } catch (ExecutionException e) {
420 - log.error("Exception thrown while relinquishing role for {}", deviceId, e); 437 + log.error("Exception thrown while relinquishing role for {}",
438 + deviceId, e);
421 } 439 }
422 } 440 }
423 } 441 }
...@@ -432,12 +450,14 @@ public class DeviceManager ...@@ -432,12 +450,14 @@ public class DeviceManager
432 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { 450 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
433 // Never been a master for this device 451 // Never been a master for this device
434 // any update will be ignored. 452 // any update will be ignored.
435 - log.trace("Ignoring {} port updates on standby node. {}", deviceId, portDescriptions); 453 + log.trace("Ignoring {} port updates on standby node. {}",
454 + deviceId, portDescriptions);
436 return; 455 return;
437 } 456 }
438 457
439 List<DeviceEvent> events = store.updatePorts(this.provider().id(), 458 List<DeviceEvent> events = store.updatePorts(this.provider().id(),
440 - deviceId, portDescriptions); 459 + deviceId,
460 + portDescriptions);
441 for (DeviceEvent event : events) { 461 for (DeviceEvent event : events) {
442 post(event); 462 post(event);
443 } 463 }
...@@ -453,12 +473,13 @@ public class DeviceManager ...@@ -453,12 +473,13 @@ public class DeviceManager
453 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) { 473 if (!deviceClockProviderService.isTimestampAvailable(deviceId)) {
454 // Never been a master for this device 474 // Never been a master for this device
455 // any update will be ignored. 475 // any update will be ignored.
456 - log.trace("Ignoring {} port update on standby node. {}", deviceId, portDescription); 476 + log.trace("Ignoring {} port update on standby node. {}",
477 + deviceId, portDescription);
457 return; 478 return;
458 } 479 }
459 480
460 - final DeviceEvent event = store.updatePortStatus(this.provider().id(), 481 + final DeviceEvent event = store.updatePortStatus(this.provider()
461 - deviceId, portDescription); 482 + .id(), deviceId, portDescription);
462 if (event != null) { 483 if (event != null) {
463 log.info("Device {} port {} status changed", deviceId, event 484 log.info("Device {} port {} status changed", deviceId, event
464 .port().number()); 485 .port().number());
...@@ -467,7 +488,8 @@ public class DeviceManager ...@@ -467,7 +488,8 @@ public class DeviceManager
467 } 488 }
468 489
469 @Override 490 @Override
470 - public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, 491 + public void receivedRoleReply(DeviceId deviceId,
492 + MastershipRole requested,
471 MastershipRole response) { 493 MastershipRole response) {
472 // Several things can happen here: 494 // Several things can happen here:
473 // 1. request and response match 495 // 1. request and response match
...@@ -480,43 +502,50 @@ public class DeviceManager ...@@ -480,43 +502,50 @@ public class DeviceManager
480 // FIXME: implement response to this notification 502 // FIXME: implement response to this notification
481 503
482 log.debug("got reply to a role request for {}: asked for {}, and got {}", 504 log.debug("got reply to a role request for {}: asked for {}, and got {}",
483 - deviceId, requested, response); 505 + deviceId, requested, response);
484 506
485 if (requested == null && response == null) { 507 if (requested == null && response == null) {
486 - // something was off with DeviceProvider, maybe check channel too? 508 + // something was off with DeviceProvider, maybe check channel
487 - log.warn("Failed to assert role [{}] onto Device {}", requested, deviceId); 509 + // too?
510 + log.warn("Failed to assert role [{}] onto Device {}",
511 + requested, deviceId);
488 mastershipService.relinquishMastership(deviceId); 512 mastershipService.relinquishMastership(deviceId);
489 return; 513 return;
490 } 514 }
491 515
492 if (Objects.equals(requested, response)) { 516 if (Objects.equals(requested, response)) {
493 - if (Objects.equals(requested, mastershipService.getLocalRole(deviceId))) { 517 + if (Objects.equals(requested,
518 + mastershipService.getLocalRole(deviceId))) {
494 return; 519 return;
495 } else { 520 } else {
496 return; 521 return;
497 - // FIXME roleManager got the device to comply, but doesn't agree with 522 + // FIXME roleManager got the device to comply, but doesn't
523 + // agree with
498 // the store; use the store's view, then try to reassert. 524 // the store; use the store's view, then try to reassert.
499 } 525 }
500 } else { 526 } else {
501 // we didn't get back what we asked for. Reelect someone else. 527 // we didn't get back what we asked for. Reelect someone else.
502 - log.warn("Failed to assert role [{}] onto Device {}", response, deviceId); 528 + log.warn("Failed to assert role [{}] onto Device {}", response,
529 + deviceId);
503 if (response == MastershipRole.MASTER) { 530 if (response == MastershipRole.MASTER) {
504 mastershipService.relinquishMastership(deviceId); 531 mastershipService.relinquishMastership(deviceId);
505 // TODO: Shouldn't we be triggering event? 532 // TODO: Shouldn't we be triggering event?
506 - //final Device device = getDevice(deviceId); 533 + // final Device device = getDevice(deviceId);
507 - //post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device)); 534 + // post(new DeviceEvent(DEVICE_MASTERSHIP_CHANGED, device));
508 } 535 }
509 } 536 }
510 } 537 }
511 538
512 @Override 539 @Override
513 - public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { 540 + public void updatePortStatistics(DeviceId deviceId,
541 + Collection<PortStatistics> portStatistics) {
514 checkNotNull(deviceId, DEVICE_ID_NULL); 542 checkNotNull(deviceId, DEVICE_ID_NULL);
515 checkNotNull(portStatistics, "Port statistics list cannot be null"); 543 checkNotNull(portStatistics, "Port statistics list cannot be null");
516 checkValidity(); 544 checkValidity();
517 545
518 - DeviceEvent event = store.updatePortStatistics(this.provider().id(), 546 + DeviceEvent event = store
519 - deviceId, portStatistics); 547 + .updatePortStatistics(this.provider().id(), deviceId,
548 + portStatistics);
520 post(event); 549 post(event);
521 } 550 }
522 } 551 }
...@@ -532,19 +561,20 @@ public class DeviceManager ...@@ -532,19 +561,20 @@ public class DeviceManager
532 /** 561 /**
533 * Apply role to device and send probe if MASTER. 562 * Apply role to device and send probe if MASTER.
534 * 563 *
535 - * @param deviceId device identifier 564 + * @param deviceId device identifier
536 - * @param newRole new role to apply to the device 565 + * @param newRole new role to apply to the device
537 * @return true if the request was sent to provider 566 * @return true if the request was sent to provider
538 */ 567 */
539 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) { 568 private boolean applyRoleAndProbe(DeviceId deviceId, MastershipRole newRole) {
540 if (newRole.equals(MastershipRole.NONE)) { 569 if (newRole.equals(MastershipRole.NONE)) {
541 - //no-op 570 + // no-op
542 return true; 571 return true;
543 } 572 }
544 573
545 DeviceProvider provider = getProvider(deviceId); 574 DeviceProvider provider = getProvider(deviceId);
546 if (provider == null) { 575 if (provider == null) {
547 - log.warn("Provider for {} was not found. Cannot apply role {}", deviceId, newRole); 576 + log.warn("Provider for {} was not found. Cannot apply role {}",
577 + deviceId, newRole);
548 return false; 578 return false;
549 } 579 }
550 provider.roleChanged(deviceId, newRole); 580 provider.roleChanged(deviceId, newRole);
...@@ -559,12 +589,11 @@ public class DeviceManager ...@@ -559,12 +589,11 @@ public class DeviceManager
559 /** 589 /**
560 * Reaasert role for specified device connected to this node. 590 * Reaasert role for specified device connected to this node.
561 * 591 *
562 - * @param did device identifier 592 + * @param did device identifier
563 - * @param nextRole role to apply. If NONE is specified, 593 + * @param nextRole role to apply. If NONE is specified, it will ask
564 - * it will ask mastership service for a role and apply it. 594 + * mastership service for a role and apply it.
565 */ 595 */
566 - private void reassertRole(final DeviceId did, 596 + private void reassertRole(final DeviceId did, final MastershipRole nextRole) {
567 - final MastershipRole nextRole) {
568 597
569 MastershipRole myNextRole = nextRole; 598 MastershipRole myNextRole = nextRole;
570 if (myNextRole == NONE) { 599 if (myNextRole == NONE) {
...@@ -581,18 +610,17 @@ public class DeviceManager ...@@ -581,18 +610,17 @@ public class DeviceManager
581 case MASTER: 610 case MASTER:
582 final Device device = getDevice(did); 611 final Device device = getDevice(did);
583 if ((device != null) && !isAvailable(did)) { 612 if ((device != null) && !isAvailable(did)) {
584 - //flag the device as online. Is there a better way to do this? 613 + // flag the device as online. Is there a better way to do this?
585 - DefaultDeviceDescription deviceDescription 614 + DefaultDeviceDescription deviceDescription = new DefaultDeviceDescription(
586 - = new DefaultDeviceDescription(did.uri(), 615 + did.uri(),
587 - device.type(), 616 + device.type(),
588 - device.manufacturer(), 617 + device.manufacturer(),
589 - device.hwVersion(), 618 + device.hwVersion(),
590 - device.swVersion(), 619 + device.swVersion(),
591 - device.serialNumber(), 620 + device.serialNumber(),
592 - device.chassisId()); 621 + device.chassisId());
593 - DeviceEvent devEvent = 622 + DeviceEvent devEvent = store.createOrUpdateDevice(device
594 - store.createOrUpdateDevice(device.providerId(), did, 623 + .providerId(), did, deviceDescription);
595 - deviceDescription);
596 post(devEvent); 624 post(devEvent);
597 } 625 }
598 // TODO: should apply role only if there is mismatch 626 // TODO: should apply role only if there is mismatch
...@@ -634,7 +662,8 @@ public class DeviceManager ...@@ -634,7 +662,8 @@ public class DeviceManager
634 if (localNodeId.equals(event.roleInfo().master())) { 662 if (localNodeId.equals(event.roleInfo().master())) {
635 // confirm latest info 663 // confirm latest info
636 MastershipTerm term = termService.getMastershipTerm(did); 664 MastershipTerm term = termService.getMastershipTerm(did);
637 - final boolean iHaveControl = term != null && localNodeId.equals(term.master()); 665 + final boolean iHaveControl = term != null
666 + && localNodeId.equals(term.master());
638 if (iHaveControl) { 667 if (iHaveControl) {
639 deviceClockProviderService.setMastershipTerm(did, term); 668 deviceClockProviderService.setMastershipTerm(did, term);
640 myNextRole = MASTER; 669 myNextRole = MASTER;
...@@ -647,15 +676,13 @@ public class DeviceManager ...@@ -647,15 +676,13 @@ public class DeviceManager
647 myNextRole = NONE; 676 myNextRole = NONE;
648 } 677 }
649 678
650 -
651 final boolean isReachable = isReachable(did); 679 final boolean isReachable = isReachable(did);
652 if (!isReachable) { 680 if (!isReachable) {
653 // device is not connected to this node 681 // device is not connected to this node
654 if (myNextRole != NONE) { 682 if (myNextRole != NONE) {
655 log.warn("Node was instructed to be {} role for {}, " 683 log.warn("Node was instructed to be {} role for {}, "
656 + "but this node cannot reach the device. " 684 + "but this node cannot reach the device. "
657 - + "Relinquishing role. ", 685 + + "Relinquishing role. ", myNextRole, did);
658 - myNextRole, did);
659 mastershipService.relinquishMastership(did); 686 mastershipService.relinquishMastership(did);
660 } 687 }
661 return; 688 return;
...@@ -691,4 +718,34 @@ public class DeviceManager ...@@ -691,4 +718,34 @@ public class DeviceManager
691 post(event); 718 post(event);
692 } 719 }
693 } 720 }
721 +
722 + @Override
723 + public Iterable<Device> getDevices(Type type) {
724 + checkPermission(Permission.DEVICE_READ);
725 + Set<Device> results = new HashSet<>();
726 + Iterable<Device> devices = store.getDevices();
727 + if (devices != null) {
728 + devices.forEach(d -> {
729 + if (type.equals(d.type())) {
730 + results.add(d);
731 + }
732 + });
733 + }
734 + return results;
735 + }
736 +
737 + @Override
738 + public Iterable<Device> getAvailableDevices(Type type) {
739 + checkPermission(Permission.DEVICE_READ);
740 + Set<Device> results = new HashSet<>();
741 + Iterable<Device> availableDevices = store.getAvailableDevices();
742 + if (availableDevices != null) {
743 + availableDevices.forEach(d -> {
744 + if (type.equals(d.type())) {
745 + results.add(d);
746 + }
747 + });
748 + }
749 + return results;
750 + }
694 } 751 }
......