tom

Enhanced the CLIs.

...@@ -24,7 +24,7 @@ public class DeviceIdCompleter implements Completer { ...@@ -24,7 +24,7 @@ public class DeviceIdCompleter implements Completer {
24 Iterator<Device> it = service.getDevices().iterator(); 24 Iterator<Device> it = service.getDevices().iterator();
25 SortedSet<String> strings = delegate.getStrings(); 25 SortedSet<String> strings = delegate.getStrings();
26 while (it.hasNext()) { 26 while (it.hasNext()) {
27 - strings.add(it.next().id().uri().toString()); 27 + strings.add(it.next().id().toString());
28 } 28 }
29 29
30 // Now let the completer do the work for figuring out what to offer. 30 // Now let the completer do the work for figuring out what to offer.
......
...@@ -2,10 +2,15 @@ package org.onlab.onos.cli.net; ...@@ -2,10 +2,15 @@ package org.onlab.onos.cli.net;
2 2
3 import org.apache.karaf.shell.commands.Argument; 3 import org.apache.karaf.shell.commands.Argument;
4 import org.apache.karaf.shell.commands.Command; 4 import org.apache.karaf.shell.commands.Command;
5 -import org.onlab.onos.cli.AbstractShellCommand; 5 +import org.onlab.onos.net.Device;
6 import org.onlab.onos.net.Port; 6 import org.onlab.onos.net.Port;
7 import org.onlab.onos.net.device.DeviceService; 7 import org.onlab.onos.net.device.DeviceService;
8 8
9 +import java.util.ArrayList;
10 +import java.util.Collections;
11 +import java.util.Comparator;
12 +import java.util.List;
13 +
9 import static org.onlab.onos.net.DeviceId.deviceId; 14 import static org.onlab.onos.net.DeviceId.deviceId;
10 15
11 /** 16 /**
...@@ -13,21 +18,42 @@ import static org.onlab.onos.net.DeviceId.deviceId; ...@@ -13,21 +18,42 @@ import static org.onlab.onos.net.DeviceId.deviceId;
13 */ 18 */
14 @Command(scope = "onos", name = "ports", 19 @Command(scope = "onos", name = "ports",
15 description = "Lists all ports of a device") 20 description = "Lists all ports of a device")
16 -public class DevicePortsListCommand extends AbstractShellCommand { 21 +public class DevicePortsListCommand extends DevicesListCommand {
17 22
18 - private static final String FMT = "port=%s, state=%s"; 23 + private static final String FMT = " port=%s, state=%s";
19 24
20 @Argument(index = 0, name = "deviceId", description = "Device ID", 25 @Argument(index = 0, name = "deviceId", description = "Device ID",
21 - required = true, multiValued = false) 26 + required = false, multiValued = false)
22 - String deviceId = null; 27 + String uri = null;
28 +
29 + private static final Comparator<Port> PORT_COMPARATOR = new Comparator<Port>() {
30 + @Override
31 + public int compare(Port p1, Port p2) {
32 + long delta = p1.number().toLong() - p2.number().toLong();
33 + return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
34 + }
35 + };
23 36
24 @Override 37 @Override
25 protected Object doExecute() throws Exception { 38 protected Object doExecute() throws Exception {
26 DeviceService service = getService(DeviceService.class); 39 DeviceService service = getService(DeviceService.class);
27 - Iterable<Port> ports = service.getPorts(deviceId(deviceId)); 40 + if (uri == null) {
41 + for (Device device : service.getDevices()) {
42 + printDevicePorts(service, device);
43 + }
44 + } else {
45 + printDevicePorts(service, service.getDevice(deviceId(uri)));
46 + }
47 + return null;
48 + }
49 +
50 + private void printDevicePorts(DeviceService service, Device device) {
51 + List<Port> ports = new ArrayList<>(service.getPorts(device.id()));
52 + Collections.sort(ports, PORT_COMPARATOR);
53 + printDevice(device, service.isAvailable(device.id()));
28 for (Port port : ports) { 54 for (Port port : ports) {
29 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled"); 55 print(FMT, port.number(), port.isEnabled() ? "enabled" : "disabled");
30 } 56 }
31 - return null;
32 } 57 }
58 +
33 } 59 }
......
...@@ -13,14 +13,27 @@ import org.onlab.onos.net.device.DeviceService; ...@@ -13,14 +13,27 @@ import org.onlab.onos.net.device.DeviceService;
13 public class DevicesListCommand extends AbstractShellCommand { 13 public class DevicesListCommand extends AbstractShellCommand {
14 14
15 private static final String FMT = 15 private static final String FMT =
16 - "id=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s"; 16 + "id=%s, available=%s, type=%s, mfr=%s, hw=%s, sw=%s, serial=%s";
17 17
18 @Override 18 @Override
19 protected Object doExecute() throws Exception { 19 protected Object doExecute() throws Exception {
20 - for (Device device : getService(DeviceService.class).getDevices()) { 20 + DeviceService service = getService(DeviceService.class);
21 - print(FMT, device.id().uri(), device.type(), device.manufacturer(), 21 + for (Device device : service.getDevices()) {
22 - device.hwVersion(), device.swVersion(), device.serialNumber()); 22 + printDevice(device, service.isAvailable(device.id()));
23 } 23 }
24 return null; 24 return null;
25 } 25 }
26 +
27 + /**
28 + * Prints information about the specified device.
29 + *
30 + * @param device infrastructure device
31 + * @param isAvailable true of device is available
32 + */
33 + protected void printDevice(Device device, boolean isAvailable) {
34 + print(FMT, device.id(), isAvailable, device.type(),
35 + device.manufacturer(), device.hwVersion(), device.swVersion(),
36 + device.serialNumber());
37 + }
38 +
26 } 39 }
......
...@@ -27,8 +27,8 @@ public class LinksListCommand extends AbstractShellCommand { ...@@ -27,8 +27,8 @@ public class LinksListCommand extends AbstractShellCommand {
27 Iterable<Link> links = deviceId != null ? 27 Iterable<Link> links = deviceId != null ?
28 service.getDeviceLinks(deviceId(deviceId)) : service.getLinks(); 28 service.getDeviceLinks(deviceId(deviceId)) : service.getLinks();
29 for (Link link : links) { 29 for (Link link : links) {
30 - print(FMT, link.src().deviceId().uri(), link.src().port(), 30 + print(FMT, link.src().deviceId(), link.src().port(),
31 - link.dst().deviceId().uri(), link.dst().port(), link.type()); 31 + link.dst().deviceId(), link.dst().port(), link.type());
32 } 32 }
33 return null; 33 return null;
34 } 34 }
......
...@@ -3,8 +3,6 @@ package org.onlab.onos.net; ...@@ -3,8 +3,6 @@ package org.onlab.onos.net;
3 import java.net.URI; 3 import java.net.URI;
4 import java.util.Objects; 4 import java.util.Objects;
5 5
6 -import static com.google.common.base.MoreObjects.toStringHelper;
7 -
8 /** 6 /**
9 * Immutable representation of a network element identity. 7 * Immutable representation of a network element identity.
10 */ 8 */
...@@ -47,7 +45,7 @@ public abstract class ElementId { ...@@ -47,7 +45,7 @@ public abstract class ElementId {
47 45
48 @Override 46 @Override
49 public String toString() { 47 public String toString() {
50 - return toStringHelper(this).add("uri", uri).toString(); 48 + return uri.toString();
51 } 49 }
52 50
53 } 51 }
......
...@@ -55,6 +55,7 @@ public interface DeviceService { ...@@ -55,6 +55,7 @@ public interface DeviceService {
55 55
56 /** 56 /**
57 * Returns the port with the specified number and hosted by the given device. 57 * Returns the port with the specified number and hosted by the given device.
58 + *
58 * @param deviceId device identifier 59 * @param deviceId device identifier
59 * @param portNumber port number 60 * @param portNumber port number
60 * @return device port 61 * @return device port
...@@ -62,6 +63,14 @@ public interface DeviceService { ...@@ -62,6 +63,14 @@ public interface DeviceService {
62 Port getPort(DeviceId deviceId, PortNumber portNumber); 63 Port getPort(DeviceId deviceId, PortNumber portNumber);
63 64
64 /** 65 /**
66 + * Indicates whether or not the device is presently online and available.
67 + *
68 + * @param deviceId device identifier
69 + * @return true if the device is available
70 + */
71 + boolean isAvailable(DeviceId deviceId);
72 +
73 + /**
65 * Adds the specified device listener. 74 * Adds the specified device listener.
66 * 75 *
67 * @param listener device listener 76 * @param listener device listener
......
...@@ -104,6 +104,12 @@ public class SimpleDeviceManager ...@@ -104,6 +104,12 @@ public class SimpleDeviceManager
104 } 104 }
105 105
106 @Override 106 @Override
107 + public boolean isAvailable(DeviceId deviceId) {
108 + checkNotNull(deviceId, DEVICE_ID_NULL);
109 + return store.isAvailable(deviceId);
110 + }
111 +
112 + @Override
107 public void addListener(DeviceListener listener) { 113 public void addListener(DeviceListener listener) {
108 listenerRegistry.addListener(listener); 114 listenerRegistry.addListener(listener);
109 } 115 }
...@@ -185,8 +191,10 @@ public class SimpleDeviceManager ...@@ -185,8 +191,10 @@ public class SimpleDeviceManager
185 checkNotNull(deviceId, DEVICE_ID_NULL); 191 checkNotNull(deviceId, DEVICE_ID_NULL);
186 checkNotNull(portDescription, PORT_DESCRIPTION_NULL); 192 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
187 checkValidity(); 193 checkValidity();
188 - log.info("Device {} port status changed", deviceId);
189 DeviceEvent event = store.updatePortStatus(deviceId, portDescription); 194 DeviceEvent event = store.updatePortStatus(deviceId, portDescription);
195 + if (event != null) {
196 + log.info("Device {} port status changed", deviceId);
197 + }
190 post(event); 198 post(event);
191 } 199 }
192 } 200 }
......
...@@ -271,6 +271,16 @@ class SimpleDeviceStore { ...@@ -271,6 +271,16 @@ class SimpleDeviceStore {
271 } 271 }
272 272
273 /** 273 /**
274 + * Indicates whether the specified device is available/online.
275 + *
276 + * @param deviceId device identifier
277 + * @return true if device is available
278 + */
279 + boolean isAvailable(DeviceId deviceId) {
280 + return availableDevices.contains(deviceId);
281 + }
282 +
283 + /**
274 * Returns the mastership role determined for this device. 284 * Returns the mastership role determined for this device.
275 * 285 *
276 * @param deviceId device identifier 286 * @param deviceId device identifier
......
...@@ -104,6 +104,7 @@ public class SimpleDeviceManagerTest { ...@@ -104,6 +104,7 @@ public class SimpleDeviceManagerTest {
104 assertNotNull("one device expected", it.next()); 104 assertNotNull("one device expected", it.next());
105 assertFalse("only one device expected", it.hasNext()); 105 assertFalse("only one device expected", it.hasNext());
106 assertEquals("incorrect device count", 1, service.getDeviceCount()); 106 assertEquals("incorrect device count", 1, service.getDeviceCount());
107 + assertTrue("device should be available", service.isAvailable(DID1));
107 } 108 }
108 109
109 @Test 110 @Test
...@@ -111,10 +112,12 @@ public class SimpleDeviceManagerTest { ...@@ -111,10 +112,12 @@ public class SimpleDeviceManagerTest {
111 connectDevice(DID1, SW1); 112 connectDevice(DID1, SW1);
112 connectDevice(DID2, SW1); 113 connectDevice(DID2, SW1);
113 validateEvents(DEVICE_ADDED, DEVICE_ADDED); 114 validateEvents(DEVICE_ADDED, DEVICE_ADDED);
115 + assertTrue("device should be available", service.isAvailable(DID1));
114 116
115 // Disconnect 117 // Disconnect
116 providerService.deviceDisconnected(DID1); 118 providerService.deviceDisconnected(DID1);
117 assertNotNull("device should not be found", service.getDevice(DID1)); 119 assertNotNull("device should not be found", service.getDevice(DID1));
120 + assertFalse("device should not be available", service.isAvailable(DID1));
118 validateEvents(DEVICE_AVAILABILITY_CHANGED); 121 validateEvents(DEVICE_AVAILABILITY_CHANGED);
119 122
120 // Reconnect 123 // Reconnect
......
1 package org.onlab.onos.of.controller.impl; 1 package org.onlab.onos.of.controller.impl;
2 2
3 -import java.util.ArrayList;
4 -import java.util.concurrent.ConcurrentHashMap;
5 -import java.util.concurrent.locks.Lock;
6 -import java.util.concurrent.locks.ReentrantLock;
7 -
8 import org.apache.felix.scr.annotations.Activate; 3 import org.apache.felix.scr.annotations.Activate;
9 import org.apache.felix.scr.annotations.Component; 4 import org.apache.felix.scr.annotations.Component;
10 import org.apache.felix.scr.annotations.Deactivate; 5 import org.apache.felix.scr.annotations.Deactivate;
...@@ -21,6 +16,14 @@ import org.projectfloodlight.openflow.protocol.OFPortStatus; ...@@ -21,6 +16,14 @@ import org.projectfloodlight.openflow.protocol.OFPortStatus;
21 import org.slf4j.Logger; 16 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory; 17 import org.slf4j.LoggerFactory;
23 18
19 +import java.util.ArrayList;
20 +import java.util.HashSet;
21 +import java.util.List;
22 +import java.util.Set;
23 +import java.util.concurrent.ConcurrentHashMap;
24 +import java.util.concurrent.locks.Lock;
25 +import java.util.concurrent.locks.ReentrantLock;
26 +
24 @Component(immediate = true) 27 @Component(immediate = true)
25 @Service 28 @Service
26 public class OpenFlowControllerImpl implements OpenFlowController { 29 public class OpenFlowControllerImpl implements OpenFlowController {
...@@ -36,11 +39,11 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -36,11 +39,11 @@ public class OpenFlowControllerImpl implements OpenFlowController {
36 new ConcurrentHashMap<Dpid, OpenFlowSwitch>(); 39 new ConcurrentHashMap<Dpid, OpenFlowSwitch>();
37 40
38 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent(); 41 protected OpenFlowSwitchAgent agent = new OpenFlowSwitchAgent();
39 - protected ArrayList<OpenFlowSwitchListener> ofEventListener = 42 + protected Set<OpenFlowSwitchListener> ofEventListener =
40 - new ArrayList<OpenFlowSwitchListener>(); 43 + new HashSet<>();
41 44
42 - protected ArrayList<PacketListener> ofPacketListener = 45 + protected List<PacketListener> ofPacketListener =
43 - new ArrayList<PacketListener>(); 46 + new ArrayList<>();
44 47
45 private final Controller ctrl = new Controller(); 48 private final Controller ctrl = new Controller();
46 49
...@@ -139,7 +142,6 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -139,7 +142,6 @@ public class OpenFlowControllerImpl implements OpenFlowController {
139 * Implementation of an OpenFlow Agent which is responsible for 142 * Implementation of an OpenFlow Agent which is responsible for
140 * keeping track of connected switches and the state in which 143 * keeping track of connected switches and the state in which
141 * they are. 144 * they are.
142 - *
143 */ 145 */
144 public class OpenFlowSwitchAgent implements OpenFlowAgent { 146 public class OpenFlowSwitchAgent implements OpenFlowAgent {
145 147
...@@ -174,7 +176,7 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -174,7 +176,7 @@ public class OpenFlowControllerImpl implements OpenFlowController {
174 activeEqualSwitches.get(dpid) != null) { 176 activeEqualSwitches.get(dpid) != null) {
175 log.error("Trying to activate switch but it is already " 177 log.error("Trying to activate switch but it is already "
176 + "activated: dpid {}. Found in activeMaster: {} " 178 + "activated: dpid {}. Found in activeMaster: {} "
177 - + "Found in activeEqual: {}. Aborting ..", new Object[] { 179 + + "Found in activeEqual: {}. Aborting ..", new Object[]{
178 dpid, 180 dpid,
179 (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y', 181 (activeMasterSwitches.get(dpid) == null) ? 'N' : 'Y',
180 (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'}); 182 (activeEqualSwitches.get(dpid) == null) ? 'N' : 'Y'});
...@@ -274,5 +276,4 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -274,5 +276,4 @@ public class OpenFlowControllerImpl implements OpenFlowController {
274 } 276 }
275 277
276 278
277 -
278 } 279 }
......
...@@ -140,6 +140,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -140,6 +140,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
140 140
141 /** 141 /**
142 * Given a dpid builds a URI for the device. 142 * Given a dpid builds a URI for the device.
143 + *
143 * @param dpid the dpid to build the uri from 144 * @param dpid the dpid to build the uri from
144 * @return returns a uri of the form of:<dpidHexForm> 145 * @return returns a uri of the form of:<dpidHexForm>
145 */ 146 */
...@@ -155,6 +156,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -155,6 +156,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
155 156
156 /** 157 /**
157 * Builds a list of port descriptions for a given list of ports. 158 * Builds a list of port descriptions for a given list of ports.
159 + *
158 * @param ports the list of ports 160 * @param ports the list of ports
159 * @return list of portdescriptions 161 * @return list of portdescriptions
160 */ 162 */
...@@ -169,6 +171,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr ...@@ -169,6 +171,7 @@ public class OpenFlowDeviceProvider extends AbstractProvider implements DevicePr
169 171
170 /** 172 /**
171 * Build a portDescription from a given port. 173 * Build a portDescription from a given port.
174 + *
172 * @param port the port to build from. 175 * @param port the port to build from.
173 * @return portDescription for the port. 176 * @return portDescription for the port.
174 */ 177 */
......