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
@Override
public final void disconnectSwitch() {
setConnected(false);
this.channel.close();
}
......
......@@ -27,6 +27,7 @@ import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.core.CoreService;
import org.onosproject.net.driver.DefaultDriverProviderService;
import org.onosproject.net.driver.DriverService;
import org.onosproject.openflow.controller.DefaultOpenFlowPacketContext;
......@@ -83,6 +84,7 @@ import static org.onlab.util.Tools.groupedThreads;
@Component(immediate = true)
@Service
public class OpenFlowControllerImpl implements OpenFlowController {
private static final String APP_ID = "org.onosproject.openflow-base";
private static final String DEFAULT_OFPORT = "6633,6653";
private static final int DEFAULT_WORKER_THREADS = 16;
......@@ -90,6 +92,9 @@ public class OpenFlowControllerImpl implements OpenFlowController {
LoggerFactory.getLogger(OpenFlowControllerImpl.class);
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected DriverService driverService;
// References exists merely for sequencing purpose to assure drivers are loaded
......@@ -147,15 +152,24 @@ public class OpenFlowControllerImpl implements OpenFlowController {
@Activate
public void activate(ComponentContext context) {
coreService.registerApplication(APP_ID, this::preDeactivate);
cfgService.registerProperties(getClass());
ctrl.setConfigParams(context.getProperties());
ctrl.start(agent, driverService);
}
private void preDeactivate() {
// Close listening channel and all OF channels before deactivating
ctrl.stop();
connectedSwitches.values().forEach(OpenFlowSwitch::disconnectSwitch);
}
@Deactivate
public void deactivate() {
cfgService.unregisterProperties(getClass(), false);
ctrl.stop();
connectedSwitches.clear();
activeMasterSwitches.clear();
activeEqualSwitches.clear();
}
@Modified
......
......@@ -32,6 +32,7 @@ import org.junit.Before;
import org.junit.Test;
import org.onlab.junit.TestTools;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.core.CoreService;
import org.onosproject.openflow.OpenflowSwitchDriverAdapter;
import org.onosproject.openflow.controller.Dpid;
import org.onosproject.openflow.controller.OpenFlowSwitch;
......@@ -127,6 +128,10 @@ public class OpenFlowControllerImplTest {
switchListener = new TestSwitchListener();
controller.addListener(switchListener);
CoreService mockCoreService =
EasyMock.createMock(CoreService.class);
controller.coreService = mockCoreService;
ComponentConfigService mockConfigService =
EasyMock.createMock(ComponentConfigService.class);
expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of());
......
......@@ -128,7 +128,8 @@ public class OpenFlowGroupProvider extends AbstractProvider implements GroupProv
public void deactivate() {
providerRegistry.unregister(this);
providerService = null;
collectors.values().forEach(GroupStatsCollector::stop);
collectors.clear();
log.info("Stopped");
}
......
......@@ -132,6 +132,8 @@ public class OpenFlowMeterProvider extends AbstractProvider implements MeterProv
@Deactivate
public void deactivate() {
providerRegistry.unregister(this);
collectors.values().forEach(MeterStatsCollector::stop);
collectors.clear();
controller.removeEventListener(listener);
controller.removeListener(listener);
providerService = null;
......