Andrea Campanella
Committed by Gerrit Code Review

Removing Rest and Netconf devices when the providers are disabled

Change-Id: Icac7146fea1295c11972ae4cbf87f8ef9689c671
...@@ -20,6 +20,7 @@ import org.onlab.packet.IpAddress; ...@@ -20,6 +20,7 @@ import org.onlab.packet.IpAddress;
20 import org.onosproject.net.DeviceId; 20 import org.onosproject.net.DeviceId;
21 21
22 import java.util.Map; 22 import java.util.Map;
23 +import java.util.Set;
23 24
24 /** 25 /**
25 * Abstraction of an NETCONF controller. Serves as a one stop shop for obtaining 26 * Abstraction of an NETCONF controller. Serves as a one stop shop for obtaining
...@@ -52,7 +53,14 @@ public interface NetconfController { ...@@ -52,7 +53,14 @@ public interface NetconfController {
52 NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException; 53 NetconfDevice connectDevice(NetconfDeviceInfo deviceInfo) throws NetconfException;
53 54
54 /** 55 /**
55 - * Removes a Netconf device. 56 + * Disconnects a Netconf device and removes it from the core.
57 + *
58 + * @param deviceInfo info about the device to remove
59 + */
60 + void disconnectDevice(NetconfDeviceInfo deviceInfo);
61 +
62 + /**
63 + * Removes a Netconf device from the core.
56 * 64 *
57 * @param deviceInfo info about the device to remove 65 * @param deviceInfo info about the device to remove
58 */ 66 */
...@@ -62,10 +70,18 @@ public interface NetconfController { ...@@ -62,10 +70,18 @@ public interface NetconfController {
62 * Gets all the nodes information. 70 * Gets all the nodes information.
63 * 71 *
64 * @return map of devices 72 * @return map of devices
73 + *
65 */ 74 */
66 Map<DeviceId, NetconfDevice> getDevicesMap(); 75 Map<DeviceId, NetconfDevice> getDevicesMap();
67 76
68 /** 77 /**
78 + * Gets all Netconf Devices.
79 + *
80 + * @return List of all the NetconfDevices Ids
81 + */
82 + Set<DeviceId> getNetconfDevices();
83 +
84 + /**
69 * Gets a Netconf Device by node identifier. 85 * Gets a Netconf Device by node identifier.
70 * 86 *
71 * @param deviceInfo node identifier 87 * @param deviceInfo node identifier
......
...@@ -108,7 +108,7 @@ public class NetconfControllerImpl implements NetconfController { ...@@ -108,7 +108,7 @@ public class NetconfControllerImpl implements NetconfController {
108 } 108 }
109 109
110 @Override 110 @Override
111 - public void removeDevice(NetconfDeviceInfo deviceInfo) { 111 + public void disconnectDevice(NetconfDeviceInfo deviceInfo) {
112 if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) { 112 if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
113 log.warn("Device {} is not present", deviceInfo); 113 log.warn("Device {} is not present", deviceInfo);
114 } else { 114 } else {
...@@ -116,6 +116,18 @@ public class NetconfControllerImpl implements NetconfController { ...@@ -116,6 +116,18 @@ public class NetconfControllerImpl implements NetconfController {
116 } 116 }
117 } 117 }
118 118
119 + @Override
120 + public void removeDevice(NetconfDeviceInfo deviceInfo) {
121 + if (!netconfDeviceMap.containsKey(deviceInfo.getDeviceId())) {
122 + log.warn("Device {} is not present", deviceInfo);
123 + } else {
124 + netconfDeviceMap.remove(deviceInfo.getDeviceId());
125 + for (NetconfDeviceListener l : netconfDeviceListeners) {
126 + l.deviceRemoved(deviceInfo);
127 + }
128 + }
129 + }
130 +
119 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { 131 private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
120 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo); 132 NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo);
121 for (NetconfDeviceListener l : netconfDeviceListeners) { 133 for (NetconfDeviceListener l : netconfDeviceListeners) {
...@@ -138,6 +150,11 @@ public class NetconfControllerImpl implements NetconfController { ...@@ -138,6 +150,11 @@ public class NetconfControllerImpl implements NetconfController {
138 return netconfDeviceMap; 150 return netconfDeviceMap;
139 } 151 }
140 152
153 + @Override
154 + public Set<DeviceId> getNetconfDevices() {
155 + return netconfDeviceMap.keySet();
156 + }
157 +
141 //Device factory for the specific NetconfDeviceImpl 158 //Device factory for the specific NetconfDeviceImpl
142 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { 159 private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory {
143 160
......
...@@ -182,6 +182,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler ...@@ -182,6 +182,7 @@ public class NetconfStreamThread extends Thread implements NetconfStreamHandler
182 null, null, Optional.of(-1), netconfDeviceInfo); 182 null, null, Optional.of(-1), netconfDeviceInfo);
183 netconfDeviceEventListeners.forEach( 183 netconfDeviceEventListeners.forEach(
184 listener -> listener.event(event)); 184 listener -> listener.event(event));
185 + this.interrupt();
185 } else { 186 } else {
186 deviceReply = deviceReply.replace(END_PATTERN, ""); 187 deviceReply = deviceReply.replace(END_PATTERN, "");
187 if (deviceReply.contains(RPC_REPLY) || 188 if (deviceReply.contains(RPC_REPLY) ||
......
...@@ -32,8 +32,10 @@ import org.onosproject.netconf.NetconfException; ...@@ -32,8 +32,10 @@ import org.onosproject.netconf.NetconfException;
32 import org.onosproject.netconf.NetconfSession; 32 import org.onosproject.netconf.NetconfSession;
33 33
34 import java.lang.reflect.Field; 34 import java.lang.reflect.Field;
35 +import java.util.HashSet;
35 import java.util.Map; 36 import java.util.Map;
36 import java.util.Optional; 37 import java.util.Optional;
38 +import java.util.Set;
37 39
38 import static org.hamcrest.Matchers.*; 40 import static org.hamcrest.Matchers.*;
39 import static org.junit.Assert.*; 41 import static org.junit.Assert.*;
...@@ -134,6 +136,14 @@ public class NetconfControllerImplTest { ...@@ -134,6 +136,14 @@ public class NetconfControllerImplTest {
134 } 136 }
135 137
136 @Test 138 @Test
139 + public void testGetNetconfDevices() {
140 + Set<DeviceId> devices = new HashSet<>();
141 + devices.add(deviceId1);
142 + devices.add(deviceId2);
143 + assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
144 + }
145 +
146 + @Test
137 public void testGetNetconfDevice() { 147 public void testGetNetconfDevice() {
138 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1); 148 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
139 assertThat("Incorrect device fetched", fetchedDevice1, is(device1)); 149 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
...@@ -192,7 +202,16 @@ public class NetconfControllerImplTest { ...@@ -192,7 +202,16 @@ public class NetconfControllerImplTest {
192 } 202 }
193 203
194 /** 204 /**
195 - * Check for removeDevice exception. 205 + * Check that disconnectDevice actually disconnects the device and removes it.
206 + */
207 + @Test
208 + public void testDisconnectDevice() throws Exception {
209 + ctrl.disconnectDevice(deviceInfo1);
210 + assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
211 + }
212 +
213 + /**
214 + * Checks that disconnectDevice actually disconnects the device and removes it.
196 */ 215 */
197 @Test 216 @Test
198 public void testRemoveDevice() throws Exception { 217 public void testRemoveDevice() throws Exception {
......
...@@ -62,9 +62,9 @@ public interface RestSBController { ...@@ -62,9 +62,9 @@ public interface RestSBController {
62 /** 62 /**
63 * Removes the device from the devices map. 63 * Removes the device from the devices map.
64 * 64 *
65 - * @param device to be removed 65 + * @param deviceId to be removed
66 */ 66 */
67 - void removeDevice(RestSBDevice device); 67 + void removeDevice(DeviceId deviceId);
68 68
69 /** 69 /**
70 * Does a REST POST request with specified parameters to the device. 70 * Does a REST POST request with specified parameters to the device.
......
...@@ -109,8 +109,8 @@ public class RestSBControllerImpl implements RestSBController { ...@@ -109,8 +109,8 @@ public class RestSBControllerImpl implements RestSBController {
109 } 109 }
110 110
111 @Override 111 @Override
112 - public void removeDevice(RestSBDevice device) { 112 + public void removeDevice(DeviceId deviceId) {
113 - deviceMap.remove(device.deviceId()); 113 + deviceMap.remove(deviceId);
114 } 114 }
115 115
116 @Override 116 @Override
......
...@@ -53,7 +53,7 @@ public class RestSBControllerImplTest { ...@@ -53,7 +53,7 @@ public class RestSBControllerImplTest {
53 assertEquals("Incorrect Get Device by IP, Port", controller.getDevice(device1.ip(), device1.port()), device1); 53 assertEquals("Incorrect Get Device by IP, Port", controller.getDevice(device1.ip(), device1.port()), device1);
54 controller.addDevice(device2); 54 controller.addDevice(device2);
55 assertTrue("Device2 non added", controller.getDevices().containsValue(device2)); 55 assertTrue("Device2 non added", controller.getDevices().containsValue(device2));
56 - controller.removeDevice(device2); 56 + controller.removeDevice(device2.deviceId());
57 assertFalse("Device2 not removed", controller.getDevices().containsValue(device2)); 57 assertFalse("Device2 not removed", controller.getDevices().containsValue(device2));
58 } 58 }
59 } 59 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -126,10 +126,13 @@ public class NetconfDeviceProvider extends AbstractProvider ...@@ -126,10 +126,13 @@ public class NetconfDeviceProvider extends AbstractProvider
126 126
127 @Deactivate 127 @Deactivate
128 public void deactivate() { 128 public void deactivate() {
129 + controller.removeDeviceListener(innerNodeListener);
130 + controller.getNetconfDevices().forEach(id ->
131 + controller.removeDevice(controller.getDevicesMap().get(id)
132 + .getDeviceInfo()));
129 providerRegistry.unregister(this); 133 providerRegistry.unregister(this);
130 providerService = null; 134 providerService = null;
131 cfgService.unregisterConfigFactory(factory); 135 cfgService.unregisterConfigFactory(factory);
132 - controller.removeDeviceListener(innerNodeListener);
133 log.info("Stopped"); 136 log.info("Stopped");
134 } 137 }
135 138
......
...@@ -135,13 +135,13 @@ public class RestDeviceProvider extends AbstractProvider ...@@ -135,13 +135,13 @@ public class RestDeviceProvider extends AbstractProvider
135 log.info("Started"); 135 log.info("Started");
136 } 136 }
137 137
138 -
139 @Deactivate 138 @Deactivate
140 public void deactivate() { 139 public void deactivate() {
140 + cfgService.removeListener(cfgLister);
141 + controller.getDevices().keySet().forEach(this::deviceRemoved);
141 providerRegistry.unregister(this); 142 providerRegistry.unregister(this);
142 providerService = null; 143 providerService = null;
143 cfgService.unregisterConfigFactory(factory); 144 cfgService.unregisterConfigFactory(factory);
144 - cfgService.removeListener(cfgLister);
145 log.info("Stopped"); 145 log.info("Stopped");
146 } 146 }
147 147
...@@ -195,12 +195,10 @@ public class RestDeviceProvider extends AbstractProvider ...@@ -195,12 +195,10 @@ public class RestDeviceProvider extends AbstractProvider
195 addedDevices.add(deviceId); 195 addedDevices.add(deviceId);
196 } 196 }
197 197
198 - //when do I call it ? 198 + private void deviceRemoved(DeviceId deviceId) {
199 - public void deviceRemoved(RestSBDevice nodeId) { 199 + Preconditions.checkNotNull(deviceId, ISNOTNULL);
200 - Preconditions.checkNotNull(nodeId, ISNOTNULL);
201 - DeviceId deviceId = nodeId.deviceId();
202 providerService.deviceDisconnected(deviceId); 200 providerService.deviceDisconnected(deviceId);
203 - controller.removeDevice(nodeId); 201 + controller.removeDevice(deviceId);
204 } 202 }
205 203
206 private void connectDevices() { 204 private void connectDevices() {
...@@ -217,7 +215,7 @@ public class RestDeviceProvider extends AbstractProvider ...@@ -217,7 +215,7 @@ public class RestDeviceProvider extends AbstractProvider
217 deviceAdded(device); 215 deviceAdded(device);
218 }); 216 });
219 //Removing devices not wanted anymore 217 //Removing devices not wanted anymore
220 - toBeRemoved.stream().forEach(device -> deviceRemoved(device)); 218 + toBeRemoved.stream().forEach(device -> deviceRemoved(device.deviceId()));
221 219
222 } 220 }
223 } catch (ConfigException e) { 221 } catch (ConfigException e) {
......