Charles Chan
Committed by Gerrit Code Review

ONOS-3411 Disconnect switches and stop stat collectors when openflow-base is being deactivated

Change-Id: I9a1b5fbb60a9e7135cd586fe717c18f83e3d864d
...@@ -98,6 +98,7 @@ public abstract class AbstractOpenFlowSwitch extends AbstractHandlerBehaviour ...@@ -98,6 +98,7 @@ public abstract class AbstractOpenFlowSwitch extends AbstractHandlerBehaviour
98 98
99 @Override 99 @Override
100 public final void disconnectSwitch() { 100 public final void disconnectSwitch() {
101 + setConnected(false);
101 this.channel.close(); 102 this.channel.close();
102 } 103 }
103 104
......
...@@ -27,6 +27,7 @@ import org.apache.felix.scr.annotations.Reference; ...@@ -27,6 +27,7 @@ import org.apache.felix.scr.annotations.Reference;
27 import org.apache.felix.scr.annotations.ReferenceCardinality; 27 import org.apache.felix.scr.annotations.ReferenceCardinality;
28 import org.apache.felix.scr.annotations.Service; 28 import org.apache.felix.scr.annotations.Service;
29 import org.onosproject.cfg.ComponentConfigService; 29 import org.onosproject.cfg.ComponentConfigService;
30 +import org.onosproject.core.CoreService;
30 import org.onosproject.net.driver.DefaultDriverProviderService; 31 import org.onosproject.net.driver.DefaultDriverProviderService;
31 import org.onosproject.net.driver.DriverService; 32 import org.onosproject.net.driver.DriverService;
32 import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext; 33 import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext;
...@@ -83,6 +84,7 @@ import static org.onlab.util.Tools.groupedThreads; ...@@ -83,6 +84,7 @@ import static org.onlab.util.Tools.groupedThreads;
83 @Component(immediate = true) 84 @Component(immediate = true)
84 @Service 85 @Service
85 public class OpenFlowControllerImpl implements OpenFlowController { 86 public class OpenFlowControllerImpl implements OpenFlowController {
87 + private static final String APP_ID = "org.onosproject.openflow-base";
86 private static final String DEFAULT_OFPORT = "6633,6653"; 88 private static final String DEFAULT_OFPORT = "6633,6653";
87 private static final int DEFAULT_WORKER_THREADS = 16; 89 private static final int DEFAULT_WORKER_THREADS = 16;
88 90
...@@ -90,6 +92,9 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -90,6 +92,9 @@ public class OpenFlowControllerImpl implements OpenFlowController {
90 LoggerFactory.getLogger(OpenFlowControllerImpl.class); 92 LoggerFactory.getLogger(OpenFlowControllerImpl.class);
91 93
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 + protected CoreService coreService;
96 +
97 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected DriverService driverService; 98 protected DriverService driverService;
94 99
95 // References exists merely for sequencing purpose to assure drivers are loaded 100 // References exists merely for sequencing purpose to assure drivers are loaded
...@@ -147,15 +152,24 @@ public class OpenFlowControllerImpl implements OpenFlowController { ...@@ -147,15 +152,24 @@ public class OpenFlowControllerImpl implements OpenFlowController {
147 152
148 @Activate 153 @Activate
149 public void activate(ComponentContext context) { 154 public void activate(ComponentContext context) {
155 + coreService.registerApplication(APP_ID, this::preDeactivate);
150 cfgService.registerProperties(getClass()); 156 cfgService.registerProperties(getClass());
151 ctrl.setConfigParams(context.getProperties()); 157 ctrl.setConfigParams(context.getProperties());
152 ctrl.start(agent, driverService); 158 ctrl.start(agent, driverService);
153 } 159 }
154 160
161 + private void preDeactivate() {
162 + // Close listening channel and all OF channels before deactivating
163 + ctrl.stop();
164 + connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
165 + }
166 +
155 @Deactivate 167 @Deactivate
156 public void deactivate() { 168 public void deactivate() {
157 cfgService.unregisterProperties(getClass(), false); 169 cfgService.unregisterProperties(getClass(), false);
158 - ctrl.stop(); 170 + connectedSwitches.clear();
171 + activeMasterSwitches.clear();
172 + activeEqualSwitches.clear();
159 } 173 }
160 174
161 @Modified 175 @Modified
......
...@@ -32,6 +32,7 @@ import org.junit.Before; ...@@ -32,6 +32,7 @@ import org.junit.Before;
32 import org.junit.Test; 32 import org.junit.Test;
33 import org.onlab.junit.TestTools; 33 import org.onlab.junit.TestTools;
34 import org.onosproject.cfg.ComponentConfigService; 34 import org.onosproject.cfg.ComponentConfigService;
35 +import org.onosproject.core.CoreService;
35 import org.onosproject.openflow.OpenflowSwitchDriverAdapter; 36 import org.onosproject.openflow.OpenflowSwitchDriverAdapter;
36 import org.onosproject.openflow.controller.Dpid; 37 import org.onosproject.openflow.controller.Dpid;
37 import org.onosproject.openflow.controller.OpenFlowSwitch; 38 import org.onosproject.openflow.controller.OpenFlowSwitch;
...@@ -127,6 +128,10 @@ public class OpenFlowControllerImplTest { ...@@ -127,6 +128,10 @@ public class OpenFlowControllerImplTest {
127 switchListener = new TestSwitchListener(); 128 switchListener = new TestSwitchListener();
128 controller.addListener(switchListener); 129 controller.addListener(switchListener);
129 130
131 + CoreService mockCoreService =
132 + EasyMock.createMock(CoreService.class);
133 + controller.coreService = mockCoreService;
134 +
130 ComponentConfigService mockConfigService = 135 ComponentConfigService mockConfigService =
131 EasyMock.createMock(ComponentConfigService.class); 136 EasyMock.createMock(ComponentConfigService.class);
132 expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of()); 137 expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of());
......
...@@ -128,7 +128,8 @@ public class OpenFlowGroupProvider extends AbstractProvider implements GroupProv ...@@ -128,7 +128,8 @@ public class OpenFlowGroupProvider extends AbstractProvider implements GroupProv
128 public void deactivate() { 128 public void deactivate() {
129 providerRegistry.unregister(this); 129 providerRegistry.unregister(this);
130 providerService = null; 130 providerService = null;
131 - 131 + collectors.values().forEach(GroupStatsCollector::stop);
132 + collectors.clear();
132 log.info("Stopped"); 133 log.info("Stopped");
133 } 134 }
134 135
......
...@@ -132,6 +132,8 @@ public class OpenFlowMeterProvider extends AbstractProvider implements MeterProv ...@@ -132,6 +132,8 @@ public class OpenFlowMeterProvider extends AbstractProvider implements MeterProv
132 @Deactivate 132 @Deactivate
133 public void deactivate() { 133 public void deactivate() {
134 providerRegistry.unregister(this); 134 providerRegistry.unregister(this);
135 + collectors.values().forEach(MeterStatsCollector::stop);
136 + collectors.clear();
135 controller.removeEventListener(listener); 137 controller.removeEventListener(listener);
136 controller.removeListener(listener); 138 controller.removeListener(listener);
137 providerService = null; 139 providerService = null;
......