Search in sources :

Example 21 with DeviceEvent

use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.

the class GossipDeviceStore method removeDevice.

@Override
public synchronized DeviceEvent removeDevice(DeviceId deviceId) {
    final NodeId myId = clusterService.getLocalNode().id();
    NodeId master = mastershipService.getMasterFor(deviceId);
    // if there exist a master, forward
    // if there is no master, try to become one and process
    boolean relinquishAtEnd = false;
    if (master == null) {
        final MastershipRole myRole = mastershipService.getLocalRole(deviceId);
        if (myRole != MastershipRole.NONE) {
            relinquishAtEnd = true;
        }
        log.debug("Temporarily requesting role for {} to remove", deviceId);
        if (mastershipService.requestRoleFor(deviceId).join() == MastershipRole.MASTER) {
            master = myId;
        }
    }
    boolean isMaster = myId.equals(master);
    // If this node is not the master, forward the request.
    if (!isMaster) {
        log.debug("{} has control of {}, forwarding remove request", master, deviceId);
        // TODO check unicast return value
        clusterCommunicator.unicast(deviceId, DEVICE_REMOVE_REQ, SERIALIZER::encode, master);
    /* error log:
             log.error("Failed to forward {} remove request to {}", deviceId, master, e);
             */
    }
    // If this node is the master, get a timestamp. Otherwise, default to the current device timestamp.
    Timestamp timestamp = isMaster ? deviceClockService.getTimestamp(deviceId) : null;
    DeviceEvent event = removeDeviceInternal(deviceId, timestamp);
    // If this node is the master, update peers.
    if (isMaster && event != null) {
        log.debug("Notifying peers of a device removed topology event for deviceId: {}", deviceId);
        notifyPeers(new InternalDeviceRemovedEvent(deviceId, timestamp));
    }
    notifyDelegateIfNotNull(event);
    // Relinquish mastership if acquired to remove the device.
    if (relinquishAtEnd) {
        log.debug("Relinquishing temporary role acquired for {}", deviceId);
        mastershipService.relinquishMastership(deviceId);
    }
    return event;
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent) ControllerNodeToNodeId.toNodeId(org.onosproject.cluster.ControllerNodeToNodeId.toNodeId) NodeId(org.onosproject.cluster.NodeId) MastershipRole(org.onosproject.net.MastershipRole) WallClockTimestamp(org.onosproject.store.service.WallClockTimestamp) Timestamp(org.onosproject.store.Timestamp) MastershipBasedTimestamp(org.onosproject.store.impl.MastershipBasedTimestamp) MultiValuedTimestamp(org.onosproject.store.service.MultiValuedTimestamp)

Example 22 with DeviceEvent

use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.

the class GossipDeviceStore method removeDeviceInternal.

private DeviceEvent removeDeviceInternal(DeviceId deviceId, Timestamp timestamp) {
    Map<ProviderId, DeviceDescriptions> descs = getOrCreateDeviceDescriptionsMap(deviceId);
    synchronized (descs) {
        // accept removal request if given timestamp is newer than
        // the latest Timestamp from Primary provider
        DeviceDescriptions primDescs = getPrimaryDescriptions(descs);
        if (primDescs == null) {
            return null;
        }
        Timestamp lastTimestamp = primDescs.getLatestTimestamp();
        // If no timestamp is set, default the timestamp to the last timestamp for the device.
        if (timestamp == null) {
            timestamp = lastTimestamp;
        }
        if (timestamp.compareTo(lastTimestamp) <= 0) {
            // outdated event ignore
            return null;
        }
        removalRequest.put(deviceId, timestamp);
        Device device = devices.remove(deviceId);
        // removing internal description
        deviceDescs.remove(deviceId);
        // should DEVICE_REMOVED carry removed ports?
        Map<PortNumber, Port> ports = devicePorts.get(deviceId);
        if (ports != null) {
            ports.clear();
        }
        markOfflineInternal(deviceId, timestamp);
        descs.clear();
        // Forget about the device
        offline.remove(deviceId);
        return device == null ? null : new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, device, null);
    }
}
Also used : ProviderId(org.onosproject.net.provider.ProviderId) DeviceEvent(org.onosproject.net.device.DeviceEvent) DefaultDevice(org.onosproject.net.DefaultDevice) Device(org.onosproject.net.Device) Port(org.onosproject.net.Port) DefaultPort(org.onosproject.net.DefaultPort) PortNumber(org.onosproject.net.PortNumber) WallClockTimestamp(org.onosproject.store.service.WallClockTimestamp) Timestamp(org.onosproject.store.Timestamp) MastershipBasedTimestamp(org.onosproject.store.impl.MastershipBasedTimestamp) MultiValuedTimestamp(org.onosproject.store.service.MultiValuedTimestamp)

