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 org.junit.After; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.junit.TestUtils; |
| 22 | import org.onlab.packet.ChassisId; |
| 23 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 24 | import org.onosproject.core.CoreServiceAdapter; |
| 25 | import org.onosproject.core.IdGenerator; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceAssociation; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepLbCreate; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMepLtCreate; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceAssociation; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.MepEntry; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.MepLbCreate; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.MepLtCreate; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 36 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdShort; |
| 37 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 38 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 39 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 40 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 41 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 42 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepProgrammable; |
| 43 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepService; |
| 44 | import org.onosproject.incubator.net.l2monitoring.soam.SoamDmProgrammable; |
| 45 | import org.onosproject.incubator.net.l2monitoring.soam.impl.TestSoamDmProgrammable; |
| 46 | import org.onosproject.net.AbstractProjectableModel; |
| 47 | import org.onosproject.net.AnnotationKeys; |
| 48 | import org.onosproject.net.DefaultAnnotations; |
| 49 | import org.onosproject.net.DefaultDevice; |
| 50 | import org.onosproject.net.Device; |
| 51 | import org.onosproject.net.DeviceId; |
| 52 | import org.onosproject.net.PortNumber; |
| 53 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 54 | import org.onosproject.net.device.DeviceService; |
| 55 | import org.onosproject.net.driver.Behaviour; |
| 56 | import org.onosproject.net.driver.DefaultDriver; |
| 57 | import org.onosproject.net.driver.Driver; |
| 58 | import org.onosproject.net.driver.DriverService; |
| 59 | import org.onosproject.net.provider.ProviderId; |
| 60 | |
| 61 | import java.util.ArrayList; |
| 62 | import java.util.Collection; |
| 63 | import java.util.HashMap; |
| 64 | import java.util.List; |
| 65 | import java.util.Map; |
| 66 | import java.util.Optional; |
| 67 | import java.util.concurrent.atomic.AtomicLong; |
| 68 | |
| 69 | import static junit.framework.TestCase.fail; |
| 70 | import static org.easymock.EasyMock.createMock; |
| 71 | import static org.easymock.EasyMock.expect; |
| 72 | import static org.easymock.EasyMock.replay; |
| 73 | import static org.junit.Assert.assertEquals; |
| 74 | import static org.junit.Assert.assertTrue; |
| 75 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 76 | |
| 77 | /** |
| 78 | * CFM MEP Manager test. |
| 79 | */ |
| 80 | public class CfmMepManagerTest { |
| 81 | private static final String TEST_MFR = "testMfr"; |
| 82 | private static final String TEST_HW_VERSION = "testHwVersion"; |
| 83 | private static final String TEST_SW_VERSION = "testSwVersion"; |
| 84 | private static final String TEST_SN = "testSn"; |
| 85 | private static final String TEST_DRIVER = "testDriver"; |
| 86 | public static final String TEST_DRIVER_3 = "testDriver3"; |
| 87 | public static final String TEST_SW_3 = "testSw3"; |
| 88 | private final CfmMdService mdService = createMock(CfmMdService.class); |
| 89 | private final DeviceService deviceService = createMock(DeviceService.class); |
| 90 | private final DriverService driverService = createMock(DriverService.class); |
| 91 | |
| 92 | private CfmMepService mepService; |
| 93 | private CfmMepManager mepManager; |
| 94 | |
| 95 | protected static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1"); |
| 96 | protected static final MaIdShort MANAME1 = MaIdCharStr.asMaId("ma-1-1"); |
| 97 | |
| 98 | private MaintenanceAssociation ma1; |
| 99 | protected static final MepId MEPID1 = MepId.valueOf((short) 10); |
| 100 | protected static final MepId MEPID2 = MepId.valueOf((short) 20); |
| 101 | protected static final DeviceId DEVICE_ID1 = DeviceId.deviceId("netconf:1.2.3.4:830"); |
| 102 | protected static final DeviceId DEVICE_ID2 = DeviceId.deviceId("netconf:2.2.3.4:830"); |
| 103 | |
| 104 | private Mep mep1; |
| 105 | private Mep mep2; |
| 106 | |
| 107 | private Device device1; |
| 108 | private Device device2; |
| 109 | |
| 110 | private Driver testDriver; |
| 111 | |
| 112 | @Before |
| 113 | public void setup() throws CfmConfigException { |
| 114 | mepManager = new CfmMepManager(); |
| 115 | |
| 116 | ma1 = DefaultMaintenanceAssociation.builder(MANAME1, MDNAME1.getNameLength()).build(); |
| 117 | |
| 118 | TestUtils.setField(mepManager, "coreService", new TestCoreService()); |
| 119 | TestUtils.setField(mepManager, "deviceService", deviceService); |
| 120 | TestUtils.setField(mepManager, "cfmMdService", mdService); |
| 121 | injectEventDispatcher(mepManager, new TestEventDispatcher()); |
| 122 | |
| 123 | mepService = mepManager; |
| 124 | mepManager.activate(); |
| 125 | |
| 126 | mep1 = DefaultMep.builder(MEPID1, DEVICE_ID1, PortNumber.P0, |
| 127 | Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build(); |
| 128 | mep2 = DefaultMep.builder(MEPID2, DEVICE_ID2, PortNumber.portNumber(2), |
| 129 | Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build(); |
| 130 | List<Mep> mepList = new ArrayList<>(); |
| 131 | mepList.add(mep1); |
| 132 | mepList.add(mep2); |
| 133 | TestUtils.setField(mepManager, "mepCollection", mepList); |
| 134 | |
| 135 | device1 = new DefaultDevice( |
| 136 | ProviderId.NONE, DEVICE_ID1, Device.Type.SWITCH, |
| 137 | TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION, TEST_SN, |
| 138 | new ChassisId(1), |
| 139 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, TEST_DRIVER).build()); |
| 140 | |
| 141 | device2 = new DefaultDevice( |
| 142 | ProviderId.NONE, DEVICE_ID2, Device.Type.SWITCH, |
| 143 | TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION, TEST_SN, |
| 144 | new ChassisId(2), |
| 145 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, TEST_DRIVER).build()); |
| 146 | |
| 147 | AbstractProjectableModel.setDriverService(null, driverService); |
| 148 | |
| 149 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = new HashMap<>(); |
| 150 | behaviours.put(DeviceDescriptionDiscovery.class, TestDeviceDiscoveryBehavior.class); |
| 151 | behaviours.put(CfmMepProgrammable.class, TestCfmMepProgrammable.class); |
| 152 | behaviours.put(SoamDmProgrammable.class, TestSoamDmProgrammable.class); |
| 153 | |
| 154 | testDriver = new DefaultDriver( |
| 155 | TEST_DRIVER, new ArrayList<Driver>(), |
| 156 | TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION, |
| 157 | behaviours, new HashMap<>()); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | @After |
| 162 | public void tearDown() { |
| 163 | // mepManager.deactivate(); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testGetAllMeps() throws CfmConfigException { |
| 168 | |
| 169 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 170 | .andReturn(Optional.ofNullable(ma1)) |
| 171 | .anyTimes(); |
| 172 | replay(mdService); |
| 173 | |
| 174 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes(); |
| 175 | expect(deviceService.getDevice(DEVICE_ID2)).andReturn(device2).anyTimes(); |
| 176 | replay(deviceService); |
| 177 | |
| 178 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 179 | replay(driverService); |
| 180 | |
| 181 | Collection<MepEntry> mepEntries = mepManager.getAllMeps(MDNAME1, MANAME1); |
| 182 | |
| 183 | assertEquals(2, mepEntries.size()); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | public void testGetMep() throws CfmConfigException { |
| 188 | |
| 189 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 190 | .andReturn(Optional.ofNullable(ma1)) |
| 191 | .anyTimes(); |
| 192 | replay(mdService); |
| 193 | |
| 194 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes(); |
| 195 | replay(deviceService); |
| 196 | |
| 197 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 198 | replay(driverService); |
| 199 | |
| 200 | MepEntry mepEntry = mepManager.getMep(MDNAME1, MANAME1, MEPID1); |
| 201 | |
| 202 | assertEquals(MEPID1.value(), mepEntry.mepId().value()); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void testGetMepMissing() { |
| 207 | |
| 208 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 209 | .andReturn(Optional.ofNullable(ma1)) |
| 210 | .anyTimes(); |
| 211 | replay(mdService); |
| 212 | |
| 213 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(null).anyTimes(); |
| 214 | replay(deviceService); |
| 215 | |
| 216 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 217 | replay(driverService); |
| 218 | |
| 219 | try { |
| 220 | mepManager.getMep(MDNAME1, MANAME1, MEPID1); |
| 221 | fail("Expecting CfmConfigException because device does not exist"); |
| 222 | } catch (CfmConfigException e) { |
| 223 | assertEquals("Device not found netconf:1.2.3.4:830", e.getMessage()); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | @Test |
| 228 | public void testDeleteMep() throws CfmConfigException { |
| 229 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 230 | .andReturn(Optional.ofNullable(ma1)) |
| 231 | .anyTimes(); |
| 232 | replay(mdService); |
| 233 | |
| 234 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes(); |
| 235 | replay(deviceService); |
| 236 | |
| 237 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 238 | replay(driverService); |
| 239 | |
| 240 | assertTrue(mepManager.deleteMep(MDNAME1, MANAME1, MEPID1)); |
| 241 | } |
| 242 | |
| 243 | @Test |
| 244 | public void testCreateMep() throws CfmConfigException { |
| 245 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 246 | .andReturn(Optional.ofNullable(ma1)) |
| 247 | .anyTimes(); |
| 248 | replay(mdService); |
| 249 | |
| 250 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes(); |
| 251 | replay(deviceService); |
| 252 | |
| 253 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 254 | replay(driverService); |
| 255 | |
| 256 | MepId mepId3 = MepId.valueOf((short) 3); |
| 257 | Mep mep3 = DefaultMep.builder(mepId3, DEVICE_ID1, PortNumber.portNumber(1), |
| 258 | Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build(); |
| 259 | |
| 260 | assertTrue(mepManager.createMep(MDNAME1, MANAME1, mep3)); |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | public void testCreateMepBehaviorNotSupported() throws CfmConfigException { |
| 265 | final DeviceId deviceId3 = DeviceId.deviceId("netconf:3.2.3.4:830"); |
| 266 | |
| 267 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = new HashMap<>(); |
| 268 | behaviours.put(DeviceDescriptionDiscovery.class, TestDeviceDiscoveryBehavior.class); |
| 269 | |
| 270 | Driver testDriver3 = new DefaultDriver( |
| 271 | TEST_DRIVER_3, new ArrayList<Driver>(), |
| 272 | TEST_MFR, TEST_HW_VERSION, TEST_SW_3, |
| 273 | behaviours, new HashMap<>()); |
| 274 | |
| 275 | Device device3 = new DefaultDevice( |
| 276 | ProviderId.NONE, deviceId3, Device.Type.SWITCH, |
| 277 | TEST_MFR, TEST_HW_VERSION, TEST_SW_3, TEST_SN, |
| 278 | new ChassisId(2), |
| 279 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, TEST_DRIVER_3).build()); |
| 280 | |
| 281 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 282 | .andReturn(Optional.ofNullable(ma1)) |
| 283 | .anyTimes(); |
| 284 | replay(mdService); |
| 285 | |
| 286 | expect(deviceService.getDevice(deviceId3)).andReturn(device3).anyTimes(); |
| 287 | replay(deviceService); |
| 288 | |
| 289 | expect(driverService.getDriver(TEST_DRIVER_3)).andReturn(testDriver3).anyTimes(); |
| 290 | replay(driverService); |
| 291 | |
| 292 | MepId mepId3 = MepId.valueOf((short) 3); |
| 293 | Mep mep3 = DefaultMep.builder(mepId3, deviceId3, PortNumber.portNumber(1), |
| 294 | Mep.MepDirection.UP_MEP, MDNAME1, MANAME1).build(); |
| 295 | |
| 296 | try { |
| 297 | mepManager.createMep(MDNAME1, MANAME1, mep3); |
| 298 | fail("Expecting CfmConfigException because driver does not support behavior"); |
| 299 | } catch (CfmConfigException e) { |
| 300 | assertEquals("Device netconf:3.2.3.4:830 does not support " + |
| 301 | "CfmMepProgrammable behaviour.", e.getMessage()); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | @Test |
| 306 | public void testTransmitLoopback() { |
| 307 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 308 | .andReturn(Optional.ofNullable(ma1)) |
| 309 | .anyTimes(); |
| 310 | replay(mdService); |
| 311 | |
| 312 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes(); |
| 313 | replay(deviceService); |
| 314 | |
| 315 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 316 | replay(driverService); |
| 317 | |
| 318 | MepLbCreate lbCreate = DefaultMepLbCreate.builder(MepId.valueOf((short) 11)).build(); |
| 319 | try { |
| 320 | mepService.transmitLoopback(MDNAME1, MANAME1, MEPID1, lbCreate); |
| 321 | } catch (CfmConfigException e) { |
| 322 | fail("Not expecting an exception"); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | @Test |
| 327 | public void testAbortLoopback() { |
| 328 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 329 | .andReturn(Optional.ofNullable(ma1)) |
| 330 | .anyTimes(); |
| 331 | replay(mdService); |
| 332 | |
| 333 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes(); |
| 334 | replay(deviceService); |
| 335 | |
| 336 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 337 | replay(driverService); |
| 338 | |
| 339 | try { |
| 340 | mepService.abortLoopback(MDNAME1, MANAME1, MEPID1); |
| 341 | } catch (CfmConfigException e) { |
| 342 | fail("Not expecting an exception"); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | @Test |
| 347 | public void testTransmitLinktrace() throws CfmConfigException { |
| 348 | expect(mdService.getMaintenanceAssociation(MDNAME1, MANAME1)) |
| 349 | .andReturn(Optional.ofNullable(ma1)) |
| 350 | .anyTimes(); |
| 351 | replay(mdService); |
| 352 | |
| 353 | expect(deviceService.getDevice(DEVICE_ID1)).andReturn(device1).anyTimes(); |
| 354 | replay(deviceService); |
| 355 | |
| 356 | expect(driverService.getDriver(TEST_DRIVER)).andReturn(testDriver).anyTimes(); |
| 357 | replay(driverService); |
| 358 | |
| 359 | MepLtCreate ltCreate = DefaultMepLtCreate.builder(MepId.valueOf((short) 11)).build(); |
| 360 | try { |
| 361 | mepService.transmitLinktrace(MDNAME1, MANAME1, MEPID1, ltCreate); |
| 362 | } catch (UnsupportedOperationException e) { |
| 363 | assertEquals("Not yet implemented", e.getMessage()); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | private class TestCoreService extends CoreServiceAdapter { |
| 368 | |
| 369 | @Override |
| 370 | public IdGenerator getIdGenerator(String topic) { |
| 371 | return new IdGenerator() { |
| 372 | private AtomicLong counter = new AtomicLong(0); |
| 373 | |
| 374 | @Override |
| 375 | public long getNewId() { |
| 376 | return counter.getAndIncrement(); |
| 377 | } |
| 378 | }; |
| 379 | } |
| 380 | } |
| 381 | } |