MastershipManager: make "useRegionForBalanceRoles" a property

useRegionForBalance roles has been available for a while but
seems to only be accessed directly (it's a public field). It'd
be useful to be able to change that using the CLI.

Change-Id: If8c6dfbb300513e4d8dc0ba3d0d8f6ce9eac036a
diff --git a/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java b/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java
index 1eba44f..ae9dc16 100644
--- a/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java
+++ b/core/net/src/main/java/org/onosproject/cluster/impl/MastershipManager.java
@@ -23,10 +23,14 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.metrics.MetricsService;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.cfg.ConfigProperty;
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.NodeId;
@@ -97,12 +101,22 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected RegionService regionService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService cfgService;
+
     private NodeId localNodeId;
     private Timer requestRoleTimer;
+
+    static final boolean DEFAULT_USE_REGION_FOR_BALANCE_ROLES = false;
+    @Property(name = "useRegionForBalanceRoles", boolValue = DEFAULT_USE_REGION_FOR_BALANCE_ROLES,
+              label = "Use Regions for balancing roles")
     public boolean useRegionForBalanceRoles;
 
     @Activate
     public void activate() {
+        cfgService.registerProperties(getClass());
+        modified();
+
         requestRoleTimer = createTimer("Mastership", "requestRole", "responseTime");
         localNodeId = clusterService.getLocalNode().id();
         eventDispatcher.addSink(MastershipEvent.class, listenerRegistry);
@@ -110,11 +124,22 @@
         log.info("Started");
     }
 
+    @Modified
+    public void modified() {
+        Set<ConfigProperty> configProperties = cfgService.getProperties(getClass().getCanonicalName());
+        for (ConfigProperty property : configProperties) {
+            if (property.name().equals("useRegionForBalanceRoles")) {
+                useRegionForBalanceRoles = property.asBoolean();
+            }
+        }
+    }
+
     @Deactivate
     public void deactivate() {
         eventDispatcher.removeSink(MastershipEvent.class);
         store.unsetDelegate(delegate);
         log.info("Stopped");
+        cfgService.unregisterProperties(getClass(), false);
     }
 
     @Override
diff --git a/core/net/src/test/java/org/onosproject/cluster/impl/MastershipManagerTest.java b/core/net/src/test/java/org/onosproject/cluster/impl/MastershipManagerTest.java
index 0e6ef61..ac2e558 100644
--- a/core/net/src/test/java/org/onosproject/cluster/impl/MastershipManagerTest.java
+++ b/core/net/src/test/java/org/onosproject/cluster/impl/MastershipManagerTest.java
@@ -21,11 +21,13 @@
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
+import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.onlab.junit.TestUtils;
 import org.onlab.packet.IpAddress;
+import org.onosproject.cfg.ComponentConfigService;
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.DefaultControllerNode;
@@ -47,6 +49,10 @@
 import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.Futures;
 
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.EasyMock.replay;
 import static org.junit.Assert.*;
 import static org.onosproject.net.MastershipRole.MASTER;
 import static org.onosproject.net.MastershipRole.NONE;
@@ -106,6 +112,18 @@
         TestUtils.setField(regionManager, "store", regionStore);
         regionManager.activate();
         mgr.regionService = regionManager;
+
+        ComponentConfigService mockConfigService =
+                EasyMock.createMock(ComponentConfigService.class);
+        expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of());
+        mockConfigService.registerProperties(mgr.getClass());
+        expectLastCall();
+        mockConfigService.unregisterProperties(mgr.getClass(), false);
+        expectLastCall();
+        expect(mockConfigService.getProperties(anyObject())).andReturn(ImmutableSet.of());
+        mgr.cfgService = mockConfigService;
+        replay(mockConfigService);
+
         mgr.activate();
     }