Example 23 with DeviceEvent

use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.

the class GossipDeviceStore method markOnline.

/**
 * Marks the device as available if the given timestamp is not outdated,
 * compared to the time the device has been marked offline.
 *
 * @param deviceId  identifier of the device
 * @param timestamp of the event triggering this change.
 * @param notifyPeers if the event needs to be notified to peers.
 * @return ready to send event describing what occurred; null if no change
 */
private DeviceEvent markOnline(DeviceId deviceId, Timestamp timestamp, boolean notifyPeers) {
    final DeviceEvent event = markOnlineInternal(deviceId, timestamp);
    if (event != null && notifyPeers) {
        log.debug("Notifying peers of a device online topology event for deviceId: {} {}", deviceId, timestamp);
        notifyPeers(new InternalDeviceStatusChangeEvent(deviceId, timestamp, true));
    }
    return event;
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent)

Example 24 with DeviceEvent

use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.

the class VirtualNetworkDeviceManagerTest method testDeviceEventsForAddRemovalDeviceAndPorts.

/**
 * Tests DeviceEvents received during virtual device/port addition and removal.
 */
@Test
public void testDeviceEventsForAddRemovalDeviceAndPorts() throws TestUtils.TestUtilsException {
    manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
    VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
    // add virtual device before virtual device manager is created
    VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), VDID1);
    // no DeviceEvent expected
    validateEvents();
    testDirectory.add(EventDeliveryService.class, dispatcher);
    DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class);
    // virtual device manager is created; register DeviceEvent listener
    deviceService.addListener(testListener);
    // list to keep track of expected event types
    List<DeviceEvent.Type> expectedEventTypes = new ArrayList<>();
    // add virtual device
    VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), VDID2);
    expectedEventTypes.add(DeviceEvent.Type.DEVICE_ADDED);
    ConnectPoint cp = new ConnectPoint(PHYDID1, PortNumber.portNumber(1));
    // add 2 virtual ports
    manager.createVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), cp);
    expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED);
    manager.createVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(2), cp);
    expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED);
    // verify virtual ports were added
    Set<VirtualPort> virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id());
    assertNotNull("The virtual port set should not be null", virtualPorts);
    assertEquals("The virtual port set size did not match.", 2, virtualPorts.size());
    virtualPorts.forEach(vp -> assertFalse("Initial virtual port state should be disabled", vp.isEnabled()));
    // verify change state of virtual port (disabled -> enabled)
    manager.updatePortState(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), true);
    Port changedPort = deviceService.getPort(device2.id(), PortNumber.portNumber(1));
    assertNotNull("The changed virtual port should not be null", changedPort);
    assertEquals("Virtual port state should be enabled", true, changedPort.isEnabled());
    expectedEventTypes.add(DeviceEvent.Type.PORT_UPDATED);
    // verify change state of virtual port (disabled -> disabled)
    manager.updatePortState(virtualNetwork.id(), device2.id(), PortNumber.portNumber(2), false);
    changedPort = deviceService.getPort(device2.id(), PortNumber.portNumber(2));
    assertNotNull("The changed virtual port should not be null", changedPort);
    assertEquals("Virtual port state should be disabled", false, changedPort.isEnabled());
    // remove 2 virtual ports
    for (VirtualPort virtualPort : virtualPorts) {
        manager.removeVirtualPort(virtualNetwork.id(), (DeviceId) virtualPort.element().id(), virtualPort.number());
        expectedEventTypes.add(DeviceEvent.Type.PORT_REMOVED);
        // attempt to remove the same virtual port again - no DeviceEvent.Type.PORT_REMOVED expected.
        manager.removeVirtualPort(virtualNetwork.id(), (DeviceId) virtualPort.element().id(), virtualPort.number());
    }
    // verify virtual ports were removed
    virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id());
    assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty());
    // Add/remove one virtual port again.
    VirtualPort virtualPort = manager.createVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), cp);
    expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED);
    ConnectPoint newCp = new ConnectPoint(PHYDID3, PortNumber.portNumber(2));
    manager.bindVirtualPort(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), newCp);
    expectedEventTypes.add(DeviceEvent.Type.PORT_UPDATED);
    manager.removeVirtualPort(virtualNetwork.id(), (DeviceId) virtualPort.element().id(), virtualPort.number());
    expectedEventTypes.add(DeviceEvent.Type.PORT_REMOVED);
    // verify no virtual ports remain
    virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id());
    assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty());
    // remove virtual device
    manager.removeVirtualDevice(virtualNetwork.id(), device2.id());
    expectedEventTypes.add(DeviceEvent.Type.DEVICE_REMOVED);
    // Validate that the events were all received in the correct order.
    validateEvents((Enum[]) expectedEventTypes.toArray(new DeviceEvent.Type[expectedEventTypes.size()]));
    // cleanup
    deviceService.removeListener(testListener);
}
Also used : VirtualNetwork(org.onosproject.incubator.net.virtual.VirtualNetwork) VirtualPort(org.onosproject.incubator.net.virtual.VirtualPort) DeviceEvent(org.onosproject.net.device.DeviceEvent) Port(org.onosproject.net.Port) VirtualPort(org.onosproject.incubator.net.virtual.VirtualPort) VirtualDevice(org.onosproject.incubator.net.virtual.VirtualDevice) DeviceService(org.onosproject.net.device.DeviceService) ArrayList(java.util.ArrayList) ConnectPoint(org.onosproject.net.ConnectPoint) Test(org.junit.Test)

