Sean Condon | 96b896d | 2017-12-11 12:44:29 -0800 | [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.drivers.microsemi.yang; |
| 17 | |
| 18 | import org.onlab.junit.TestUtils; |
| 19 | import org.onlab.packet.ChassisId; |
| 20 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 21 | import org.onosproject.core.CoreServiceAdapter; |
| 22 | import org.onosproject.core.IdGenerator; |
| 23 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep; |
| 24 | import org.onosproject.incubator.net.l2monitoring.cfm.Mep; |
| 25 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr; |
| 26 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepKeyId; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.impl.CfmMdManager; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.impl.CfmMepManager; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.impl.TestCfmMepProgrammable; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.impl.TestDeviceDiscoveryBehavior; |
| 33 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 34 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepProgrammable; |
| 35 | import org.onosproject.incubator.net.l2monitoring.cfm.service.MepStore; |
| 36 | import org.onosproject.incubator.net.l2monitoring.soam.SoamDmProgrammable; |
| 37 | import org.onosproject.incubator.net.l2monitoring.soam.impl.TestSoamDmProgrammable; |
| 38 | import org.onosproject.net.AnnotationKeys; |
| 39 | import org.onosproject.net.DefaultAnnotations; |
| 40 | import org.onosproject.net.DefaultDevice; |
| 41 | import org.onosproject.net.Device; |
| 42 | import org.onosproject.net.DeviceId; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 45 | import org.onosproject.net.device.DeviceService; |
| 46 | import org.onosproject.net.driver.Behaviour; |
| 47 | import org.onosproject.net.driver.DefaultDriver; |
| 48 | import org.onosproject.net.driver.Driver; |
| 49 | import org.onosproject.net.driver.DriverService; |
| 50 | import org.onosproject.net.provider.ProviderId; |
| 51 | |
| 52 | import java.util.ArrayList; |
| 53 | import java.util.HashMap; |
| 54 | import java.util.Map; |
| 55 | import java.util.Optional; |
| 56 | import java.util.concurrent.atomic.AtomicLong; |
| 57 | |
| 58 | import static org.easymock.EasyMock.createMock; |
| 59 | import static org.easymock.EasyMock.expect; |
| 60 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
| 61 | |
| 62 | /** |
| 63 | * Supports testing of services that reply on the CfmMepService. |
| 64 | */ |
| 65 | public class MockCfmMepService extends CfmMepManager { |
| 66 | private static final String TEST_MFR = "testMfr"; |
| 67 | private static final String TEST_HW_VERSION = "testHwVersion"; |
| 68 | private static final String TEST_SW_VERSION = "testSwVersion"; |
| 69 | private static final String TEST_SN = "testSn"; |
| 70 | private static final String TEST_DRIVER = "testDriver"; |
| 71 | protected static final DeviceId DEVICE_ID1 = DeviceId.deviceId("netconf:1.2.3.4:830"); |
| 72 | |
| 73 | |
| 74 | private final DriverService driverService = createMock(DriverService.class); |
| 75 | |
| 76 | private Device device1; |
| 77 | private Driver testDriver; |
| 78 | |
| 79 | |
| 80 | @Override |
| 81 | public void activate() { |
| 82 | mepStore = createMock(MepStore.class); |
| 83 | cfmMdService = new MockCfmMdService(); |
| 84 | deviceService = createMock(DeviceService.class); |
| 85 | ((CfmMdManager) cfmMdService).activate(); |
| 86 | |
| 87 | device1 = new DefaultDevice( |
| 88 | ProviderId.NONE, DEVICE_ID1, Device.Type.SWITCH, |
| 89 | TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION, TEST_SN, |
| 90 | new ChassisId(1), |
| 91 | DefaultAnnotations.builder().set(AnnotationKeys.DRIVER, TEST_DRIVER).build()); |
| 92 | |
| 93 | Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours = new HashMap<>(); |
| 94 | behaviours.put(DeviceDescriptionDiscovery.class, TestDeviceDiscoveryBehavior.class); |
| 95 | behaviours.put(CfmMepProgrammable.class, TestCfmMepProgrammable.class); |
| 96 | behaviours.put(SoamDmProgrammable.class, TestSoamDmProgrammable.class); |
| 97 | |
| 98 | TestUtils.setField(this, "coreService", new TestCoreService()); |
| 99 | TestUtils.setField(this, "deviceService", deviceService); |
| 100 | injectEventDispatcher(this, new TestEventDispatcher()); |
| 101 | |
| 102 | testDriver = new DefaultDriver( |
| 103 | TEST_DRIVER, new ArrayList<Driver>(), |
| 104 | TEST_MFR, TEST_HW_VERSION, TEST_SW_VERSION, |
| 105 | behaviours, new HashMap<>()); |
| 106 | |
| 107 | try { |
| 108 | Mep mep1 = DefaultMep.builder( |
| 109 | MepId.valueOf((short) 10), |
| 110 | DEVICE_ID1, |
| 111 | PortNumber.P0, |
| 112 | Mep.MepDirection.UP_MEP, |
| 113 | MdIdCharStr.asMdId("md-1"), |
| 114 | MaIdCharStr.asMaId("ma-1-1")) |
| 115 | .build(); |
| 116 | |
| 117 | expect(mepStore.getMep(new MepKeyId(mep1))).andReturn(Optional.of(mep1)).anyTimes(); |
| 118 | } catch (CfmConfigException e) { |
| 119 | throw new IllegalArgumentException("Error creating MEPs for test", e); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | private class TestCoreService extends CoreServiceAdapter { |
| 124 | |
| 125 | @Override |
| 126 | public IdGenerator getIdGenerator(String topic) { |
| 127 | return new IdGenerator() { |
| 128 | private AtomicLong counter = new AtomicLong(0); |
| 129 | |
| 130 | @Override |
| 131 | public long getNewId() { |
| 132 | return counter.getAndIncrement(); |
| 133 | } |
| 134 | }; |
| 135 | } |
| 136 | } |
| 137 | } |