blob: c00fc4e1aa74840523fd83b37ce49447537b25d8 [file] [log] [blame]
Sean Condon96b896d2017-12-11 12:44:29 -08001/*
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 */
16package org.onosproject.drivers.microsemi.yang;
17
18import org.onlab.junit.TestUtils;
19import org.onlab.packet.ChassisId;
20import org.onosproject.common.event.impl.TestEventDispatcher;
21import org.onosproject.core.CoreServiceAdapter;
22import org.onosproject.core.IdGenerator;
23import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMep;
24import org.onosproject.incubator.net.l2monitoring.cfm.Mep;
25import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MaIdCharStr;
26import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr;
27import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepId;
28import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MepKeyId;
29import org.onosproject.incubator.net.l2monitoring.cfm.impl.CfmMdManager;
30import org.onosproject.incubator.net.l2monitoring.cfm.impl.CfmMepManager;
31import org.onosproject.incubator.net.l2monitoring.cfm.impl.TestCfmMepProgrammable;
32import org.onosproject.incubator.net.l2monitoring.cfm.impl.TestDeviceDiscoveryBehavior;
33import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
34import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMepProgrammable;
35import org.onosproject.incubator.net.l2monitoring.cfm.service.MepStore;
36import org.onosproject.incubator.net.l2monitoring.soam.SoamDmProgrammable;
37import org.onosproject.incubator.net.l2monitoring.soam.impl.TestSoamDmProgrammable;
38import org.onosproject.net.AnnotationKeys;
39import org.onosproject.net.DefaultAnnotations;
40import org.onosproject.net.DefaultDevice;
41import org.onosproject.net.Device;
42import org.onosproject.net.DeviceId;
43import org.onosproject.net.PortNumber;
44import org.onosproject.net.device.DeviceDescriptionDiscovery;
45import org.onosproject.net.device.DeviceService;
46import org.onosproject.net.driver.Behaviour;
47import org.onosproject.net.driver.DefaultDriver;
48import org.onosproject.net.driver.Driver;
49import org.onosproject.net.driver.DriverService;
50import org.onosproject.net.provider.ProviderId;
51
52import java.util.ArrayList;
53import java.util.HashMap;
54import java.util.Map;
55import java.util.Optional;
56import java.util.concurrent.atomic.AtomicLong;
57
58import static org.easymock.EasyMock.createMock;
59import static org.easymock.EasyMock.expect;
60import static org.onosproject.net.NetTestTools.injectEventDispatcher;
61
62/**
63 * Supports testing of services that reply on the CfmMepService.
64 */
65public 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}