Example 25 with DeviceEvent

use of org.onosproject.net.device.DeviceEvent in project onos by opennetworkinglab.

the class DeviceManager method removeDevice.

@Override
public void removeDevice(DeviceId deviceId) {
    checkNotNull(deviceId, DEVICE_ID_NULL);
    DeviceEvent event = store.removeDevice(deviceId);
    if (event != null) {
        log.info("Device {} administratively removed", deviceId);
    }
}
Also used : DeviceEvent(org.onosproject.net.device.DeviceEvent)

Aggregations

DeviceEvent (org.onosproject.net.device.DeviceEvent)77 Test (org.junit.Test)42 Device (org.onosproject.net.Device)26 DefaultDevice (org.onosproject.net.DefaultDevice)17 DefaultPort (org.onosproject.net.DefaultPort)17 PortDescription (org.onosproject.net.device.PortDescription)14 PortNumber (org.onosproject.net.PortNumber)11 Port (org.onosproject.net.Port)10 DefaultPortDescription (org.onosproject.net.device.DefaultPortDescription)10 ProviderId (org.onosproject.net.provider.ProviderId)10 ArrayList (java.util.ArrayList)8 ConnectPoint (org.onosproject.net.ConnectPoint)8 Timestamp (org.onosproject.store.Timestamp)8 MessageSubject (org.onosproject.store.cluster.messaging.MessageSubject)8 MastershipBasedTimestamp (org.onosproject.store.impl.MastershipBasedTimestamp)8 MultiValuedTimestamp (org.onosproject.store.service.MultiValuedTimestamp)8 WallClockTimestamp (org.onosproject.store.service.WallClockTimestamp)8 BiFunction (java.util.function.BiFunction)7 Function (java.util.function.Function)7 DeviceDescription (org.onosproject.net.device.DeviceDescription)7