Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.store.region.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.util.ItemNotFoundException; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.region.Region; |
| 28 | import org.onosproject.net.region.RegionEvent; |
| 29 | import org.onosproject.net.region.RegionId; |
| 30 | import org.onosproject.store.service.TestStorageService; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.Set; |
| 34 | |
| 35 | import static org.junit.Assert.*; |
| 36 | import static org.onosproject.net.region.Region.Type.*; |
| 37 | import static org.onosproject.net.region.RegionEvent.Type.*; |
| 38 | |
| 39 | /** |
| 40 | * Test of the distributed region store implementation. |
| 41 | */ |
| 42 | public class DistributedRegionStoreTest { |
| 43 | |
| 44 | private static final RegionId RID1 = RegionId.regionId("r1"); |
| 45 | private static final RegionId RID2 = RegionId.regionId("r2"); |
| 46 | |
| 47 | private static final DeviceId DID1 = DeviceId.deviceId("foo:d1"); |
| 48 | private static final DeviceId DID2 = DeviceId.deviceId("foo:d2"); |
| 49 | private static final DeviceId DID3 = DeviceId.deviceId("foo:d3"); |
| 50 | |
| 51 | private static final NodeId NID1 = NodeId.nodeId("n1"); |
| 52 | |
| 53 | private static final List<Set<NodeId>> MASTERS = ImmutableList.of(ImmutableSet.of(NID1)); |
| 54 | |
| 55 | private TestStore store; |
| 56 | private RegionEvent event; |
| 57 | |
| 58 | /** |
| 59 | * Sets up the device key store and the storage service test harness. |
| 60 | */ |
| 61 | @Before |
| 62 | public void setUp() { |
| 63 | store = new TestStore(); |
| 64 | store.storageService = new TestStorageService(); |
| 65 | store.setDelegate(e -> this.event = e); |
| 66 | store.activate(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tears down the device key store. |
| 71 | */ |
| 72 | @After |
| 73 | public void tearDown() { |
| 74 | store.deactivate(); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void basics() { |
| 79 | Region r1 = store.createRegion(RID1, "R1", METRO, MASTERS); |
| 80 | assertEquals("incorrect id", RID1, r1.id()); |
| 81 | assertEquals("incorrect event", REGION_ADDED, event.type()); |
| 82 | |
| 83 | Region r2 = store.createRegion(RID2, "R2", CAMPUS, MASTERS); |
| 84 | assertEquals("incorrect id", RID2, r2.id()); |
| 85 | assertEquals("incorrect type", CAMPUS, r2.type()); |
| 86 | assertEquals("incorrect event", REGION_ADDED, event.type()); |
| 87 | |
| 88 | r2 = store.updateRegion(RID2, "R2", COUNTRY, MASTERS); |
| 89 | assertEquals("incorrect type", COUNTRY, r2.type()); |
| 90 | assertEquals("incorrect event", REGION_UPDATED, event.type()); |
| 91 | |
| 92 | Set<Region> regions = store.getRegions(); |
| 93 | assertEquals("incorrect size", 2, regions.size()); |
| 94 | assertTrue("missing r1", regions.contains(r1)); |
| 95 | assertTrue("missing r2", regions.contains(r2)); |
| 96 | |
| 97 | r1 = store.getRegion(RID1); |
| 98 | assertEquals("incorrect id", RID1, r1.id()); |
| 99 | |
| 100 | store.removeRegion(RID1); |
| 101 | regions = store.getRegions(); |
| 102 | assertEquals("incorrect size", 1, regions.size()); |
| 103 | assertTrue("missing r2", regions.contains(r2)); |
| 104 | assertEquals("incorrect event", REGION_REMOVED, event.type()); |
| 105 | } |
| 106 | |
| 107 | @Test(expected = IllegalArgumentException.class) |
| 108 | public void duplicateCreate() { |
| 109 | store.createRegion(RID1, "R1", METRO, MASTERS); |
| 110 | store.createRegion(RID1, "R2", CAMPUS, MASTERS); |
| 111 | } |
| 112 | |
| 113 | @Test(expected = ItemNotFoundException.class) |
| 114 | public void missingUpdate() { |
| 115 | store.updateRegion(RID1, "R1", METRO, MASTERS); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void membership() { |
| 120 | Region r = store.createRegion(RID1, "R1", METRO, MASTERS); |
| 121 | assertTrue("no devices expected", store.getRegionDevices(RID1).isEmpty()); |
| 122 | assertNull("no region expected", store.getRegionForDevice(DID1)); |
| 123 | |
| 124 | store.addDevices(RID1, ImmutableSet.of(DID1, DID2)); |
| 125 | Set<DeviceId> deviceIds = store.getRegionDevices(RID1); |
| 126 | assertEquals("incorrect device count", 2, deviceIds.size()); |
| 127 | assertTrue("missing d1", deviceIds.contains(DID1)); |
| 128 | assertTrue("missing d2", deviceIds.contains(DID2)); |
| 129 | assertEquals("wrong region", r, store.getRegionForDevice(DID1)); |
| 130 | |
| 131 | store.addDevices(RID1, ImmutableSet.of(DID3)); |
| 132 | deviceIds = store.getRegionDevices(RID1); |
| 133 | assertEquals("incorrect device count", 3, deviceIds.size()); |
| 134 | assertTrue("missing d3", deviceIds.contains(DID3)); |
| 135 | |
| 136 | store.addDevices(RID1, ImmutableSet.of(DID3, DID1)); |
| 137 | deviceIds = store.getRegionDevices(RID1); |
| 138 | assertEquals("incorrect device count", 3, deviceIds.size()); |
| 139 | |
Brian Stanke | d09e2ee | 2016-02-26 11:36:46 -0500 | [diff] [blame^] | 140 | // Test adding DID3 to RID2 but it is already in RID1. |
| 141 | // DID3 will be removed from RID1 and added to RID2. |
| 142 | Region r2 = store.createRegion(RID2, "R2", CAMPUS, MASTERS); |
| 143 | store.addDevices(RID2, ImmutableSet.of(DID3)); |
| 144 | deviceIds = store.getRegionDevices(RID1); |
| 145 | assertEquals("incorrect device count", 2, deviceIds.size()); |
| 146 | deviceIds = store.getRegionDevices(RID2); |
| 147 | assertEquals("incorrect device count", 1, deviceIds.size()); |
| 148 | |
Thomas Vachuska | 4844808 | 2016-02-19 22:14:54 -0800 | [diff] [blame] | 149 | store.removeDevices(RID1, ImmutableSet.of(DID2, DID3)); |
| 150 | deviceIds = store.getRegionDevices(RID1); |
| 151 | assertEquals("incorrect device count", 1, deviceIds.size()); |
| 152 | assertTrue("missing d1", deviceIds.contains(DID1)); |
| 153 | |
| 154 | store.removeDevices(RID1, ImmutableSet.of(DID1, DID3)); |
| 155 | assertTrue("no devices expected", store.getRegionDevices(RID1).isEmpty()); |
| 156 | |
| 157 | store.removeDevices(RID1, ImmutableSet.of(DID2)); |
| 158 | assertTrue("no devices expected", store.getRegionDevices(RID1).isEmpty()); |
| 159 | } |
| 160 | |
| 161 | class TestStore extends DistributedRegionStore { |
| 162 | } |
| 163 | } |