Introducing concept of a physical or logical region to facilitate
support of geographically distributed cluster and to lay ground
for multiple/filtered topology layouts.

Added implementation of manager and store; unit-tests included.

Change-Id: Ia01673a0b711b8785c0ea68768552c2f61d7ea6d
diff --git a/core/net/src/main/java/org/onosproject/net/region/impl/RegionManager.java b/core/net/src/main/java/org/onosproject/net/region/impl/RegionManager.java
new file mode 100644
index 0000000..fcb1d6a
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/region/impl/RegionManager.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.region.impl;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.event.AbstractListenerManager;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.region.Region;
+import org.onosproject.net.region.RegionAdminService;
+import org.onosproject.net.region.RegionEvent;
+import org.onosproject.net.region.RegionId;
+import org.onosproject.net.region.RegionListener;
+import org.onosproject.net.region.RegionService;
+import org.onosproject.net.region.RegionStore;
+import org.onosproject.net.region.RegionStoreDelegate;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.collect.ImmutableList.of;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides implementation of the region service APIs.
+ */
+public class RegionManager extends AbstractListenerManager<RegionEvent, RegionListener>
+        implements RegionAdminService, RegionService {
+
+    private static final String REGION_ID_NULL = "Region ID cannot be null";
+    private static final String REGION_TYPE_NULL = "Region type cannot be null";
+    private static final String DEVICE_ID_NULL = "Device ID cannot be null";
+    private static final String DEVICE_IDS_NULL = "Device IDs cannot be null";
+    private static final String DEVICE_IDS_EMPTY = "Device IDs cannot be empty";
+    private static final String NAME_NULL = "Name cannot be null";
+
+    private final Logger log = getLogger(getClass());
+
+    private RegionStoreDelegate delegate = this::post;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected RegionStore store;
+
+    @Activate
+    public void activate() {
+        store.setDelegate(delegate);
+        eventDispatcher.addSink(RegionEvent.class, listenerRegistry);
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        store.unsetDelegate(delegate);
+        eventDispatcher.removeSink(RegionEvent.class);
+        log.info("Stopped");
+    }
+
+    @Override
+    public Region createRegion(RegionId regionId, String name, Region.Type type,
+                               List<Set<NodeId>> masterNodeIds) {
+        checkNotNull(regionId, REGION_ID_NULL);
+        checkNotNull(name, NAME_NULL);
+        checkNotNull(name, REGION_TYPE_NULL);
+        return store.createRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
+    }
+
+    @Override
+    public Region updateRegion(RegionId regionId, String name, Region.Type type,
+                               List<Set<NodeId>> masterNodeIds) {
+        checkNotNull(regionId, REGION_ID_NULL);
+        checkNotNull(name, NAME_NULL);
+        checkNotNull(name, REGION_TYPE_NULL);
+        return store.updateRegion(regionId, name, type, masterNodeIds == null ? of() : masterNodeIds);
+    }
+
+    @Override
+    public void removeRegion(RegionId regionId) {
+        checkNotNull(regionId, REGION_ID_NULL);
+        store.removeRegion(regionId);
+    }
+
+    @Override
+    public void addDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
+        checkNotNull(regionId, REGION_ID_NULL);
+        checkNotNull(deviceIds, DEVICE_IDS_NULL);
+        checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
+        store.addDevices(regionId, deviceIds);
+    }
+
+    @Override
+    public void removeDevices(RegionId regionId, Collection<DeviceId> deviceIds) {
+        checkNotNull(regionId, REGION_ID_NULL);
+        checkNotNull(deviceIds, DEVICE_IDS_NULL);
+        checkState(!deviceIds.isEmpty(), DEVICE_IDS_EMPTY);
+        store.removeDevices(regionId, deviceIds);
+    }
+
+    @Override
+    public Set<Region> getRegions() {
+        return store.getRegions();
+    }
+
+    @Override
+    public Region getRegion(RegionId regionId) {
+        checkNotNull(regionId, REGION_ID_NULL);
+        return store.getRegion(regionId);
+    }
+
+    @Override
+    public Region getRegionForDevice(DeviceId deviceId) {
+        checkNotNull(deviceId, DEVICE_ID_NULL);
+        return store.getRegionForDevice(deviceId);
+    }
+
+    @Override
+    public Set<DeviceId> getRegionDevices(RegionId regionId) {
+        checkNotNull(regionId, REGION_ID_NULL);
+        return store.getRegionDevices(regionId);
+    }
+
+}
diff --git a/core/net/src/main/java/org/onosproject/net/region/impl/package-info.java b/core/net/src/main/java/org/onosproject/net/region/impl/package-info.java
new file mode 100644
index 0000000..ce41213
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/region/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Core subsystem for managing region definitions.
+ */
+package org.onosproject.net.region.impl;
\ No newline at end of file
diff --git a/core/net/src/test/java/org/onosproject/net/region/impl/RegionManagerTest.java b/core/net/src/test/java/org/onosproject/net/region/impl/RegionManagerTest.java
new file mode 100644
index 0000000..01c5911
--- /dev/null
+++ b/core/net/src/test/java/org/onosproject/net/region/impl/RegionManagerTest.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.region.impl;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onlab.util.ItemNotFoundException;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.common.event.impl.TestEventDispatcher;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.NetTestTools;
+import org.onosproject.net.region.Region;
+import org.onosproject.net.region.RegionAdminService;
+import org.onosproject.net.region.RegionEvent;
+import org.onosproject.net.region.RegionId;
+import org.onosproject.net.region.RegionListener;
+import org.onosproject.store.region.impl.DistributedRegionStore;
+import org.onosproject.store.service.TestStorageService;
+
+import java.util.List;
+import java.util.Set;
+
+import static org.junit.Assert.*;
+import static org.onosproject.net.region.Region.Type.*;
+import static org.onosproject.net.region.RegionEvent.Type.*;
+
+/**
+ * Tests of the region service implementation.
+ */
+public class RegionManagerTest {
+
+    private static final RegionId RID1 = RegionId.regionId("r1");
+    private static final RegionId RID2 = RegionId.regionId("r2");
+
+    private static final DeviceId DID1 = DeviceId.deviceId("foo:d1");
+    private static final DeviceId DID2 = DeviceId.deviceId("foo:d2");
+    private static final DeviceId DID3 = DeviceId.deviceId("foo:d3");
+
+    private static final NodeId NID1 = NodeId.nodeId("n1");
+
+    private static final List<Set<NodeId>> MASTERS = ImmutableList.of(ImmutableSet.of(NID1));
+
+    private TestManager manager = new TestManager();
+    private RegionAdminService service;
+    private TestStore store = new TestStore();
+    private TestListener listener = new TestListener();
+
+    @Before
+    public void setUp() throws Exception {
+        TestUtils.setField(store, "storageService", new TestStorageService());
+        store.activate();
+
+        manager.store = store;
+        manager.addListener(listener);
+        NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
+        manager.activate();
+        service = manager;
+    }
+
+    @After
+    public void tearDown() {
+        store.deactivate();
+        manager.removeListener(listener);
+        manager.deactivate();
+        NetTestTools.injectEventDispatcher(manager, null);
+    }
+
+    @Test
+    public void basics() {
+        Region r1 = service.createRegion(RID1, "R1", METRO, MASTERS);
+        assertEquals("incorrect id", RID1, r1.id());
+        assertEquals("incorrect event", REGION_ADDED, listener.event.type());
+
+        Region r2 = service.createRegion(RID2, "R2", CAMPUS, MASTERS);
+        assertEquals("incorrect id", RID2, r2.id());
+        assertEquals("incorrect type", CAMPUS, r2.type());
+        assertEquals("incorrect event", REGION_ADDED, listener.event.type());
+
+        r2 = service.updateRegion(RID2, "R2", COUNTRY, MASTERS);
+        assertEquals("incorrect type", COUNTRY, r2.type());
+        assertEquals("incorrect event", REGION_UPDATED, listener.event.type());
+
+        Set<Region> regions = service.getRegions();
+        assertEquals("incorrect size", 2, regions.size());
+        assertTrue("missing r1", regions.contains(r1));
+        assertTrue("missing r2", regions.contains(r2));
+
+        r1 = service.getRegion(RID1);
+        assertEquals("incorrect id", RID1, r1.id());
+
+        service.removeRegion(RID1);
+        regions = service.getRegions();
+        assertEquals("incorrect size", 1, regions.size());
+        assertTrue("missing r2", regions.contains(r2));
+        assertEquals("incorrect event", REGION_REMOVED, listener.event.type());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void duplicateCreate() {
+        service.createRegion(RID1, "R1", METRO, MASTERS);
+        service.createRegion(RID1, "R2", CAMPUS, MASTERS);
+    }
+
+    @Test(expected = ItemNotFoundException.class)
+    public void missingUpdate() {
+        service.updateRegion(RID1, "R1", METRO, MASTERS);
+    }
+
+    @Test
+    public void membership() {
+        Region r = service.createRegion(RID1, "R1", METRO, MASTERS);
+        assertTrue("no devices expected", service.getRegionDevices(RID1).isEmpty());
+        assertNull("no region expected", service.getRegionForDevice(DID1));
+
+        service.addDevices(RID1, ImmutableSet.of(DID1, DID2));
+        Set<DeviceId> deviceIds = service.getRegionDevices(RID1);
+        assertEquals("incorrect device count", 2, deviceIds.size());
+        assertTrue("missing d1", deviceIds.contains(DID1));
+        assertTrue("missing d2", deviceIds.contains(DID2));
+        assertEquals("wrong region", r, service.getRegionForDevice(DID1));
+        assertEquals("incorrect event", REGION_MEMBERSHIP_CHANGED, listener.event.type());
+
+        service.addDevices(RID1, ImmutableSet.of(DID3));
+        deviceIds = service.getRegionDevices(RID1);
+        assertEquals("incorrect device count", 3, deviceIds.size());
+        assertTrue("missing d3", deviceIds.contains(DID3));
+        assertEquals("incorrect event", REGION_MEMBERSHIP_CHANGED, listener.event.type());
+
+        service.addDevices(RID1, ImmutableSet.of(DID3, DID1));
+        deviceIds = service.getRegionDevices(RID1);
+        assertEquals("incorrect device count", 3, deviceIds.size());
+
+        service.removeDevices(RID1, ImmutableSet.of(DID2, DID3));
+        deviceIds = service.getRegionDevices(RID1);
+        assertEquals("incorrect device count", 1, deviceIds.size());
+        assertTrue("missing d1", deviceIds.contains(DID1));
+
+        service.removeDevices(RID1, ImmutableSet.of(DID1, DID3));
+        assertTrue("no devices expected", service.getRegionDevices(RID1).isEmpty());
+
+        service.removeDevices(RID1, ImmutableSet.of(DID2));
+        assertTrue("no devices expected", service.getRegionDevices(RID1).isEmpty());
+    }
+
+    private class TestStore extends DistributedRegionStore {
+        @Override
+        protected void activate() {
+            super.activate();
+        }
+
+        @Override
+        protected void deactivate() {
+            super.deactivate();
+        }
+    }
+
+    private class TestManager extends RegionManager {
+        TestManager() {
+            eventDispatcher = new TestEventDispatcher();
+        }
+    }
+
+    private class TestListener implements RegionListener {
+        RegionEvent event;
+
+        @Override
+        public void event(RegionEvent event) {
+            this.event = event;
+        }
+    }
+}
\ No newline at end of file