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 | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.region.Region; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.impl.topo.model.UiModelEvent.Type; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.model.topo.UiClusterMember; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.model.topo.UiDevice; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.model.topo.UiElement; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.model.topo.UiLink; |
| 32 | import org.onosproject.ui.model.topo.UiLinkId; |
| 33 | import org.onosproject.ui.model.topo.UiRegion; |
| 34 | |
| 35 | import java.util.Collection; |
| 36 | import java.util.Iterator; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 37 | |
| 38 | import static org.junit.Assert.assertEquals; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 39 | import static org.junit.Assert.assertFalse; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 40 | import static org.junit.Assert.assertNotNull; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 41 | import static org.junit.Assert.assertTrue; |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 42 | import static org.onosproject.cluster.NodeId.nodeId; |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 43 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Unit tests for {@link ModelCache}. |
| 47 | */ |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 48 | public class ModelCacheTest extends AbstractTopoModelTest { |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 49 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 50 | private class TestEvDisp implements EventDispatcher { |
| 51 | |
| 52 | private Event<Type, UiElement> lastEvent = null; |
| 53 | private int eventCount = 0; |
| 54 | |
| 55 | @Override |
| 56 | public void post(Event event) { |
| 57 | lastEvent = event; |
| 58 | eventCount++; |
| 59 | // print("Event dispatched: %s", event); |
| 60 | } |
| 61 | |
| 62 | private void assertEventCount(int exp) { |
| 63 | assertEquals("unex event count", exp, eventCount); |
| 64 | } |
| 65 | |
| 66 | private void assertLast(Type expEventType, String expId) { |
| 67 | assertNotNull("no last event", lastEvent); |
| 68 | assertEquals("unex event type", expEventType, lastEvent.type()); |
| 69 | assertEquals("unex element ID", expId, lastEvent.subject().idAsString()); |
| 70 | } |
| 71 | } |
| 72 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 73 | |
| 74 | private final TestEvDisp dispatcher = new TestEvDisp(); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 75 | |
| 76 | private ModelCache cache; |
| 77 | |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 78 | private void assertContains(String msg, Collection<?> coll, Object... things) { |
| 79 | for (Object o : things) { |
| 80 | assertTrue(msg, coll.contains(o)); |
| 81 | } |
| 82 | } |
| 83 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 84 | @Before |
| 85 | public void setUp() { |
| 86 | cache = new ModelCache(MOCK_SERVICES, dispatcher); |
| 87 | } |
| 88 | |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 89 | @Test |
| 90 | public void basic() { |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 91 | title("basic"); |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 92 | print(cache); |
| 93 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 94 | assertEquals("unex # regions", 0, cache.regionCount()); |
| 95 | } |
| 96 | |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 97 | @Test |
| 98 | public void addAndRemoveClusterMember() { |
| 99 | title("addAndRemoveClusterMember"); |
| 100 | print(cache); |
| 101 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 102 | dispatcher.assertEventCount(0); |
| 103 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 104 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 105 | print(cache); |
| 106 | assertEquals("unex # members", 1, cache.clusterMemberCount()); |
| 107 | dispatcher.assertEventCount(1); |
| 108 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
| 109 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 110 | cache.removeClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 111 | print(cache); |
| 112 | assertEquals("unex # members", 0, cache.clusterMemberCount()); |
| 113 | dispatcher.assertEventCount(2); |
| 114 | dispatcher.assertLast(Type.CLUSTER_MEMBER_REMOVED, C1); |
| 115 | } |
| 116 | |
| 117 | @Test |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 118 | public void nonExistentClusterMember() { |
| 119 | title("nonExistentClusterMember"); |
| 120 | cache.addOrUpdateClusterMember(CNODE_1); |
| 121 | print(cache); |
| 122 | assertEquals("unex # members", 1, cache.clusterMemberCount()); |
| 123 | dispatcher.assertEventCount(1); |
| 124 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
| 125 | |
| 126 | cache.removeClusterMember(CNODE_2); |
| 127 | assertEquals("unex # members", 1, cache.clusterMemberCount()); |
| 128 | dispatcher.assertEventCount(1); |
| 129 | } |
| 130 | |
| 131 | @Test |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 132 | public void createThreeNodeCluster() { |
| 133 | title("createThreeNodeCluster"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 134 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 135 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 136 | cache.addOrUpdateClusterMember(CNODE_2); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 137 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C2); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 138 | cache.addOrUpdateClusterMember(CNODE_3); |
Simon Hunt | 642bc45 | 2016-05-04 19:34:45 -0700 | [diff] [blame] | 139 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C3); |
| 140 | dispatcher.assertEventCount(3); |
| 141 | print(cache); |
| 142 | } |
| 143 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 144 | @Test |
| 145 | public void addNodeThenExamineIt() { |
| 146 | title("addNodeThenExamineIt"); |
| 147 | cache.addOrUpdateClusterMember(CNODE_1); |
| 148 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
| 149 | |
| 150 | UiClusterMember member = cache.accessClusterMember(nodeId(C1)); |
| 151 | print(member); |
| 152 | // see AbstractUiImplTest Mock Environment for expected values... |
| 153 | assertEquals("wrong id str", C1, member.idAsString()); |
| 154 | assertEquals("wrong id", nodeId(C1), member.id()); |
| 155 | assertEquals("wrong dev count", 3, member.deviceCount()); |
| 156 | assertEquals("not online", true, member.isOnline()); |
| 157 | assertEquals("not ready", true, member.isReady()); |
| 158 | |
| 159 | assertMasterOf(member, DEVID_1, DEVID_2, DEVID_3); |
| 160 | assertNotMasterOf(member, DEVID_4, DEVID_6, DEVID_9); |
| 161 | } |
| 162 | |
| 163 | private void assertMasterOf(UiClusterMember member, DeviceId... ids) { |
| 164 | for (DeviceId id : ids) { |
| 165 | assertTrue("not master of " + id, member.masterOf(id)); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | private void assertNotMasterOf(UiClusterMember member, DeviceId... ids) { |
| 170 | for (DeviceId id : ids) { |
| 171 | assertFalse("? master of " + id, member.masterOf(id)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | @Test |
| 177 | public void addNodeAndDevices() { |
| 178 | title("addNodeAndDevices"); |
| 179 | cache.addOrUpdateClusterMember(CNODE_1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 180 | dispatcher.assertLast(Type.CLUSTER_MEMBER_ADDED_OR_UPDATED, C1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 181 | cache.addOrUpdateDevice(DEV_1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 182 | dispatcher.assertLast(Type.DEVICE_ADDED_OR_UPDATED, D1); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 183 | cache.addOrUpdateDevice(DEV_2); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 184 | dispatcher.assertLast(Type.DEVICE_ADDED_OR_UPDATED, D2); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 185 | cache.addOrUpdateDevice(DEV_3); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 186 | dispatcher.assertLast(Type.DEVICE_ADDED_OR_UPDATED, D3); |
| 187 | dispatcher.assertEventCount(4); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 188 | print(cache); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 189 | |
| 190 | assertEquals("unex # nodes", 1, cache.clusterMemberCount()); |
| 191 | assertEquals("unex # devices", 3, cache.deviceCount()); |
| 192 | cache.removeDevice(DEV_4); |
| 193 | assertEquals("unex # devices", 3, cache.deviceCount()); |
| 194 | dispatcher.assertEventCount(4); |
| 195 | |
| 196 | cache.removeDevice(DEV_2); |
| 197 | dispatcher.assertLast(Type.DEVICE_REMOVED, D2); |
| 198 | dispatcher.assertEventCount(5); |
| 199 | |
| 200 | // check out details of device |
| 201 | UiDevice dev = cache.accessDevice(DEVID_1); |
| 202 | assertEquals("wrong id", D1, dev.idAsString()); |
| 203 | assertEquals("wrong region", R1, dev.regionId().toString()); |
| 204 | Device d = dev.backingDevice(); |
| 205 | assertEquals("wrong serial", SERIAL, d.serialNumber()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Test |
| 209 | public void addRegions() { |
| 210 | title("addRegions"); |
| 211 | cache.addOrUpdateRegion(REGION_1); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 212 | dispatcher.assertLast(Type.REGION_ADDED_OR_UPDATED, R1); |
| 213 | dispatcher.assertEventCount(1); |
| 214 | assertEquals("unex # regions", 1, cache.regionCount()); |
| 215 | |
| 216 | cache.addOrUpdateRegion(REGION_2); |
| 217 | dispatcher.assertLast(Type.REGION_ADDED_OR_UPDATED, R2); |
| 218 | dispatcher.assertEventCount(2); |
| 219 | assertEquals("unex # regions", 2, cache.regionCount()); |
| 220 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 221 | print(cache); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 222 | |
| 223 | cache.removeRegion(REGION_3); |
| 224 | dispatcher.assertEventCount(2); |
| 225 | assertEquals("unex # regions", 2, cache.regionCount()); |
| 226 | |
| 227 | cache.removeRegion(REGION_1); |
| 228 | dispatcher.assertLast(Type.REGION_REMOVED, R1); |
| 229 | dispatcher.assertEventCount(3); |
| 230 | assertEquals("unex # regions", 1, cache.regionCount()); |
| 231 | |
| 232 | print(cache); |
| 233 | |
| 234 | UiRegion region = cache.accessRegion(REGION_2.id()); |
| 235 | assertEquals("wrong id", REGION_2.id(), region.id()); |
| 236 | assertEquals("unex # device IDs", 3, region.deviceIds().size()); |
| 237 | assertContains("missing ID", region.deviceIds(), DEVID_4, DEVID_5, DEVID_6); |
| 238 | Region r = region.backingRegion(); |
| 239 | print(r); |
| 240 | assertEquals("wrong region name", "Region-R2", r.name()); |
| 241 | } |
| 242 | |
| 243 | private static final String[] LINKS_2_7 = {D2, "27", D7, "72"}; |
| 244 | |
| 245 | @Test |
| 246 | public void addLinks() { |
| 247 | title("addLinks"); |
| 248 | |
| 249 | Iterator<Link> iter = makeLinkPair(LINKS_2_7).iterator(); |
| 250 | Link link1 = iter.next(); |
| 251 | Link link2 = iter.next(); |
| 252 | print(link1); |
| 253 | print(link2); |
| 254 | |
| 255 | UiLinkId idA2B = uiLinkId(link1); |
| 256 | UiLinkId idB2A = uiLinkId(link2); |
| 257 | // remember, link IDs are canonicalized |
| 258 | assertEquals("not same link ID", idA2B, idB2A); |
| 259 | |
| 260 | // we've established that the ID is the same for both |
| 261 | UiLinkId linkId = idA2B; |
| 262 | |
| 263 | cache.addOrUpdateLink(link1); |
| 264 | dispatcher.assertLast(Type.LINK_ADDED_OR_UPDATED, linkId.toString()); |
| 265 | dispatcher.assertEventCount(1); |
| 266 | assertEquals("unex # links", 1, cache.linkCount()); |
| 267 | |
| 268 | UiLink link = cache.accessLink(linkId); |
| 269 | assertEquals("dev A not d2", DEVID_2, link.deviceA()); |
| 270 | assertEquals("dev B not d7", DEVID_7, link.deviceB()); |
| 271 | assertEquals("wrong backing link A-B", link1, link.linkAtoB()); |
| 272 | assertEquals("backing link B-A?", null, link.linkBtoA()); |
| 273 | |
| 274 | cache.addOrUpdateLink(link2); |
| 275 | dispatcher.assertLast(Type.LINK_ADDED_OR_UPDATED, linkId.toString()); |
| 276 | dispatcher.assertEventCount(2); |
| 277 | // NOTE: yes! expect 1 UiLink |
| 278 | assertEquals("unex # links", 1, cache.linkCount()); |
| 279 | |
| 280 | link = cache.accessLink(linkId); |
| 281 | assertEquals("dev A not d2", DEVID_2, link.deviceA()); |
| 282 | assertEquals("dev B not d7", DEVID_7, link.deviceB()); |
| 283 | assertEquals("wrong backing link A-B", link1, link.linkAtoB()); |
| 284 | assertEquals("wrong backing link B-A", link2, link.linkBtoA()); |
| 285 | |
| 286 | // now remove links one at a time |
| 287 | cache.removeLink(link1); |
| 288 | // NOTE: yes! ADD_OR_UPDATE, since the link was updated |
| 289 | dispatcher.assertLast(Type.LINK_ADDED_OR_UPDATED, linkId.toString()); |
| 290 | dispatcher.assertEventCount(3); |
| 291 | // NOTE: yes! expect 1 UiLink (still) |
| 292 | assertEquals("unex # links", 1, cache.linkCount()); |
| 293 | |
| 294 | link = cache.accessLink(linkId); |
| 295 | assertEquals("dev A not d2", DEVID_2, link.deviceA()); |
| 296 | assertEquals("dev B not d7", DEVID_7, link.deviceB()); |
| 297 | assertEquals("backing link A-B?", null, link.linkAtoB()); |
| 298 | assertEquals("wrong backing link B-A", link2, link.linkBtoA()); |
| 299 | |
| 300 | // remove final link |
| 301 | cache.removeLink(link2); |
| 302 | dispatcher.assertLast(Type.LINK_REMOVED, linkId.toString()); |
| 303 | dispatcher.assertEventCount(4); |
| 304 | // NOTE: finally link should be removed from cache |
| 305 | assertEquals("unex # links", 0, cache.linkCount()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | @Test |
| 309 | public void load() { |
| 310 | title("load"); |
| 311 | cache.load(); |
Simon Hunt | 58a0dd0 | 2016-05-17 11:54:23 -0700 | [diff] [blame] | 312 | print(cache.dumpString()); |
| 313 | |
| 314 | // See mock service bundle for expected values (AbstractTopoModelTest) |
| 315 | assertEquals("unex # cnodes", 3, cache.clusterMemberCount()); |
| 316 | assertEquals("unex # regions", 3, cache.regionCount()); |
| 317 | assertEquals("unex # devices", 9, cache.deviceCount()); |
| 318 | assertEquals("unex # hosts", 18, cache.hostCount()); |
| 319 | assertEquals("unex # hosts", 26, cache.linkCount()); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 320 | } |
Simon Hunt | 338a3b4 | 2016-04-14 09:43:52 -0700 | [diff] [blame] | 321 | } |