Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | package org.onosproject.incubator.net.l2monitoring.cfm.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.junit.TestUtils; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 25 | import org.onosproject.cluster.ControllerNode; |
| 26 | import org.onosproject.cluster.DefaultControllerNode; |
| 27 | import org.onosproject.cluster.NodeId; |
| 28 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
| 31 | import org.onosproject.core.IdGenerator; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.Component; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultComponent; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 36 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 37 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 38 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 39 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 40 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 41 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 42 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 43 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.store.service.TestStorageService; |
| 46 | |
| 47 | import java.util.Collection; |
| 48 | import java.util.Optional; |
| 49 | import java.util.Set; |
| 50 | import java.util.concurrent.atomic.AtomicLong; |
| 51 | |
| 52 | import static junit.framework.TestCase.assertFalse; |
| 53 | import static org.junit.Assert.assertEquals; |
| 54 | import static org.junit.Assert.assertTrue; |
| 55 | import static org.junit.Assert.fail; |
| 56 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 57 | |
| 58 | /** |
| 59 | * CFM MD Manager test. |
| 60 | */ |
| 61 | public class CfmMdManagerTest { |
| 62 | private static final NodeId NID_LOCAL = new NodeId("local"); |
| 63 | private static final IpAddress LOCALHOST = IpAddress.valueOf("127.0.0.1"); |
| 64 | |
| 65 | private DistributedMdStore mdStore; |
| 66 | private CfmMdService service; |
| 67 | private CfmMdManager manager; |
| 68 | |
| 69 | @Before |
| 70 | public void setup() throws Exception, CfmConfigException { |
| 71 | mdStore = new DistributedMdStore(); |
| 72 | |
| 73 | MaintenanceAssociation maTest11 = DefaultMaintenanceAssociation |
| 74 | .builder(MaIdCharStr.asMaId("test-ma-1-1"), 9) |
| 75 | .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_10MIN) |
| 76 | .maNumericId((short) 1) |
| 77 | .addToRemoteMepIdList(MepId.valueOf((short) 101)) |
| 78 | .addToRemoteMepIdList(MepId.valueOf((short) 102)) |
| 79 | .addToComponentList( |
| 80 | DefaultComponent.builder(1) |
| 81 | .tagType(Component.TagType.VLAN_CTAG) |
| 82 | .build()) |
| 83 | .build(); |
| 84 | |
| 85 | MaintenanceAssociation maTest12 = DefaultMaintenanceAssociation |
| 86 | .builder(MaIdCharStr.asMaId("test-ma-1-2"), 9) |
| 87 | .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_10MIN) |
| 88 | .maNumericId((short) 2) |
| 89 | .addToRemoteMepIdList(MepId.valueOf((short) 201)) |
| 90 | .addToRemoteMepIdList(MepId.valueOf((short) 202)) |
| 91 | .addToComponentList( |
| 92 | DefaultComponent.builder(2) |
| 93 | .tagType(Component.TagType.VLAN_CTAG) |
| 94 | .build()) |
| 95 | .build(); |
| 96 | |
| 97 | MaintenanceDomain mdTest1 = DefaultMaintenanceDomain |
| 98 | .builder(MdIdCharStr.asMdId("test-md-1")) |
| 99 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL1) |
| 100 | .mdNumericId((short) 1) |
| 101 | .addToMaList(maTest11) |
| 102 | .addToMaList(maTest12) |
| 103 | .build(); |
| 104 | |
| 105 | TestUtils.setField(mdStore, "storageService", new TestStorageService()); |
| 106 | TestUtils.setField(mdStore, "clusterService", new CfmMdManagerTest.TestClusterService()); |
| 107 | TestUtils.setField(mdStore, "mastershipService", new CfmMdManagerTest.TestMastershipService()); |
| 108 | mdStore.activate(); |
| 109 | mdStore.createUpdateMaintenanceDomain(mdTest1); |
| 110 | |
| 111 | manager = new CfmMdManager(); |
| 112 | manager.store = mdStore; |
| 113 | service = manager; |
| 114 | TestUtils.setField(manager, "storageService", new TestStorageService()); |
| 115 | TestUtils.setField(manager, "coreService", new TestCoreService()); |
| 116 | injectEventDispatcher(manager, new TestEventDispatcher()); |
| 117 | |
| 118 | manager.appId = new CfmMdManagerTest.TestApplicationId(0, "CfmMdManagerTest"); |
| 119 | manager.activate(); |
| 120 | } |
| 121 | |
| 122 | @After |
| 123 | public void tearDown() { |
| 124 | |
| 125 | manager.deactivate(); |
| 126 | injectEventDispatcher(manager, null); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testGetAllMaintenanceDomain() { |
| 132 | Collection<MaintenanceDomain> mdList = service.getAllMaintenanceDomain(); |
| 133 | assertEquals(1, mdList.size()); |
| 134 | |
| 135 | MaintenanceDomain md = mdList.iterator().next(); |
| 136 | assertEquals(1, md.mdNumericId()); |
| 137 | |
| 138 | assertEquals(2, md.maintenanceAssociationList().size()); |
| 139 | |
| 140 | md.maintenanceAssociationList().iterator().forEachRemaining(ma -> |
| 141 | assertTrue(ma.maId().maName().endsWith(String.valueOf(ma.maNumericId()))) |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void testGetMaintenanceDomain() { |
| 147 | Optional<MaintenanceDomain> md = |
| 148 | service.getMaintenanceDomain(MdIdCharStr.asMdId("test-md-1")); |
| 149 | assertTrue(md.isPresent()); |
| 150 | |
| 151 | assertEquals(1, md.get().mdNumericId()); |
| 152 | |
| 153 | assertEquals(2, md.get().maintenanceAssociationList().size()); |
| 154 | |
| 155 | |
| 156 | |
| 157 | //Now try an invalid name |
| 158 | Optional<MaintenanceDomain> mdInvalid = |
| 159 | service.getMaintenanceDomain(MdIdCharStr.asMdId("test-md-3")); |
| 160 | assertFalse(mdInvalid.isPresent()); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testDeleteMaintenanceDomain() { |
| 165 | try { |
| 166 | assertTrue(service.deleteMaintenanceDomain( |
| 167 | MdIdCharStr.asMdId("test-md-1"))); |
| 168 | } catch (CfmConfigException e) { |
| 169 | fail("Should not have thrown exception: " + e.getMessage()); |
| 170 | } |
| 171 | |
| 172 | //Now try an invalid name |
| 173 | try { |
| 174 | assertFalse(service.deleteMaintenanceDomain( |
| 175 | MdIdCharStr.asMdId("test-md-3"))); |
| 176 | } catch (CfmConfigException e) { |
| 177 | fail("Should not have thrown exception: " + e.getMessage()); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testCreateMaintenanceDomain() throws CfmConfigException { |
| 183 | |
| 184 | MaintenanceAssociation maTest21 = DefaultMaintenanceAssociation |
| 185 | .builder(MaIdCharStr.asMaId("test-ma-2-1"), 9) |
| 186 | .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_1S) |
| 187 | .maNumericId((short) 1) |
| 188 | .addToRemoteMepIdList(MepId.valueOf((short) 101)) |
| 189 | .addToRemoteMepIdList(MepId.valueOf((short) 102)) |
| 190 | .addToComponentList( |
| 191 | DefaultComponent.builder(1) |
| 192 | .tagType(Component.TagType.VLAN_STAG) |
| 193 | .build()) |
| 194 | .build(); |
| 195 | |
| 196 | MaintenanceDomain mdTest2 = DefaultMaintenanceDomain |
| 197 | .builder(MdIdCharStr.asMdId("test-md-2")) |
| 198 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL2) |
| 199 | .mdNumericId((short) 2) |
| 200 | .addToMaList(maTest21) |
| 201 | .build(); |
| 202 | |
| 203 | |
| 204 | //We expect false here because there should have been no previous value |
| 205 | //with that ID in the store |
| 206 | assertFalse(service.createMaintenanceDomain(mdTest2)); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testGetAllMaintenanceAssociation() { |
| 211 | Collection<MaintenanceAssociation> maListMd1 = |
| 212 | service.getAllMaintenanceAssociation( |
| 213 | MdIdCharStr.asMdId("test-md-1")); |
| 214 | |
| 215 | assertEquals(2, maListMd1.size()); |
| 216 | |
| 217 | maListMd1.iterator().forEachRemaining(ma -> |
| 218 | assertTrue(ma.maId().maName().endsWith(String.valueOf(ma.maNumericId()))) |
| 219 | ); |
| 220 | |
| 221 | //Now try with an invalid name |
| 222 | try { |
| 223 | service.getAllMaintenanceAssociation( |
| 224 | MdIdCharStr.asMdId("test-md-2")); |
| 225 | } catch (IllegalArgumentException e) { |
| 226 | assertEquals("Unknown MD test-md-2", e.getMessage()); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | @Test |
| 231 | public void testGetMaintenanceAssociation() { |
| 232 | Optional<MaintenanceAssociation> ma = |
| 233 | service.getMaintenanceAssociation( |
| 234 | MdIdCharStr.asMdId("test-md-1"), |
| 235 | MaIdCharStr.asMaId("test-ma-1-2")); |
| 236 | |
| 237 | assertTrue(ma.isPresent()); |
| 238 | |
| 239 | //Now try an invalid MD Name |
| 240 | try { |
| 241 | service.getMaintenanceAssociation( |
| 242 | MdIdCharStr.asMdId("test-md-2"), |
| 243 | MaIdCharStr.asMaId("test-ma-1-2")); |
| 244 | } catch (IllegalArgumentException e) { |
| 245 | assertEquals("Unknown MD test-md-2", e.getMessage()); |
| 246 | } |
| 247 | |
| 248 | //Now try an invalid MA Name with a valid MD Name |
| 249 | try { |
| 250 | Optional<MaintenanceAssociation> maInvalid = |
| 251 | service.getMaintenanceAssociation( |
| 252 | MdIdCharStr.asMdId("test-md-1"), |
| 253 | MaIdCharStr.asMaId("test-ma-1-3")); |
| 254 | assertFalse(maInvalid.isPresent()); |
| 255 | } catch (IllegalArgumentException e) { |
| 256 | fail("Should not throw exception: " + e.getMessage()); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | @Test |
| 261 | public void testDeleteMaintenanceAssociation() throws CfmConfigException { |
| 262 | assertTrue(service.deleteMaintenanceAssociation( |
| 263 | MdIdCharStr.asMdId("test-md-1"), |
| 264 | MaIdCharStr.asMaId("test-ma-1-2"))); |
| 265 | |
| 266 | //Now check it has actually been removed |
| 267 | Collection<MaintenanceAssociation> maListUpdated = |
| 268 | service.getAllMaintenanceAssociation( |
| 269 | MdIdCharStr.asMdId("test-md-1")); |
| 270 | assertEquals(1, maListUpdated.size()); |
| 271 | maListUpdated.stream().findFirst().ifPresent(ma -> |
| 272 | assertEquals("test-ma-1-1", ma.maId().maName()) |
| 273 | ); |
| 274 | |
| 275 | //Now try with an invalid mdName |
| 276 | try { |
| 277 | service.deleteMaintenanceAssociation( |
| 278 | MdIdCharStr.asMdId("test-md-2"), |
| 279 | MaIdCharStr.asMaId("test-ma-1-2")); |
| 280 | } catch (IllegalArgumentException e) { |
| 281 | assertEquals("Unknown MD: test-md-2", e.getMessage()); |
| 282 | } |
| 283 | |
| 284 | //Now try with an invalid maName |
| 285 | try { |
| 286 | assertFalse(service.deleteMaintenanceAssociation( |
| 287 | MdIdCharStr.asMdId("test-md-1"), |
| 288 | MaIdCharStr.asMaId("test-ma-1-3"))); |
| 289 | } catch (IllegalArgumentException e) { |
| 290 | fail("Should not throw exception: " + e.getMessage()); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | @Test |
| 295 | public void testCreateMaintenanceAssociation() throws CfmConfigException { |
| 296 | MaintenanceAssociation maTest41 = DefaultMaintenanceAssociation |
| 297 | .builder(MaIdCharStr.asMaId("test-ma-1-4"), 9) |
| 298 | .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_1S) |
| 299 | .maNumericId((short) 4) |
| 300 | .addToRemoteMepIdList(MepId.valueOf((short) 401)) |
| 301 | .addToRemoteMepIdList(MepId.valueOf((short) 402)) |
| 302 | .addToComponentList( |
| 303 | DefaultComponent.builder(4) |
| 304 | .tagType(Component.TagType.VLAN_STAG) |
| 305 | .build()) |
| 306 | .build(); |
| 307 | |
| 308 | //Should return false, as this MA did not exist before |
| 309 | assertFalse(service.createMaintenanceAssociation( |
| 310 | MdIdCharStr.asMdId("test-md-1"), maTest41)); |
| 311 | assertEquals(3, service.getAllMaintenanceAssociation( |
| 312 | MdIdCharStr.asMdId("test-md-1")).size()); |
| 313 | |
| 314 | //Now try with an invalid mdName |
| 315 | try { |
| 316 | service.createMaintenanceAssociation( |
| 317 | MdIdCharStr.asMdId("test-md-2"), maTest41); |
| 318 | } catch (IllegalArgumentException e) { |
| 319 | assertEquals("Unknown MD: test-md-2", e.getMessage()); |
| 320 | } |
| 321 | |
| 322 | //Now try replacing an MA with some new values or Remote MEP |
| 323 | MaintenanceAssociation maTest11 = DefaultMaintenanceAssociation |
| 324 | .builder(MaIdCharStr.asMaId("test-ma-1-1"), 9) |
| 325 | .ccmInterval(MaintenanceAssociation.CcmInterval.INTERVAL_10MIN) |
| 326 | .maNumericId((short) 1) |
| 327 | .addToRemoteMepIdList(MepId.valueOf((short) 111)) //Changed |
| 328 | .addToRemoteMepIdList(MepId.valueOf((short) 112)) //Changed |
| 329 | .addToComponentList( |
| 330 | DefaultComponent.builder(1) |
| 331 | .tagType(Component.TagType.VLAN_CTAG) |
| 332 | .build()) |
| 333 | .build(); |
| 334 | |
| 335 | //Should return true, as this MA did exist before |
| 336 | assertTrue(service.createMaintenanceAssociation( |
| 337 | MdIdCharStr.asMdId("test-md-1"), maTest11)); |
| 338 | assertEquals(3, service.getAllMaintenanceAssociation( |
| 339 | MdIdCharStr.asMdId("test-md-1")).size()); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | public class TestApplicationId extends DefaultApplicationId { |
| 344 | public TestApplicationId(int id, String name) { |
| 345 | super(id, name); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | private final class TestClusterService extends ClusterServiceAdapter { |
| 350 | |
| 351 | ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST); |
| 352 | |
| 353 | @Override |
| 354 | public ControllerNode getLocalNode() { |
| 355 | return local; |
| 356 | } |
| 357 | |
| 358 | @Override |
| 359 | public Set<ControllerNode> getNodes() { |
| 360 | return Sets.newHashSet(); |
| 361 | } |
| 362 | |
| 363 | } |
| 364 | |
| 365 | private class TestMastershipService extends MastershipServiceAdapter { |
| 366 | @Override |
| 367 | public NodeId getMasterFor(DeviceId deviceId) { |
| 368 | return NID_LOCAL; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | private class TestCoreService extends CoreServiceAdapter { |
| 373 | |
| 374 | @Override |
| 375 | public IdGenerator getIdGenerator(String topic) { |
| 376 | return new IdGenerator() { |
| 377 | private AtomicLong counter = new AtomicLong(0); |
| 378 | |
| 379 | @Override |
| 380 | public long getNewId() { |
| 381 | return counter.getAndIncrement(); |
| 382 | } |
| 383 | }; |
| 384 | } |
| 385 | } |
| 386 | } |