[CORD-200] Improves Segment Routing using Network Configuration Subsystem

The missing part of gerrit #5533

Change-Id: I0830aa80f9be0e9933a99e0c12518aef67008a53
diff --git a/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index 764bed3..0566312 100644
--- a/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -141,7 +141,9 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigRegistry cfgService;
 
-    private final InternalConfigListener cfgListener = new InternalConfigListener();
+    private final InternalConfigListener cfgListener =
+            new InternalConfigListener(this);
+
     private final ConfigFactory cfgFactory =
             new ConfigFactory(SubjectFactories.DEVICE_SUBJECT_FACTORY,
                               SegmentRoutingConfig.class,
@@ -211,40 +213,8 @@
 
         cfgService.addListener(cfgListener);
         cfgService.registerConfigFactory(cfgFactory);
-        deviceConfiguration = new DeviceConfiguration(cfgService);
 
-        arpHandler = new ArpHandler(this);
-        icmpHandler = new IcmpHandler(this);
-        ipHandler = new IpHandler(this);
-        routingRulePopulator = new RoutingRulePopulator(this);
-        defaultRoutingHandler = new DefaultRoutingHandler(this);
-        tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
-                groupHandlerMap, tunnelStore);
-        policyHandler = new PolicyHandler(appId, deviceConfiguration,
-                flowObjectiveService, tunnelHandler, policyStore);
-
-        packetService.addProcessor(processor, PacketProcessor.director(2));
-        linkService.addListener(new InternalLinkListener());
-        deviceService.addListener(new InternalDeviceListener());
-
-        for (Device device : deviceService.getDevices()) {
-            //Irrespective whether the local is a MASTER or not for this device,
-            //create group handler instance and push default TTP flow rules.
-            //Because in a multi-instance setup, instances can initiate
-            //groups for any devices. Also the default TTP rules are needed
-            //to be pushed before inserting any IP table entries for any device
-            DefaultGroupHandler groupHandler = DefaultGroupHandler
-                    .createGroupHandler(device.id(), appId,
-                                        deviceConfiguration, linkService,
-                                        flowObjectiveService,
-                                        nsNextObjStore);
-            groupHandlerMap.put(device.id(), groupHandler);
-            defaultRoutingHandler.populateTtpRules(device.id());
-        }
-
-        defaultRoutingHandler.startPopulationProcess();
         log.info("Started");
-
     }
 
     @Deactivate
@@ -535,12 +505,56 @@
     }
 
     private class InternalConfigListener implements NetworkConfigListener {
+        SegmentRoutingManager segmentRoutingManager;
+
+        public InternalConfigListener(SegmentRoutingManager srMgr) {
+            this.segmentRoutingManager = srMgr;
+        }
+
+        public void configureNetwork() {
+            deviceConfiguration = new DeviceConfiguration(segmentRoutingManager.cfgService);
+
+            arpHandler = new ArpHandler(segmentRoutingManager);
+            icmpHandler = new IcmpHandler(segmentRoutingManager);
+            ipHandler = new IpHandler(segmentRoutingManager);
+            routingRulePopulator = new RoutingRulePopulator(segmentRoutingManager);
+            defaultRoutingHandler = new DefaultRoutingHandler(segmentRoutingManager);
+
+            tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
+                                              groupHandlerMap, tunnelStore);
+            policyHandler = new PolicyHandler(appId, deviceConfiguration,
+                                              flowObjectiveService,
+                                              tunnelHandler, policyStore);
+
+            packetService.addProcessor(processor, PacketProcessor.director(2));
+            linkService.addListener(new InternalLinkListener());
+            deviceService.addListener(new InternalDeviceListener());
+
+            for (Device device : deviceService.getDevices()) {
+                //Irrespective whether the local is a MASTER or not for this device,
+                //create group handler instance and push default TTP flow rules.
+                //Because in a multi-instance setup, instances can initiate
+                //groups for any devices. Also the default TTP rules are needed
+                //to be pushed before inserting any IP table entries for any device
+                DefaultGroupHandler groupHandler = DefaultGroupHandler
+                        .createGroupHandler(device.id(), appId,
+                                            deviceConfiguration, linkService,
+                                            flowObjectiveService,
+                                            nsNextObjStore);
+                groupHandlerMap.put(device.id(), groupHandler);
+                defaultRoutingHandler.populateTtpRules(device.id());
+            }
+
+            defaultRoutingHandler.startPopulationProcess();
+        }
+
         @Override
         public void event(NetworkConfigEvent event) {
             if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
                     event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
                     event.configClass().equals(SegmentRoutingConfig.class)) {
-                // TODO Support dynamic configuration in the future
+                log.info("Network configuration change detected. (Re)Configuring...");
+                configureNetwork();
                 return;
             }
         }