@Override public void event(DeviceEvent event) { Device device = event.subject(); switch (event.type()) { case DEVICE_ADDED: break; case DEVICE_AVAILABILITY_CHANGED: if (hostRemovalEnabled && !deviceService.isAvailable(device.id())) { removeHosts(hostService.getConnectedHosts(device.id())); } break; case DEVICE_SUSPENDED: case DEVICE_UPDATED: // Nothing to do? break; case DEVICE_REMOVED: if (hostRemovalEnabled) { removeHosts(hostService.getConnectedHosts(device.id())); } break; case PORT_ADDED: break; case PORT_UPDATED: if (hostRemovalEnabled) { ConnectPoint point = new ConnectPoint(device.id(), event.port().number()); removeHosts(hostService.getConnectedHosts(point)); } break; case PORT_REMOVED: // Nothing to do? break; default: break; } }
// Adds any missing device ports. private void addMissingPorts(Device device) { try { List<Port> ports = deviceService.getPorts(device.id()); Set<ConnectPoint> existing = ports .stream() .map(p -> new ConnectPoint(device.id(), p.number())) .collect(Collectors.toSet()); Set<ConnectPoint> missing = connectPoints .stream() .filter(cp -> cp.deviceId().equals(device.id())) .filter(cp -> !existing.contains(cp)) .collect(Collectors.toSet()); if (!missing.isEmpty()) { List<PortDescription> newPorts = Stream.concat( ports.stream().map(this::description), missing.stream().map(this::description)) .collect(Collectors.toList()); deviceProviderService.updatePorts(device.id(), newPorts); } } catch (IllegalArgumentException e) { log.warn("Error pushing ports: {}", e.getMessage()); } }
private void prepareInstallation() { Set<ControllerNode> instances = Sets.newHashSet(clusterService.getNodes()); instances.remove(clusterService.getLocalNode()); Set<NodeId> acceptableNodes = Sets.newHashSet(); if (neighbours >= instances.size()) { instances.forEach(instance -> acceptableNodes.add(instance.id())); } else { Iterator<ControllerNode> nodes = instances.iterator(); for (int i = neighbours; i > 0; i--) { acceptableNodes.add(nodes.next().id()); } } acceptableNodes.add(clusterService.getLocalNode().id()); Set<Device> devices = Sets.newHashSet(); for (Device dev : deviceService.getDevices()) { if (acceptableNodes.contains(mastershipService.getMasterFor(dev.id()))) { devices.add(dev); } } TrafficTreatment treatment = DefaultTrafficTreatment.builder() .setOutput(PortNumber.portNumber(RandomUtils.nextInt())) .build(); TrafficSelector.Builder sbuilder; FlowRuleOperations.Builder rules = FlowRuleOperations.builder(); FlowRuleOperations.Builder remove = FlowRuleOperations.builder(); for (Device d : devices) { for (int i = 0; i < this.flowPerDevice; i++) { sbuilder = DefaultTrafficSelector.builder(); sbuilder .matchEthSrc(MacAddress.valueOf(RandomUtils.nextInt() * i)) .matchEthDst(MacAddress.valueOf((Integer.MAX_VALUE - i) * RandomUtils.nextInt())); int randomPriority = RandomUtils.nextInt(); FlowRule f = DefaultFlowRule.builder() .forDevice(d.id()) .withSelector(sbuilder.build()) .withTreatment(treatment) .withPriority(randomPriority) .fromApp(appId) .makeTemporary(10) .build(); rules.add(f); remove.remove(f); } } this.adds = rules; this.removes = remove; }
// Adds any missing device ports. private void addMissingPorts(Device device) { List<Port> ports = deviceService.getPorts(device.id()); Set<ConnectPoint> existing = ports .stream() .map(p -> new ConnectPoint(device.id(), p.number())) .collect(Collectors.toSet()); Set<ConnectPoint> missing = connectPoints.stream().filter(cp -> !existing.contains(cp)).collect(Collectors.toSet()); if (!missing.isEmpty()) { List<PortDescription> newPorts = Lists.newArrayList(); ports.forEach(p -> newPorts.add(description(p))); missing.forEach(cp -> newPorts.add(description(cp))); deviceProviderService.updatePorts(device.id(), newPorts); } }
public static DeviceDescription descriptionOf(Device device) { checkNotNull(device, "Must supply non-null Device"); return new DefaultDeviceDescription( device.id().uri(), device.type(), device.manufacturer(), device.hwVersion(), device.swVersion(), device.serialNumber(), device.chassisId(), (SparseAnnotations) device.annotations()); }
private static void assertDevice(DeviceId id, String swVersion, Device device) { assertNotNull(device); assertEquals(id, device.id()); assertEquals(MFR, device.manufacturer()); assertEquals(HW, device.hwVersion()); assertEquals(swVersion, device.swVersion()); assertEquals(SN, device.serialNumber()); }
@Test public final void testGetDevices() { assertEquals("initialy empty", 0, Iterables.size(deviceStore.getDevices())); putDevice(DID1, SW1); putDevice(DID2, SW2); putDevice(DID1, SW1); assertEquals("expect 2 uniq devices", 2, Iterables.size(deviceStore.getDevices())); Map<DeviceId, Device> devices = new HashMap<>(); for (Device device : deviceStore.getDevices()) { devices.put(device.id(), device); } assertDevice(DID1, SW1, devices.get(DID1)); assertDevice(DID2, SW2, devices.get(DID2)); // add case for new node? }
@Override public void run() { if (doNotPushFlows) { return; } switch (deviceEvent.type()) { case DEVICE_ADDED: processDeviceAdded((Device) deviceEvent.subject()); break; case DEVICE_UPDATED: Port port = (Port) deviceEvent.subject(); if (port.isEnabled()) { processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); } break; case DEVICE_AVAILABILITY_CHANGED: Device device = (Device) deviceEvent.subject(); if (deviceService.isAvailable(device.id())) { processDeviceAdded(device); } break; case PORT_ADDED: processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); break; case PORT_UPDATED: processPortAdded((Device) deviceEvent.subject(), deviceEvent.port()); break; case PORT_REMOVED: processPortRemoved((Device) deviceEvent.subject(), deviceEvent.port()); break; default: break; } }
private void processDeviceAdded(Device device) { log.debug("device {} is added", device.id()); }