tom

Enhanced the CLIs.

......@@ -24,7 +24,7 @@ public class DeviceIdCompleter implements Completer {
Iterator<Device> it = service.getDevices().iterator();
SortedSet<String> strings = delegate.getStrings();
while (it.hasNext()) {
strings.add(it.next().id().uri().toString());
strings.add(it.next().id().toString());
}
// Now let the completer do the work for figuring out what to offer.
......
......@@ -2,10 +2,15 @@ package org.onlab.onos.cli.net;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cli.AbstractShellCommand;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.Port;
import org.onlab.onos.net.device.DeviceService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static org.onlab.onos.net.DeviceId.deviceId;
/**
......@@ -13,21 +18,42 @@ import static org.onlab.onos.net.DeviceId.deviceId;
*/
@Command(scope = "onos", name = "ports",
description = "Lists all ports of a device")
public class DevicePortsListCommand extends AbstractShellCommand {
public class DevicePortsListCommand extends DevicesListCommand {
private static final String FMT = "port=%s, state=%s";
private static final String FMT = " port=%s, state=%s";
@Argument(index = 0, name = "deviceId", description = "Device ID",
required = true, multiValued = false)
String deviceId = null;
required = false, multiValued = false)
String uri = null;
private static final Comparator<Port> PORT_COMPARATOR = new Comparator<Port>() {
@Override
public int compare(Port p1, Port p2) {
long delta = p1.number().toLong() - p2.number().toLong();
return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
}
};
@Override
protected Object doExecute() throws Exception {
DeviceService service = getService(DeviceService.class);
Iterable<Port> ports = service.getPorts(deviceId(deviceId));
if (uri == null) {
for (Device device : service.getDevices()) {
printDevicePorts(service, device);
}
} else {
printDevicePorts(service, service.getDevice(deviceId(uri)));
}
return null;
}
private void printDevicePorts(DeviceService service, Device device) {
List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
Collections.sort(ports, PORT_COMPARATOR);
printDevice(device, service.isAvailable(device.id()));
for (Port port : ports) {
print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
}
return null;
}
}
......
......@@ -13,14 +13,27 @@ import org.onlab.onos.net.device.DeviceService;
public class DevicesListCommand extends AbstractShellCommand {
private static final String FMT =
"id=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s";
"id=%s, available=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s";
@Override
protected Object doExecute() throws Exception {
for (Device device : getService(DeviceService.class).getDevices()) {
print(FMT, device.id().uri(), device.type(), device.manufacturer(),
device.hwVersion(), device.swVersion(), device.serialNumber());
DeviceService service = getService(DeviceService.class);
for (Device device : service.getDevices()) {
printDevice(device, service.isAvailable(device.id()));
}
return null;
}
/**
* Prints information about the specified device.
*
* @param device infrastructure device
* @param isAvailable true of device is available
*/
protected void printDevice(Device device, boolean isAvailable) {
print(FMT, device.id(), isAvailable, device.type(),
device.manufacturer(), device.hwVersion(), device.swVersion(),
device.serialNumber());
}
}
......
......@@ -27,8 +27,8 @@ public class LinksListCommand extends AbstractShellCommand {
Iterable<Link> links = deviceId != null ?
service.getDeviceLinks(deviceId(deviceId)) : service.getLinks();
for (Link link : links) {
print(FMT, link.src().deviceId().uri(), link.src().port(),
link.dst().deviceId().uri(), link.dst().port(), link.type());
print(FMT, link.src().deviceId(), link.src().port(),
link.dst().deviceId(), link.dst().port(), link.type());
}
return null;
}
......
......@@ -3,8 +3,6 @@ package org.onlab.onos.net;
import java.net.URI;
import java.util.Objects;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Immutable representation of a network element identity.
*/
......@@ -47,7 +45,7 @@ public abstract class ElementId {
@Override
public String toString() {
return toStringHelper(this).add("uri", uri).toString();
return uri.toString();
}
}
......
......@@ -55,6 +55,7 @@ public interface DeviceService {
/**
* Returns the port with the specified number and hosted by the given device.
*
* @param deviceId device identifier
* @param portNumber port number
* @return device port
......@@ -62,6 +63,14 @@ public interface DeviceService {
Port getPort(DeviceId deviceId, PortNumber portNumber);
/**
* Indicates whether or not the device is presently online and available.
*
* @param deviceId device identifier
* @return true if the device is available
*/
boolean isAvailable(DeviceId deviceId);
/**
* Adds the specified device listener.
*
* @param listener device listener
......
......@@ -104,6 +104,12 @@ public class SimpleDeviceManager
}
@Override
public boolean isAvailable(DeviceId deviceId) {
checkNotNull(deviceId, DEVICE_ID_NULL);
return store.isAvailable(deviceId);
}
@Override
public void addListener(DeviceListener listener) {
listenerRegistry.addListener(listener);
}
......@@ -185,8 +191,10 @@ public class SimpleDeviceManager
checkNotNull(deviceId, DEVICE_ID_NULL);
checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
checkValidity();
log.info("Device {} port status changed", deviceId);
DeviceEvent event = store.updatePortStatus(deviceId, portDescription);
if (event != null) {
log.info("Device {} port status changed", deviceId);
}
post(event);
}
}
......
......@@ -271,6 +271,16 @@ class SimpleDeviceStore {
}
/**
* Indicates whether the specified device is available/online.
*
* @param deviceId device identifier
* @return true if device is available
*/
boolean isAvailable(DeviceId deviceId) {
return availableDevices.contains(deviceId);
}
/**
* Returns the mastership role determined for this device.
*
* @param deviceId device identifier
......
......@@ -104,6 +104,7 @@ public class SimpleDeviceManagerTest {
assertNotNull("one device expected", it.next());
assertFalse("only one device expected", it.hasNext());
assertEquals("incorrect device count", 1, service.getDeviceCount());
assertTrue("device should be available", service.isAvailable(DID1));
}
@Test
......@@ -111,10 +112,12 @@ public class SimpleDeviceManagerTest {
connectDevice(DID1, SW1);
connectDevice(DID2, SW1);
validateEvents(DEVICE_ADDED, DEVICE_ADDED);
assertTrue("device should be available", service.isAvailable(DID1));
// Disconnect
providerService.deviceDisconnected(DID1);
assertNotNull("device should not be found", service.getDevice(DID1));
assertFalse("device should not be available", service.isAvailable(DID1));
validateEvents(DEVICE_AVAILABILITY_CHANGED);
// Reconnect
......
package org.onlab.onos.of.controller.impl;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -21,6 +16,14 @@ import org.projectfloodlight.openflow.protocol.OFPortStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@Component(immediate = true)
@Service
public class OpenFlowControllerImpl implements OpenFlowController {
......@@ -36,11 +39,11 @@ public class OpenFlowControllerImpl implements OpenFlowController {
new ConcurrentHashMap<Dpid, OpenFlowSwitch>();
protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
protected ArrayList<OpenFlowSwitchListener> ofEventListener =
new ArrayList<OpenFlowSwitchListener>();
protected Set<OpenFlowSwitchListener> ofEventListener =
new HashSet<>();
protected ArrayList<PacketListener> ofPacketListener =
new ArrayList<PacketListener>();
protected List<PacketListener> ofPacketListener =
new ArrayList<>();
private final Controller ctrl = new Controller();
......@@ -139,7 +142,6 @@ public class OpenFlowControllerImpl implements OpenFlowController {
* Implementation of an OpenFlow Agent which is responsible for
* keeping track of connected switches and the state in which
* they are.
*
*/
public class OpenFlowSwitchAgent implements OpenFlowAgent {
......@@ -174,7 +176,7 @@ public class OpenFlowControllerImpl implements OpenFlowController {
activeEqualSwitches.get(dpid) != null) {
log.error("Trying to activate switch but it is already "
+ "activated: dpid {}. Found in activeMaster: {} "
+ "Found in activeEqual: {}. Aborting ..", new Object[] {
+ "Found in activeEqual: {}. Aborting ..", new Object[]{
dpid,
(activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
(activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'});
......@@ -274,5 +276,4 @@ public class OpenFlowControllerImpl implements OpenFlowController {
}
}
......
......@@ -140,6 +140,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
/**
* Given a dpid builds a URI for the device.
*
* @param dpid the dpid to build the uri from
* @return returns a uri of the form of:<dpidHexForm>
*/
......@@ -155,6 +156,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
/**
* Builds a list of port descriptions for a given list of ports.
*
* @param ports the list of ports
* @return list of portdescriptions
*/
......@@ -169,6 +171,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
/**
* Build a portDescription from a given port.
*
* @param port the port to build from.
* @return portDescription for the port.
*/
......