alshabib

Merge branch 'master' of ssh://gerrit.onlab.us:29418/onos-next

...@@ -14,12 +14,11 @@ import java.util.List; ...@@ -14,12 +14,11 @@ import java.util.List;
14 public interface DeviceService { 14 public interface DeviceService {
15 15
16 /** 16 /**
17 - * Returns the current mastership role for the specified device. 17 + * Returns the number of infrastructure devices known to the system.
18 * 18 *
19 - * @param deviceId device identifier 19 + * @return number of infrastructure devices
20 - * @return designated mastership role
21 */ 20 */
22 - MastershipRole getRole(DeviceId deviceId); 21 + int getDeviceCount();
23 22
24 /** 23 /**
25 * Returns a collection of the currently known infrastructure 24 * Returns a collection of the currently known infrastructure
...@@ -37,6 +36,14 @@ public interface DeviceService { ...@@ -37,6 +36,14 @@ public interface DeviceService {
37 */ 36 */
38 Device getDevice(DeviceId deviceId); 37 Device getDevice(DeviceId deviceId);
39 38
39 + /**
40 + * Returns the current mastership role for the specified device.
41 + *
42 + * @param deviceId device identifier
43 + * @return designated mastership role
44 + */
45 + MastershipRole getRole(DeviceId deviceId);
46 +
40 47
41 /** 48 /**
42 * Returns the list of ports associated with the device. 49 * Returns the list of ports associated with the device.
......
...@@ -69,9 +69,8 @@ public class SimpleDeviceManager ...@@ -69,9 +69,8 @@ public class SimpleDeviceManager
69 } 69 }
70 70
71 @Override 71 @Override
72 - public MastershipRole getRole(DeviceId deviceId) { 72 + public int getDeviceCount() {
73 - checkNotNull(deviceId, DEVICE_ID_NULL); 73 + return store.getDeviceCount();
74 - return store.getRole(deviceId);
75 } 74 }
76 75
77 @Override 76 @Override
...@@ -86,6 +85,12 @@ public class SimpleDeviceManager ...@@ -86,6 +85,12 @@ public class SimpleDeviceManager
86 } 85 }
87 86
88 @Override 87 @Override
88 + public MastershipRole getRole(DeviceId deviceId) {
89 + checkNotNull(deviceId, DEVICE_ID_NULL);
90 + return store.getRole(deviceId);
91 + }
92 +
93 + @Override
89 public List<Port> getPorts(DeviceId deviceId) { 94 public List<Port> getPorts(DeviceId deviceId) {
90 checkNotNull(deviceId, DEVICE_ID_NULL); 95 checkNotNull(deviceId, DEVICE_ID_NULL);
91 return store.getPorts(deviceId); 96 return store.getPorts(deviceId);
...@@ -147,7 +152,7 @@ public class SimpleDeviceManager ...@@ -147,7 +152,7 @@ public class SimpleDeviceManager
147 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { 152 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
148 checkNotNull(deviceId, DEVICE_ID_NULL); 153 checkNotNull(deviceId, DEVICE_ID_NULL);
149 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL); 154 checkNotNull(deviceDescription, DEVICE_DESCRIPTION_NULL);
150 - log.info("Device {} connected: {}", deviceId, deviceDescription); 155 + log.info("Device {} connected", deviceId);
151 DeviceEvent event = store.createOrUpdateDevice(provider().id(), 156 DeviceEvent event = store.createOrUpdateDevice(provider().id(),
152 deviceId, deviceDescription); 157 deviceId, deviceDescription);
153 post(event); 158 post(event);
...@@ -165,7 +170,7 @@ public class SimpleDeviceManager ...@@ -165,7 +170,7 @@ public class SimpleDeviceManager
165 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { 170 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
166 checkNotNull(deviceId, DEVICE_ID_NULL); 171 checkNotNull(deviceId, DEVICE_ID_NULL);
167 checkNotNull(portDescriptions, "Port descriptions list cannot be null"); 172 checkNotNull(portDescriptions, "Port descriptions list cannot be null");
168 - log.info("Device {} ports updated: {}", portDescriptions); 173 + log.info("Device {} ports updated", deviceId);
169 List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions); 174 List<DeviceEvent> events = store.updatePorts(deviceId, portDescriptions);
170 for (DeviceEvent event : events) { 175 for (DeviceEvent event : events) {
171 post(event); 176 post(event);
...@@ -176,7 +181,7 @@ public class SimpleDeviceManager ...@@ -176,7 +181,7 @@ public class SimpleDeviceManager
176 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { 181 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
177 checkNotNull(deviceId, DEVICE_ID_NULL); 182 checkNotNull(deviceId, DEVICE_ID_NULL);
178 checkNotNull(portDescription, PORT_DESCRIPTION_NULL); 183 checkNotNull(portDescription, PORT_DESCRIPTION_NULL);
179 - log.info("Device {} port status changed: {}", deviceId, portDescription); 184 + log.info("Device {} port status changed", deviceId);
180 DeviceEvent event = store.updatePortStatus(deviceId, portDescription); 185 DeviceEvent event = store.updatePortStatus(deviceId, portDescription);
181 post(event); 186 post(event);
182 } 187 }
......
...@@ -28,7 +28,8 @@ import static com.google.common.base.Preconditions.checkArgument; ...@@ -28,7 +28,8 @@ import static com.google.common.base.Preconditions.checkArgument;
28 import static org.onlab.onos.net.device.DeviceEvent.Type.*; 28 import static org.onlab.onos.net.device.DeviceEvent.Type.*;
29 29
30 /** 30 /**
31 - * Manages inventory of infrastructure devices. 31 + * Manages inventory of infrastructure devices using trivial in-memory
32 + * implementation.
32 */ 33 */
33 class SimpleDeviceStore { 34 class SimpleDeviceStore {
34 35
...@@ -40,6 +41,15 @@ class SimpleDeviceStore { ...@@ -40,6 +41,15 @@ class SimpleDeviceStore {
40 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>(); 41 private final Map<DeviceId, Map<PortNumber, Port>> devicePorts = new HashMap<>();
41 42
42 /** 43 /**
44 + * Returns the number of devices known to the system.
45 + *
46 + * @return number of devices
47 + */
48 + public int getDeviceCount() {
49 + return devices.size();
50 + }
51 +
52 + /**
43 * Returns an iterable collection of all devices known to the system. 53 * Returns an iterable collection of all devices known to the system.
44 * 54 *
45 * @return device collection 55 * @return device collection
...@@ -153,9 +163,9 @@ class SimpleDeviceStore { ...@@ -153,9 +163,9 @@ class SimpleDeviceStore {
153 Set<PortNumber> processed = new HashSet<>(); 163 Set<PortNumber> processed = new HashSet<>();
154 for (PortDescription portDescription : portDescriptions) { 164 for (PortDescription portDescription : portDescriptions) {
155 Port port = ports.get(portDescription.portNumber()); 165 Port port = ports.get(portDescription.portNumber());
156 - DeviceEvent event = port == null ? 166 + events.add(port == null ?
157 - createPort(device, portDescription, ports) : 167 + createPort(device, portDescription, ports) :
158 - updatePort(device, port, portDescription, ports); 168 + updatePort(device, port, portDescription, ports));
159 processed.add(portDescription.portNumber()); 169 processed.add(portDescription.portNumber());
160 } 170 }
161 171
...@@ -198,7 +208,7 @@ class SimpleDeviceStore { ...@@ -198,7 +208,7 @@ class SimpleDeviceStore {
198 Iterator<PortNumber> iterator = ports.keySet().iterator(); 208 Iterator<PortNumber> iterator = ports.keySet().iterator();
199 while (iterator.hasNext()) { 209 while (iterator.hasNext()) {
200 PortNumber portNumber = iterator.next(); 210 PortNumber portNumber = iterator.next();
201 - if (processed.contains(portNumber)) { 211 + if (!processed.contains(portNumber)) {
202 events.add(new DeviceEvent(PORT_REMOVED, device, 212 events.add(new DeviceEvent(PORT_REMOVED, device,
203 ports.get(portNumber))); 213 ports.get(portNumber)));
204 iterator.remove(); 214 iterator.remove();
...@@ -301,5 +311,4 @@ class SimpleDeviceStore { ...@@ -301,5 +311,4 @@ class SimpleDeviceStore {
301 new DeviceEvent(DEVICE_REMOVED, device, null); 311 new DeviceEvent(DEVICE_REMOVED, device, null);
302 } 312 }
303 } 313 }
304 -
305 } 314 }
......
...@@ -7,7 +7,10 @@ import org.onlab.onos.event.Event; ...@@ -7,7 +7,10 @@ import org.onlab.onos.event.Event;
7 import org.onlab.onos.net.Device; 7 import org.onlab.onos.net.Device;
8 import org.onlab.onos.net.DeviceId; 8 import org.onlab.onos.net.DeviceId;
9 import org.onlab.onos.net.MastershipRole; 9 import org.onlab.onos.net.MastershipRole;
10 +import org.onlab.onos.net.Port;
11 +import org.onlab.onos.net.PortNumber;
10 import org.onlab.onos.net.device.DefaultDeviceDescription; 12 import org.onlab.onos.net.device.DefaultDeviceDescription;
13 +import org.onlab.onos.net.device.DefaultPortDescription;
11 import org.onlab.onos.net.device.DeviceAdminService; 14 import org.onlab.onos.net.device.DeviceAdminService;
12 import org.onlab.onos.net.device.DeviceDescription; 15 import org.onlab.onos.net.device.DeviceDescription;
13 import org.onlab.onos.net.device.DeviceEvent; 16 import org.onlab.onos.net.device.DeviceEvent;
...@@ -16,6 +19,7 @@ import org.onlab.onos.net.device.DeviceProvider; ...@@ -16,6 +19,7 @@ import org.onlab.onos.net.device.DeviceProvider;
16 import org.onlab.onos.net.device.DeviceProviderRegistry; 19 import org.onlab.onos.net.device.DeviceProviderRegistry;
17 import org.onlab.onos.net.device.DeviceProviderService; 20 import org.onlab.onos.net.device.DeviceProviderService;
18 import org.onlab.onos.net.device.DeviceService; 21 import org.onlab.onos.net.device.DeviceService;
22 +import org.onlab.onos.net.device.PortDescription;
19 import org.onlab.onos.net.provider.AbstractProvider; 23 import org.onlab.onos.net.provider.AbstractProvider;
20 import org.onlab.onos.net.provider.ProviderId; 24 import org.onlab.onos.net.provider.ProviderId;
21 25
...@@ -42,6 +46,10 @@ public class SimpleDeviceManagerTest { ...@@ -42,6 +46,10 @@ public class SimpleDeviceManagerTest {
42 private static final String SW2 = "3.9.5"; 46 private static final String SW2 = "3.9.5";
43 private static final String SN = "43311-12345"; 47 private static final String SN = "43311-12345";
44 48
49 + private static final PortNumber P1 = PortNumber.portNumber(1);
50 + private static final PortNumber P2 = PortNumber.portNumber(2);
51 + private static final PortNumber P3 = PortNumber.portNumber(3);
52 +
45 53
46 private SimpleDeviceManager mgr; 54 private SimpleDeviceManager mgr;
47 55
...@@ -95,6 +103,7 @@ public class SimpleDeviceManagerTest { ...@@ -95,6 +103,7 @@ public class SimpleDeviceManagerTest {
95 Iterator<Device> it = service.getDevices().iterator(); 103 Iterator<Device> it = service.getDevices().iterator();
96 assertNotNull("one device expected", it.next()); 104 assertNotNull("one device expected", it.next());
97 assertFalse("only one device expected", it.hasNext()); 105 assertFalse("only one device expected", it.hasNext());
106 + assertEquals("incorrect device count", 1, service.getDeviceCount());
98 } 107 }
99 108
100 @Test 109 @Test
...@@ -111,6 +120,8 @@ public class SimpleDeviceManagerTest { ...@@ -111,6 +120,8 @@ public class SimpleDeviceManagerTest {
111 // Reconnect 120 // Reconnect
112 connectDevice(DID1, SW1); 121 connectDevice(DID1, SW1);
113 validateEvents(DEVICE_AVAILABILITY_CHANGED); 122 validateEvents(DEVICE_AVAILABILITY_CHANGED);
123 +
124 + assertEquals("incorrect device count", 2, service.getDeviceCount());
114 } 125 }
115 126
116 @Test 127 @Test
...@@ -138,7 +149,64 @@ public class SimpleDeviceManagerTest { ...@@ -138,7 +149,64 @@ public class SimpleDeviceManagerTest {
138 assertEquals("incorrect role", MastershipRole.MASTER, provider.roleReceived); 149 assertEquals("incorrect role", MastershipRole.MASTER, provider.roleReceived);
139 } 150 }
140 151
152 + @Test
153 + public void updatePorts() {
154 + connectDevice(DID1, SW1);
155 + List<PortDescription> pds = new ArrayList<>();
156 + pds.add(new DefaultPortDescription(P1, true));
157 + pds.add(new DefaultPortDescription(P2, true));
158 + pds.add(new DefaultPortDescription(P3, true));
159 + providerService.updatePorts(DID1, pds);
160 + validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED, PORT_ADDED);
161 + pds.clear();
162 +
163 + pds.add(new DefaultPortDescription(P1, false));
164 + pds.add(new DefaultPortDescription(P3, true));
165 + providerService.updatePorts(DID1, pds);
166 + validateEvents(PORT_UPDATED, PORT_REMOVED);
167 + }
168 +
169 + @Test
170 + public void updatePortStatus() {
171 + connectDevice(DID1, SW1);
172 + List<PortDescription> pds = new ArrayList<>();
173 + pds.add(new DefaultPortDescription(P1, true));
174 + pds.add(new DefaultPortDescription(P2, true));
175 + providerService.updatePorts(DID1, pds);
176 + validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED);
177 +
178 + providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false));
179 + validateEvents(PORT_UPDATED);
180 + providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false));
181 + assertTrue("no events expected", listener.events.isEmpty());
182 + }
183 +
184 + @Test
185 + public void getPorts() {
186 + connectDevice(DID1, SW1);
187 + List<PortDescription> pds = new ArrayList<>();
188 + pds.add(new DefaultPortDescription(P1, true));
189 + pds.add(new DefaultPortDescription(P2, true));
190 + providerService.updatePorts(DID1, pds);
191 + validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED);
192 + assertEquals("wrong port count", 2, service.getPorts(DID1).size());
193 +
194 + Port port = service.getPort(DID1, P1);
195 + assertEquals("incorrect port", P1, service.getPort(DID1, P1).number());
196 + assertEquals("incorrect state", true, service.getPort(DID1, P1).isEnabled());
197 + }
141 198
199 + @Test
200 + public void removeDevice() {
201 + connectDevice(DID1, SW1);
202 + connectDevice(DID2, SW2);
203 + assertEquals("incorrect device count", 2, service.getDeviceCount());
204 + admin.removeDevice(DID1);
205 + assertNull("device should not be found", service.getDevice(DID1));
206 + assertNotNull("device should be found", service.getDevice(DID2));
207 + assertEquals("incorrect device count", 1, service.getDeviceCount());
208 +
209 + }
142 210
143 protected void validateEvents(Enum... types) { 211 protected void validateEvents(Enum... types) {
144 int i = 0; 212 int i = 0;
......