Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.ui.impl.topo.model; |
| 18 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 19 | import org.junit.Before; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 21 | import org.onosproject.event.Event; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 22 | import org.onosproject.event.EventDispatcher; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Host; |
| 26 | import org.onosproject.net.HostId; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 27 | import org.onosproject.net.Link; |
| 28 | import org.onosproject.net.region.Region; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.impl.topo.model.UiModelEvent.Type; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.model.topo.UiDevice; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 32 | import org.onosproject.ui.model.topo.UiDeviceLink; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.model.topo.UiElement; |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.model.topo.UiHost; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.model.topo.UiLinkId; |
| 36 | import org.onosproject.ui.model.topo.UiRegion; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | import java.util.Iterator; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 40 | |
| 41 | import static org.junit.Assert.assertEquals; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 42 | import static org.junit.Assert.assertFalse; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 43 | import static org.junit.Assert.assertNotNull; |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 44 | import static org.junit.Assert.assertNull; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 45 | import static org.junit.Assert.assertTrue; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 46 | import static org.onosproject.cluster.NodeId.nodeId; |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 47 | import static org.onosproject.net.PortNumber.portNumber; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 48 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Unit tests for {@link ModelCache}. |
| 52 | */ |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 53 | public class ModelCacheTest extends AbstractTopoModelTest { |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 54 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 55 | private class TestEvDisp implements EventDispatcher { |
| 56 | |
| 57 | private Event<Type, UiElement> lastEvent = null; |
| 58 | private int eventCount = 0; |
| 59 | |
| 60 | @Override |
| 61 | public void post(Event event) { |
| 62 | lastEvent = event; |
| 63 | eventCount++; |
| 64 | // print("Event dispatched: %s", event); |
| 65 | } |
| 66 | |
| 67 | private void assertEventCount(int exp) { |
| 68 | assertEquals("unex event count", exp, eventCount); |
| 69 | } |
| 70 | |
| 71 | private void assertLast(Type expEventType, String expId) { |
| 72 | assertNotNull("no last event", lastEvent); |
| 73 | assertEquals("unex event type", expEventType, lastEvent.type()); |
| 74 | assertEquals("unex element ID", expId, lastEvent.subject().idAsString()); |
| 75 | } |
| 76 | } |
| 77 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 78 | |
| 79 | private final TestEvDisp dispatcher = new TestEvDisp(); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 80 | |
| 81 | private ModelCache cache; |
| 82 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 83 | private void assertContains(String msg, Collection<?> coll, Object... things) { |
| 84 | for (Object o : things) { |
| 85 | assertTrue(msg, coll.contains(o)); |
| 86 | } |
| 87 | } |
| 88 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 89 | @Before |
| 90 | public void setUp() { |
| 91 | cache = new ModelCache(MOCK_SERVICES, dispatcher); |
| 92 | } |
| 93 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 94 | @Test |
| 95 | public void basic() { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 96 | title("basic"); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 97 | print(cache); |
| 98 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 99 | assertEquals("unex # regions", 0, cache.regionCount()); |
| 100 | } |
| 101 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 102 | @Test |
| 103 | public void addAndRemoveClusterMember() { |
| 104 | title("addAndRemoveClusterMember"); |
| 105 | print(cache); |
| 106 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 107 | dispatcher.assertEventCount(0); |
| 108 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 109 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 110 | print(cache); |
| 111 | assertEquals("unex # members", 1, cache.clusterMemberCount()); |
| 112 | dispatcher.assertEventCount(1); |
| 113 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
| 114 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 115 | cache.removeClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 116 | print(cache); |
| 117 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 118 | dispatcher.assertEventCount(2); |
| 119 | dispatcher.assertLast(Type.CLUSTER_MEMBER_REMOVED, C1); |
| 120 | } |
| 121 | |
| 122 | @Test |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 123 | public void nonExistentClusterMember() { |
| 124 | title("nonExistentClusterMember"); |
| 125 | cache.addOrUpdateClusterMember(CNODE_1); |
| 126 | print(cache); |
| 127 | assertEquals("unex # members", 1, cache.clusterMemberCount()); |
| 128 | dispatcher.assertEventCount(1); |
| 129 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
| 130 | |
| 131 | cache.removeClusterMember(CNODE_2); |
| 132 | assertEquals("unex # members", 1, cache.clusterMemberCount()); |
| 133 | dispatcher.assertEventCount(1); |
| 134 | } |
| 135 | |
| 136 | @Test |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 137 | public void createThreeNodeCluster() { |
| 138 | title("createThreeNodeCluster"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 139 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 140 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 141 | cache.addOrUpdateClusterMember(CNODE_2); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 142 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C2); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 143 | cache.addOrUpdateClusterMember(CNODE_3); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 144 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C3); |
| 145 | dispatcher.assertEventCount(3); |
| 146 | print(cache); |
| 147 | } |
| 148 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 149 | @Test |
| 150 | public void addNodeThenExamineIt() { |
| 151 | title("addNodeThenExamineIt"); |
| 152 | cache.addOrUpdateClusterMember(CNODE_1); |
| 153 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
| 154 | |
| 155 | UiClusterMember member = cache.accessClusterMember(nodeId(C1)); |
| 156 | print(member); |
| 157 | // see AbstractUiImplTest Mock Environment for expected values... |
| 158 | assertEquals("wrong id str", C1, member.idAsString()); |
| 159 | assertEquals("wrong id", nodeId(C1), member.id()); |
| 160 | assertEquals("wrong dev count", 3, member.deviceCount()); |
| 161 | assertEquals("not online", true, member.isOnline()); |
| 162 | assertEquals("not ready", true, member.isReady()); |
| 163 | |
| 164 | assertMasterOf(member, DEVID_1, DEVID_2, DEVID_3); |
| 165 | assertNotMasterOf(member, DEVID_4, DEVID_6, DEVID_9); |
| 166 | } |
| 167 | |
| 168 | private void assertMasterOf(UiClusterMember member, DeviceId... ids) { |
| 169 | for (DeviceId id : ids) { |
| 170 | assertTrue("not master of " + id, member.masterOf(id)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private void assertNotMasterOf(UiClusterMember member, DeviceId... ids) { |
| 175 | for (DeviceId id : ids) { |
| 176 | assertFalse("? master of " + id, member.masterOf(id)); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | |
| 181 | @Test |
| 182 | public void addNodeAndDevices() { |
| 183 | title("addNodeAndDevices"); |
| 184 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 185 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 186 | cache.addOrUpdateDevice(DEV_1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 187 | dispatcher.assertLast(Type.DEVICE_ADDED_OR_UPDATED, D1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 188 | cache.addOrUpdateDevice(DEV_2); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 189 | dispatcher.assertLast(Type.DEVICE_ADDED_OR_UPDATED, D2); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 190 | cache.addOrUpdateDevice(DEV_3); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 191 | dispatcher.assertLast(Type.DEVICE_ADDED_OR_UPDATED, D3); |
| 192 | dispatcher.assertEventCount(4); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 193 | print(cache); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 194 | |
| 195 | assertEquals("unex # nodes", 1, cache.clusterMemberCount()); |
| 196 | assertEquals("unex # devices", 3, cache.deviceCount()); |
| 197 | cache.removeDevice(DEV_4); |
| 198 | assertEquals("unex # devices", 3, cache.deviceCount()); |
| 199 | dispatcher.assertEventCount(4); |
| 200 | |
| 201 | cache.removeDevice(DEV_2); |
| 202 | dispatcher.assertLast(Type.DEVICE_REMOVED, D2); |
| 203 | dispatcher.assertEventCount(5); |
| 204 | |
| 205 | // check out details of device |
| 206 | UiDevice dev = cache.accessDevice(DEVID_1); |
| 207 | assertEquals("wrong id", D1, dev.idAsString()); |
| 208 | assertEquals("wrong region", R1, dev.regionId().toString()); |
| 209 | Device d = dev.backingDevice(); |
| 210 | assertEquals("wrong serial", SERIAL, d.serialNumber()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Test |
| 214 | public void addRegions() { |
| 215 | title("addRegions"); |
| 216 | cache.addOrUpdateRegion(REGION_1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 217 | dispatcher.assertLast(Type.REGION_ADDED_OR_UPDATED, R1); |
| 218 | dispatcher.assertEventCount(1); |
| 219 | assertEquals("unex # regions", 1, cache.regionCount()); |
| 220 | |
| 221 | cache.addOrUpdateRegion(REGION_2); |
| 222 | dispatcher.assertLast(Type.REGION_ADDED_OR_UPDATED, R2); |
| 223 | dispatcher.assertEventCount(2); |
| 224 | assertEquals("unex # regions", 2, cache.regionCount()); |
| 225 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 226 | print(cache); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 227 | |
| 228 | cache.removeRegion(REGION_3); |
| 229 | dispatcher.assertEventCount(2); |
| 230 | assertEquals("unex # regions", 2, cache.regionCount()); |
| 231 | |
| 232 | cache.removeRegion(REGION_1); |
| 233 | dispatcher.assertLast(Type.REGION_REMOVED, R1); |
| 234 | dispatcher.assertEventCount(3); |
| 235 | assertEquals("unex # regions", 1, cache.regionCount()); |
| 236 | |
| 237 | print(cache); |
| 238 | |
| 239 | UiRegion region = cache.accessRegion(REGION_2.id()); |
| 240 | assertEquals("wrong id", REGION_2.id(), region.id()); |
| 241 | assertEquals("unex # device IDs", 3, region.deviceIds().size()); |
| 242 | assertContains("missing ID", region.deviceIds(), DEVID_4, DEVID_5, DEVID_6); |
| 243 | Region r = region.backingRegion(); |
| 244 | print(r); |
| 245 | assertEquals("wrong region name", "Region-R2", r.name()); |
| 246 | } |
| 247 | |
| 248 | private static final String[] LINKS_2_7 = {D2, "27", D7, "72"}; |
| 249 | |
| 250 | @Test |
| 251 | public void addLinks() { |
| 252 | title("addLinks"); |
| 253 | |
| 254 | Iterator<Link> iter = makeLinkPair(LINKS_2_7).iterator(); |
| 255 | Link link1 = iter.next(); |
| 256 | Link link2 = iter.next(); |
| 257 | print(link1); |
| 258 | print(link2); |
| 259 | |
| 260 | UiLinkId idA2B = uiLinkId(link1); |
| 261 | UiLinkId idB2A = uiLinkId(link2); |
| 262 | // remember, link IDs are canonicalized |
| 263 | assertEquals("not same link ID", idA2B, idB2A); |
| 264 | |
| 265 | // we've established that the ID is the same for both |
| 266 | UiLinkId linkId = idA2B; |
| 267 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 268 | cache.addOrUpdateDeviceLink(link1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 269 | dispatcher.assertLast(Type.LINK_ADDED_OR_UPDATED, linkId.toString()); |
| 270 | dispatcher.assertEventCount(1); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 271 | assertEquals("unex # links", 1, cache.deviceLinkCount()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 272 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 273 | UiDeviceLink link = cache.accessDeviceLink(linkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 274 | assertEquals("dev A not d2", DEVID_2, link.deviceA()); |
| 275 | assertEquals("dev B not d7", DEVID_7, link.deviceB()); |
| 276 | assertEquals("wrong backing link A-B", link1, link.linkAtoB()); |
| 277 | assertEquals("backing link B-A?", null, link.linkBtoA()); |
| 278 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 279 | cache.addOrUpdateDeviceLink(link2); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 280 | dispatcher.assertLast(Type.LINK_ADDED_OR_UPDATED, linkId.toString()); |
| 281 | dispatcher.assertEventCount(2); |
| 282 | // NOTE: yes! expect 1 UiLink |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 283 | assertEquals("unex # links", 1, cache.deviceLinkCount()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 284 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 285 | link = cache.accessDeviceLink(linkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 286 | assertEquals("dev A not d2", DEVID_2, link.deviceA()); |
| 287 | assertEquals("dev B not d7", DEVID_7, link.deviceB()); |
| 288 | assertEquals("wrong backing link A-B", link1, link.linkAtoB()); |
| 289 | assertEquals("wrong backing link B-A", link2, link.linkBtoA()); |
| 290 | |
| 291 | // now remove links one at a time |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 292 | cache.removeDeviceLink(link1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 293 | // NOTE: yes! ADD_OR_UPDATE, since the link was updated |
| 294 | dispatcher.assertLast(Type.LINK_ADDED_OR_UPDATED, linkId.toString()); |
| 295 | dispatcher.assertEventCount(3); |
| 296 | // NOTE: yes! expect 1 UiLink (still) |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 297 | assertEquals("unex # links", 1, cache.deviceLinkCount()); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 298 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 299 | link = cache.accessDeviceLink(linkId); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 300 | assertEquals("dev A not d2", DEVID_2, link.deviceA()); |
| 301 | assertEquals("dev B not d7", DEVID_7, link.deviceB()); |
| 302 | assertEquals("backing link A-B?", null, link.linkAtoB()); |
| 303 | assertEquals("wrong backing link B-A", link2, link.linkBtoA()); |
| 304 | |
| 305 | // remove final link |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 306 | cache.removeDeviceLink(link2); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 307 | dispatcher.assertLast(Type.LINK_REMOVED, linkId.toString()); |
| 308 | dispatcher.assertEventCount(4); |
| 309 | // NOTE: finally link should be removed from cache |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 310 | assertEquals("unex # links", 0, cache.deviceLinkCount()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 313 | private void assertHostLinkCounts(int nHosts, int nLinks) { |
| 314 | assertEquals("unex # hosts", nHosts, cache.hostCount()); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 315 | assertEquals("unex # links", nLinks, cache.edgeLinkCount()); |
Simon Hunt | eae81ee | 2016-05-19 12:33:22 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | private void assertLocation(HostId hid, DeviceId expDev, int expPort) { |
| 319 | UiHost h = cache.accessHost(hid); |
| 320 | assertEquals("unex device", expDev, h.locationDevice()); |
| 321 | assertEquals("unex port", portNumber(expPort), h.locationPort()); |
| 322 | } |
| 323 | |
| 324 | @Test |
| 325 | public void addHosts() { |
| 326 | title("addHosts"); |
| 327 | |
| 328 | assertHostLinkCounts(0, 0); |
| 329 | Host hostA = createHost(DEV_1, 101, "a"); |
| 330 | Host hostB = createHost(DEV_1, 102, "b"); |
| 331 | |
| 332 | // add a host |
| 333 | cache.addOrUpdateHost(hostA); |
| 334 | dispatcher.assertLast(Type.HOST_ADDED_OR_UPDATED, hostA.id().toString()); |
| 335 | dispatcher.assertEventCount(1); |
| 336 | assertHostLinkCounts(1, 1); |
| 337 | assertLocation(hostA.id(), DEVID_1, 101); |
| 338 | |
| 339 | // add a second host |
| 340 | cache.addOrUpdateHost(hostB); |
| 341 | dispatcher.assertLast(Type.HOST_ADDED_OR_UPDATED, hostB.id().toString()); |
| 342 | dispatcher.assertEventCount(2); |
| 343 | assertHostLinkCounts(2, 2); |
| 344 | assertLocation(hostB.id(), DEVID_1, 102); |
| 345 | |
| 346 | // update the first host |
| 347 | cache.addOrUpdateHost(hostA); |
| 348 | dispatcher.assertLast(Type.HOST_ADDED_OR_UPDATED, hostA.id().toString()); |
| 349 | dispatcher.assertEventCount(3); |
| 350 | assertHostLinkCounts(2, 2); |
| 351 | assertLocation(hostA.id(), DEVID_1, 101); |
| 352 | |
| 353 | print(cache.dumpString()); |
| 354 | |
| 355 | // remove the second host |
| 356 | cache.removeHost(hostB); |
| 357 | dispatcher.assertLast(Type.HOST_REMOVED, hostB.id().toString()); |
| 358 | dispatcher.assertEventCount(4); |
| 359 | assertHostLinkCounts(1, 1); |
| 360 | assertNull("still host B?", cache.accessHost(hostB.id())); |
| 361 | |
| 362 | print(cache.dumpString()); |
| 363 | |
| 364 | // first, verify where host A is currently residing |
| 365 | assertLocation(hostA.id(), DEVID_1, 101); |
| 366 | |
| 367 | // now let's move hostA to a different port |
| 368 | Host movedHost = createHost(DEV_1, 200, "a"); |
| 369 | print(hostA); |
| 370 | print(movedHost); |
| 371 | |
| 372 | cache.moveHost(movedHost, hostA); |
| 373 | dispatcher.assertLast(Type.HOST_MOVED, hostA.id().toString()); |
| 374 | dispatcher.assertEventCount(5); |
| 375 | assertHostLinkCounts(1, 1); |
| 376 | |
| 377 | assertLocation(hostA.id(), DEVID_1, 200); |
| 378 | |
| 379 | print(cache.dumpString()); |
| 380 | |
| 381 | // finally, let's move the host to a different device and port |
| 382 | Host movedAgain = createHost(DEV_8, 800, "a"); |
| 383 | |
| 384 | cache.moveHost(movedAgain, movedHost); |
| 385 | dispatcher.assertLast(Type.HOST_MOVED, hostA.id().toString()); |
| 386 | dispatcher.assertEventCount(6); |
| 387 | assertHostLinkCounts(1, 1); |
| 388 | |
| 389 | assertLocation(hostA.id(), DEVID_8, 800); |
| 390 | |
| 391 | print(cache.dumpString()); |
| 392 | } |
| 393 | |
| 394 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 395 | @Test |
| 396 | public void load() { |
| 397 | title("load"); |
| 398 | cache.load(); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 399 | print(cache.dumpString()); |
| 400 | |
| 401 | // See mock service bundle for expected values (AbstractTopoModelTest) |
| 402 | assertEquals("unex # cnodes", 3, cache.clusterMemberCount()); |
| 403 | assertEquals("unex # regions", 3, cache.regionCount()); |
| 404 | assertEquals("unex # devices", 9, cache.deviceCount()); |
| 405 | assertEquals("unex # hosts", 18, cache.hostCount()); |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame^] | 406 | assertEquals("unex # device-links", 8, cache.deviceLinkCount()); |
| 407 | assertEquals("unex # edge-links", 18, cache.edgeLinkCount()); |
| 408 | assertEquals("unex # synth-links", 0, cache.synthLinkCount()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 409 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 410 | } |