@Test public final void testCreateOrUpdateDevice() throws IOException { DeviceDescription description = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW1, SN, CID); Capture<InternalDeviceEvent> message = new Capture<>(); Capture<MessageSubject> subject = new Capture<>(); Capture<Function<InternalDeviceEvent, byte[]>> encoder = new Capture<>(); resetCommunicatorExpectingSingleBroadcast(message, subject, encoder); DeviceEvent event = deviceStore.createOrUpdateDevice(PID, DID1, description); assertEquals(DEVICE_ADDED, event.type()); assertDevice(DID1, SW1, event.subject()); verify(clusterCommunicator); assertInternalDeviceEvent(NID1, DID1, PID, description, message, subject, encoder); DeviceDescription description2 = new DefaultDeviceDescription(DID1.uri(), SWITCH, MFR, HW, SW2, SN, CID); resetCommunicatorExpectingSingleBroadcast(message, subject, encoder); DeviceEvent event2 = deviceStore.createOrUpdateDevice(PID, DID1, description2); assertEquals(DEVICE_UPDATED, event2.type()); assertDevice(DID1, SW2, event2.subject()); verify(clusterCommunicator); assertInternalDeviceEvent(NID1, DID1, PID, description2, message, subject, encoder); reset(clusterCommunicator); assertNull("No change expected", deviceStore.createOrUpdateDevice(PID, DID1, description2)); }
@Override public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, DeviceDescription deviceDescription) { NodeId localNode = clusterService.getLocalNode().id(); NodeId deviceNode = mastershipService.getMasterFor(deviceId); DeviceEvent deviceEvent = null; if (localNode.equals(deviceNode)) { final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); final Timestamped<DeviceDescription> mergedDesc; final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); synchronized (device) { deviceEvent = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); mergedDesc = device.get(providerId).getDeviceDesc(); } if (deviceEvent != null) { log.debug("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", providerId, deviceId); notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); } } else { if (!providerId.scheme().equals("cfg")) { return null; } if (deviceNode == null) { return null; } DeviceInjectedEvent deviceInjectedEvent = new DeviceInjectedEvent( providerId, deviceId, deviceDescription); clusterCommunicator.unicast(deviceInjectedEvent, DEVICE_INJECTED, SERIALIZER::encode, deviceNode); } return deviceEvent; }
GossipDeviceStore extends AbstractStore<DeviceEvent, DeviceStoreDelegate> implements DeviceStore { @Override public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, DeviceDescription deviceDescription) { NodeId localNode = clusterService.getLocalNode().id(); NodeId deviceNode = mastershipService.getMasterFor(deviceId); DeviceEvent deviceEvent = null; if (localNode.equals(deviceNode)) { final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); final Timestamped<DeviceDescription> mergedDesc; final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); synchronized (device) { deviceEvent = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); mergedDesc = device.get(providerId).getDeviceDesc(); } if (deviceEvent != null) { log.debug("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", providerId, deviceId); notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); } } else { if (!providerId.scheme().equals("cfg")) { return null; } if (deviceNode == null) { return null; } DeviceInjectedEvent deviceInjectedEvent = new DeviceInjectedEvent( providerId, deviceId, deviceDescription); clusterCommunicator.unicast(deviceInjectedEvent, DEVICE_INJECTED, SERIALIZER::encode, deviceNode); } return deviceEvent; } }
GossipDeviceStore extends AbstractStore<DeviceEvent, DeviceStoreDelegate> implements DeviceStore { @Override public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, DeviceDescription deviceDescription) { NodeId localNode = clusterService.getLocalNode().id(); NodeId deviceNode = mastershipService.getMasterFor(deviceId); DeviceEvent deviceEvent = null; if (localNode.equals(deviceNode)) { final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); final Timestamped<DeviceDescription> mergedDesc; final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); synchronized (device) { deviceEvent = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); mergedDesc = device.get(providerId).getDeviceDesc(); } if (deviceEvent != null) { log.debug("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", providerId, deviceId); notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); } } else { if (!providerId.scheme().equals("cfg")) { return null; } if (deviceNode == null) { return null; } DeviceInjectedEvent deviceInjectedEvent = new DeviceInjectedEvent( providerId, deviceId, deviceDescription); clusterCommunicator.unicast(deviceInjectedEvent, DEVICE_INJECTED, SERIALIZER::encode, deviceNode); } return deviceEvent; }  }
GossipDeviceStore extends AbstractStore<DeviceEvent, DeviceStoreDelegate> implements DeviceStore { @Override public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, DeviceDescription deviceDescription) { NodeId localNode = clusterService.getLocalNode().id(); NodeId deviceNode = mastershipService.getMasterFor(deviceId); DeviceEvent deviceEvent = null; if (localNode.equals(deviceNode)) { final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); final Timestamped<DeviceDescription> mergedDesc; final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); synchronized (device) { deviceEvent = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); mergedDesc = device.get(providerId).getDeviceDesc(); } if (deviceEvent != null) { log.debug("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", providerId, deviceId); notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); } } else { if (!providerId.scheme().equals("cfg")) { return null; } if (deviceNode == null) { return null; } DeviceInjectedEvent deviceInjectedEvent = new DeviceInjectedEvent( providerId, deviceId, deviceDescription); clusterCommunicator.unicast(deviceInjectedEvent, DEVICE_INJECTED, SERIALIZER::encode, deviceNode); } return deviceEvent; }  @Activate void activate(); @Deactivate void deactivate(); @Override int getDeviceCount(); @Override Iterable<Device> getDevices(); @Override Iterable<Device> getAvailableDevices(); @Override Device getDevice(DeviceId deviceId); @Override synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId,
                                                         DeviceId deviceId,
                                                         DeviceDescription deviceDescription); @Override DeviceEvent markOffline(DeviceId deviceId); @Override synchronized List<DeviceEvent> updatePorts(ProviderId providerId,
                                                      DeviceId deviceId,
                                                      List<PortDescription> portDescriptions); @Override synchronized DeviceEvent updatePortStatus(ProviderId providerId,
                                                     DeviceId deviceId,
                                                     PortDescription portDescription); @Override List<Port> getPorts(DeviceId deviceId); @Override DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
                                            Collection<PortStatistics> newStatsCollection); PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats); @Override List<PortStatistics> getPortStatistics(DeviceId deviceId); @Override List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId); @Override Port getPort(DeviceId deviceId, PortNumber portNumber); @Override boolean isAvailable(DeviceId deviceId); @Override synchronized DeviceEvent removeDevice(DeviceId deviceId); }
