Handle packet Customization config changes

Change-Id: Ia0bd08fb758c8cb2665e9fb4c2c6c9f22dd75387
diff --git a/src/main/java/org/opencord/aaa/AaaManager.java b/src/main/java/org/opencord/aaa/AaaManager.java
index 9ac7ffb..80d380f 100755
--- a/src/main/java/org/opencord/aaa/AaaManager.java
+++ b/src/main/java/org/opencord/aaa/AaaManager.java
@@ -66,7 +66,8 @@
  * AAA application for ONOS.
  */
 @Component(immediate = true)
-public class AaaManager {
+public class
+AaaManager {
     private static final String APP_NAME = "org.opencord.aaa";
 
     // for verbose output
@@ -200,33 +201,13 @@
     public void activate() {
         netCfgService.registerConfigFactory(factory);
         appId = coreService.registerApplication(APP_NAME);
-
+        customInfo = new CustomizationInfo(subsService, deviceService);
         cfgListener.reconfigureNetwork(netCfgService.getConfig(appId, AaaConfig.class));
+        configureRadiusCommunication();
 
         // register our event handler
         packetService.addProcessor(processor, PacketProcessor.director(2));
 
-        customInfo = new CustomizationInfo(subsService, deviceService);
-
-        switch (customizer.toLowerCase()) {
-            case "sample":
-                pktCustomizer = new SamplePacketCustomizer(customInfo);
-                log.info("Created SamplePacketCustomizer");
-                break;
-            default:
-                pktCustomizer = new PacketCustomizer(customInfo);
-                log.info("Created default PacketCustomizer");
-                break;
-        }
-
-        if (radiusConnectionType.toLowerCase().equals("socket")) {
-            impl = new SocketBasedRadiusCommunicator(appId, packetService, this);
-        } else {
-            impl = new PortBasedRadiusCommunicator(appId, packetService,
-                                                   mastershipService,
-                                                   deviceService, subsService,
-                                                   pktCustomizer, this);
-        }
 
         StateMachine.initializeMaps();
         // FIXME: can't depend on AccessDeviceService
@@ -242,20 +223,42 @@
         log.info("Started");
     }
 
+
     @Deactivate
     public void deactivate() {
         impl.withdrawIntercepts();
         // de-register and null our handler
         packetService.removeProcessor(processor);
         processor = null;
-        StateMachine.destroyMaps();
         netCfgService.removeListener(cfgListener);
+        StateMachine.destroyMaps();
         impl.deactivate();
         deviceService.removeListener(deviceListener);
-
         log.info("Stopped");
     }
 
+    private void configureRadiusCommunication() {
+        if (radiusConnectionType.toLowerCase().equals("socket")) {
+            impl = new SocketBasedRadiusCommunicator(appId, packetService, this);
+        } else {
+            impl = new PortBasedRadiusCommunicator(appId, packetService, mastershipService,
+                                                   deviceService, subsService, pktCustomizer, this);
+        }
+    }
+
+    private void configurePacketCustomizer() {
+        switch (customizer.toLowerCase()) {
+            case "sample":
+                pktCustomizer = new SamplePacketCustomizer(customInfo);
+                log.info("Created SamplePacketCustomizer");
+                break;
+            default:
+                pktCustomizer = new PacketCustomizer(customInfo);
+                log.info("Created default PacketCustomizer");
+                break;
+        }
+    }
+
     /**
      * Send RADIUS packet to the RADIUS server.
      *
@@ -596,10 +599,25 @@
                 radiusSecret = newCfg.radiusSecret();
             }
 
-            radiusConnectionType = newCfg.radiusConnectionType();
-            customizer = newCfg.radiusPktCustomizer();
+            boolean reconfigureCustomizer = false;
+            if (customizer == null || !customizer.equals(newCfg.radiusPktCustomizer())) {
+                customizer = newCfg.radiusPktCustomizer();
+                configurePacketCustomizer();
+                reconfigureCustomizer = true;
+            }
 
-            if (impl != null) {
+            if (radiusConnectionType == null
+                    || reconfigureCustomizer
+                    || !radiusConnectionType.equals(newCfg.radiusConnectionType())) {
+                radiusConnectionType = newCfg.radiusConnectionType();
+                if (impl != null) {
+                    impl.withdrawIntercepts();
+                    impl.clearLocalState();
+                }
+                configureRadiusCommunication();
+                impl.initializeLocalState(newCfg);
+                impl.requestIntercepts();
+            } else if (impl != null) {
                 impl.clearLocalState();
                 impl.initializeLocalState(newCfg);
             }
diff --git a/src/main/java/org/opencord/aaa/PortBasedRadiusCommunicator.java b/src/main/java/org/opencord/aaa/PortBasedRadiusCommunicator.java
index 8e93777..cf53b68 100755
--- a/src/main/java/org/opencord/aaa/PortBasedRadiusCommunicator.java
+++ b/src/main/java/org/opencord/aaa/PortBasedRadiusCommunicator.java
@@ -180,7 +180,10 @@
     }
 
     @Override
-    public void clearLocalState() {}
+    public void clearLocalState() {
+        mastershipService.removeListener(changeListener);
+        deviceService.removeListener(deviceListener);
+    }
 
     @Override
     public void deactivate() {
@@ -199,6 +202,10 @@
                 .matchIPProtocol(IPv4.PROTOCOL_UDP)
                 .matchUdpSrc(TpPort.tpPort(radiusServerPort));
         packetService.requestPackets(selectorServer.build(), CONTROL, appId);
+
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+               selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
+               packetService.requestPackets(selector.build(), CONTROL, appId);
     }
 
     @Override
@@ -212,6 +219,10 @@
                 .matchIPProtocol(IPv4.PROTOCOL_UDP)
                 .matchUdpSrc(TpPort.tpPort(radiusServerPort));
         packetService.cancelPackets(selectorServer.build(), CONTROL, appId);
+
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+        selector.matchEthType(EthType.EtherType.EAPOL.ethType().toShort());
+        packetService.cancelPackets(selector.build(), CONTROL, appId);
     }
 
     @Override