/** * Updates ports of the specified device to the specified discovery helper. */ private void updatePorts(LinkDiscovery discoverer, DeviceId deviceId) { deviceService.getPorts(deviceId).forEach(p -> updatePort(discoverer, p)); }
private PortNumber getTunnelPort(DeviceId deviceId) { Port port = deviceService.getPorts(deviceId).stream() .filter(p -> p.annotations().value(PORT_NAME).equals( OpenstackSwitchingManager.PORTNAME_PREFIX_TUNNEL)) .findAny().orElse(null); if (port == null) { log.error("No TunnelPort was created."); return null; } return port.number(); }
private void printAllocation(DeviceId did, int level) { print("%s%s", Strings.repeat(" ", level), did); StreamSupport.stream(deviceService.getPorts(did).spliterator(), false) .map(Port::number) .forEach(num -> printAllocation(did, num, level + 1)); }
@Override protected List<String> choices() { DeviceId deviceId = lookForDeviceId(); if (deviceId == null) { return Collections.emptyList(); } DeviceService deviceService = getService(DeviceService.class); return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false) .map(port -> Long.toString(port.number().toLong())) .collect(Collectors.toList()); } }
@Override protected List<String> choices() { DeviceId deviceId = lookForDeviceId(); if (deviceId == null) { return Collections.emptyList(); } DeviceService deviceService = getService(DeviceService.class); return StreamSupport.stream(deviceService.getPorts(deviceId).spliterator(), false) .map(port -> port.number().toString()) .collect(Collectors.toList()); }
private PortNumber getVxlanPortNumber(DeviceId deviceId) { Iterable<Port> ports = deviceService.getPorts(deviceId); Port vxlanPort = Sets.newHashSet(ports).stream() .filter(p ->!p.number().equals(PortNumber.LOCAL)) .filter(p ->p.annotations().value(AnnotationKeys.PORT_NAME) .startsWith(VXLANPORT_HEAD)) .findFirst().get(); return vxlanPort.number(); } }
@Override public void removePort(String uuid) { // When VMs are remvoed, the flow rules for the VMs are removed using ONOS port update event. // But, when router is removed, no ONOS port event occurs and we need to use Neutron port event. // Here we should not touch any rules for VMs. log.debug("port {} was removed", uuid); String routerPortName = PORTNAME_PREFIX_ROUTER + uuid.substring(0, 11); OpenstackPortInfo routerPortInfo = openstackPortInfoMap.get(routerPortName); if (routerPortInfo != null) { dhcpService.removeStaticMapping(routerPortInfo.mac()); deviceService.getPorts(routerPortInfo.deviceId()).forEach(port -> { String pName = port.annotations().value(PORTNAME); if (pName.equals(routerPortName)) { OpenstackSwitchingRulePopulator rulePopulator = new OpenstackSwitchingRulePopulator(appId, flowObjectiveService, deviceService, openstackService, driverService, config); rulePopulator.removeSwitchingRules(port, openstackPortInfoMap); openstackPortInfoMap.remove(routerPortName); return; } }); } }
private ConnectPoint createConnectPoint(String devicePortString) { String[] splitted = devicePortString.split("/"); checkArgument(splitted.length == 2, "Connect point must be in \"deviceUri/portNumber\" format"); DeviceId deviceId = DeviceId.deviceId(splitted[0]); DeviceService deviceService = get(DeviceService.class); List<Port> ports = deviceService.getPorts(deviceId); for (Port port : ports) { if (splitted[1].equals(port.number().name())) { return new ConnectPoint(deviceId, port.number()); } } return null; }
private Port getExPort(DeviceId deviceId) { List<Port> ports = deviceService.getPorts(deviceId); Port exPort = null; for (Port port : ports) { String portName = port.annotations().value(AnnotationKeys.PORT_NAME); Versioned<String> exPortVersioned = exPortMap.get(EX_PORT_KEY); if (portName != null && exPortVersioned != null && portName. equals(exPortVersioned.value())) { exPort = port; break; } } return exPort; }
@Override public Map<ConnectPoint, SummaryFlowEntryWithLoad> loadSummary(Device device) { checkPermission(STATISTIC_READ); Map<ConnectPoint, SummaryFlowEntryWithLoad> summaryLoad = new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR); if (device == null) { return summaryLoad; } List<Port> ports = new ArrayList<>(deviceService.getPorts(device.id())); for (Port port : ports) { ConnectPoint cp = new ConnectPoint(device.id(), port.number()); SummaryFlowEntryWithLoad sfe = loadSummaryPortInternal(cp); summaryLoad.put(cp, sfe); } return summaryLoad; }
private void loadAllEdgePorts() { deviceService.getAvailableDevices().forEach(d -> deviceService.getPorts(d.id()) .forEach(p -> addEdgePort(new ConnectPoint(d.id(), p.number())))); }
@Override public Map<ConnectPoint, List<FlowEntryWithLoad>> loadAllByType(Device device, FlowEntry.FlowLiveType liveType, Instruction.Type instType) { checkPermission(STATISTIC_READ); Map<ConnectPoint, List<FlowEntryWithLoad>> allLoad = new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR); if (device == null) { return allLoad; } List<Port> ports = new ArrayList<>(deviceService.getPorts(device.id())); for (Port port : ports) { ConnectPoint cp = new ConnectPoint(device.id(), port.number()); List<FlowEntryWithLoad> fel = loadAllPortInternal(cp, liveType, instType); allLoad.put(cp, fel); } return allLoad; }
@Override public Map<ConnectPoint, List<FlowEntryWithLoad>> loadTopnByType(Device device, FlowEntry.FlowLiveType liveType, Instruction.Type instType, int topn) { checkPermission(STATISTIC_READ); Map<ConnectPoint, List<FlowEntryWithLoad>> allLoad = new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR); if (device == null) { return allLoad; } List<Port> ports = new ArrayList<>(deviceService.getPorts(device.id())); for (Port port : ports) { ConnectPoint cp = new ConnectPoint(device.id(), port.number()); List<FlowEntryWithLoad> fel = loadTopnPortInternal(cp, liveType, instType, topn); allLoad.put(cp, fel); } return allLoad; }
/** * Produces JSON array containing ports of the specified device. * * @param service device service * @param mapper object mapper * @param device infrastructure devices * @return JSON array */ public JsonNode jsonPorts(DeviceService service, ObjectMapper mapper, Device device) { ObjectNode result = mapper.createObjectNode(); ArrayNode ports = mapper.createArrayNode(); for (Port port : service.getPorts(device.id())) { if (isIncluded(port)) { ports.add(mapper.createObjectNode() .put("port", port.number().toString()) .put("isEnabled", port.isEnabled()) .put("type", port.type().toString().toLowerCase()) .put("portSpeed", port.portSpeed()) .set("annotations", annotations(mapper, port.annotations()))); } } result.set("device", jsonForEntity(device, Device.class)); result.set("ports", ports); return result; }
protected void printPorts(DeviceService service, Device device) { List<Port> ports = new ArrayList<>(service.getPorts(device.id())); Collections.sort(ports, Comparators.PORT_COMPARATOR); for (Port port : ports) { if (!isIncluded(port)) { continue; } String portName = port.number().toString(); Object portIsEnabled = port.isEnabled() ? "enabled" : "disabled"; String portType = port.type().toString().toLowerCase(); String annotations = annotations(port.annotations()); print(FMT, portName, portIsEnabled, portType, port.portSpeed(), annotations); } } }
private void updatePortsAndStats(DeviceId did) { Device device = deviceService.getDevice(did); if (device.is(DeviceDescriptionDiscovery.class)) { DeviceDescriptionDiscovery discovery = device.as(DeviceDescriptionDiscovery.class); List<PortDescription> portDescriptions = discovery.discoverPortDetails(); if (portDescriptions != null) { providerService.updatePorts(did, portDescriptions); } } else { log.warn("No DeviceDescriptionDiscovery behavior for device {}", did); } try { Collection<PortStatistics> portStats = getPortStatistics(controller.getAgent(did), deviceService.getPorts(did)); providerService.updatePortStatistics(did, portStats); } catch (Bmv2RuntimeException e) { log.warn("Unable to get port statistics for {}: {}", did, e.explain()); } }
private ConnectPoint staticPort(ConnectPoint connectPoint) { Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); // FIXME: need a better way to match the port if (staticPort != null) { for (Port p : deviceService.getPorts(connectPoint.deviceId())) { if (staticPort.equals(p.number().name())) { return new ConnectPoint(p.element().id(), p.number()); } } } return null; }
private ConnectPoint staticPort(ConnectPoint connectPoint) { Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); // FIXME: need a better way to match the port if (staticPort != null) { for (Port p : deviceService.getPorts(connectPoint.deviceId())) { if (staticPort.equals(p.number().name())) { return new ConnectPoint(p.element().id(), p.number()); } } } return null; }
@Override public int complete(Session session, CommandLine commandLine, List<String> candidates) { // Delegate string completer StringsCompleter delegate = new StringsCompleter(); // Fetch our service and feed it's offerings to the string completer DeviceService service = AbstractShellCommand.get(DeviceService.class); // Generate the device ID/port number identifiers for (Device device : service.getDevices()) { SortedSet<String> strings = delegate.getStrings(); for (Port port : service.getPorts(device.id())) { if (!port.number().isLogical()) { strings.add(device.id().toString() + "/" + port.number()); } } } // Now let the completer do the work for figuring out what to offer. return delegate.complete(session, commandLine, candidates); }
private void processDeviceEvent(DeviceEvent event) { //FIXME handle the case where a device is suspended, this may or may not come up DeviceEvent.Type type = event.type(); DeviceId id = event.subject().id(); if (type == DEVICE_ADDED || type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) { // When device is added or becomes available, add all its ports deviceService.getPorts(event.subject().id()) .forEach(p -> addEdgePort(new ConnectPoint(id, p.number()))); } else if (type == DEVICE_REMOVED || type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) { // When device is removed or becomes unavailable, remove all its ports. // Note: cannot rely on Device subsystem, ports may be gone. Optional.ofNullable(connectionPoints.remove(id)) .orElse(ImmutableSet.of()) .forEach(point -> post(new EdgePortEvent(EDGE_PORT_REMOVED, point))); } else if (type == DeviceEvent.Type.PORT_ADDED || type == PORT_UPDATED && event.port().isEnabled()) { addEdgePort(new ConnectPoint(id, event.port().number())); } else if (type == DeviceEvent.Type.PORT_REMOVED || type == PORT_UPDATED && !event.port().isEnabled()) { removeEdgePort(new ConnectPoint(id, event.port().number())); } }