GossipDeviceStore extends AbstractStore<DeviceEvent, DeviceStoreDelegate> implements DeviceStore { @Override public synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, DeviceDescription deviceDescription) { NodeId localNode = clusterService.getLocalNode().id(); NodeId deviceNode = mastershipService.getMasterFor(deviceId); DeviceEvent deviceEvent = null; if (localNode.equals(deviceNode)) { final Timestamp newTimestamp = deviceClockService.getTimestamp(deviceId); final Timestamped<DeviceDescription> deltaDesc = new Timestamped<>(deviceDescription, newTimestamp); final Timestamped<DeviceDescription> mergedDesc; final Map<ProviderId, DeviceDescriptions> device = getOrCreateDeviceDescriptionsMap(deviceId); synchronized (device) { deviceEvent = createOrUpdateDeviceInternal(providerId, deviceId, deltaDesc); mergedDesc = device.get(providerId).getDeviceDesc(); } if (deviceEvent != null) { log.debug("Notifying peers of a device update topology event for providerId: {} and deviceId: {}", providerId, deviceId); notifyPeers(new InternalDeviceEvent(providerId, deviceId, mergedDesc)); } } else { if (!providerId.scheme().equals("cfg")) { return null; } if (deviceNode == null) { return null; } DeviceInjectedEvent deviceInjectedEvent = new DeviceInjectedEvent( providerId, deviceId, deviceDescription); clusterCommunicator.unicast(deviceInjectedEvent, DEVICE_INJECTED, SERIALIZER::encode, deviceNode); } return deviceEvent; }  @Activate void activate(); @Deactivate void deactivate(); @Override int getDeviceCount(); @Override Iterable<Device> getDevices(); @Override Iterable<Device> getAvailableDevices(); @Override Device getDevice(DeviceId deviceId); @Override synchronized DeviceEvent createOrUpdateDevice(ProviderId providerId,
                                                         DeviceId deviceId,
                                                         DeviceDescription deviceDescription); @Override DeviceEvent markOffline(DeviceId deviceId); @Override synchronized List<DeviceEvent> updatePorts(ProviderId providerId,
                                                      DeviceId deviceId,
                                                      List<PortDescription> portDescriptions); @Override synchronized DeviceEvent updatePortStatus(ProviderId providerId,
                                                     DeviceId deviceId,
                                                     PortDescription portDescription); @Override List<Port> getPorts(DeviceId deviceId); @Override DeviceEvent updatePortStatistics(ProviderId providerId, DeviceId deviceId,
                                            Collection<PortStatistics> newStatsCollection); PortStatistics calcDeltaStats(DeviceId deviceId, PortStatistics prvStats, PortStatistics newStats); @Override List<PortStatistics> getPortStatistics(DeviceId deviceId); @Override List<PortStatistics> getPortDeltaStatistics(DeviceId deviceId); @Override Port getPort(DeviceId deviceId, PortNumber portNumber); @Override boolean isAvailable(DeviceId deviceId); @Override synchronized DeviceEvent removeDevice(DeviceId deviceId);  }