2 * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.genius.interfacemanager.test;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Singleton;
14 import org.mockito.Mockito;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.controller.md.sal.binding.test.DataBrokerTestModule;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
24 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
25 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
26 import org.opendaylight.genius.idmanager.IdManager;
27 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
28 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
29 import org.opendaylight.genius.interfacemanager.test.infra.TestEntityOwnershipService;
30 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
31 import org.opendaylight.genius.mdsalutil.interfaces.testutils.TestIMdsalApiManager;
32 import org.opendaylight.infrautils.inject.guice.testutils.AbstractGuiceJsr250Module;
33 import org.opendaylight.lockmanager.LockManager;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.impl.rev160406.InterfacemgrImplModule;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.RpcService;
44 * Dependency Injection Wiring for {@link InterfaceManagerTest}.
46 * <p>This class looks a little bit more complicated than it could and later will be
47 * just because interfacemanager is still using CSS instead of BP with @Inject.
49 * <p>Please DO NOT copy/paste this class as-is into other projects; this is intended
50 * to be temporary, until interfacemanager is switch from CSS to BP.
52 * <p>For "proper" *Module examples, please see the AclServiceModule and
53 * AclServiceTestModule or ElanServiceTestModule instead.
55 * @author Michael Vorburger
57 @SuppressWarnings("deprecation")
58 public class InterfaceManagerTestModule extends AbstractGuiceJsr250Module {
61 protected void configureBindings() {
62 // TODO Ordering as below.. hard to do currently, because of interdeps. due to CSS
63 // Bindings for services from this project
64 // Bindings for external services to "real" implementations
65 // Bindings to test infra (fakes & mocks)
67 DataBroker dataBroker = DataBrokerTestModule.dataBroker();
68 bind(DataBroker.class).toInstance(dataBroker);
70 LockManagerService lockManager = new LockManager(dataBroker);
71 bind(LockManagerService.class).toInstance(lockManager);
73 IdManagerService idManager = new IdManager(dataBroker, lockManager);
74 bind(IdManagerService.class).toInstance(idManager);
76 TestIMdsalApiManager mdsalManager = TestIMdsalApiManager.newInstance();
77 bind(IMdsalApiManager.class).toInstance(mdsalManager);
78 bind(TestIMdsalApiManager.class).toInstance(mdsalManager);
80 EntityOwnershipService entityOwnershipService = TestEntityOwnershipService.newInstance();
81 bind(EntityOwnershipService.class).toInstance(entityOwnershipService);
83 // Temporary bindings which normally would be on top, but cauz of CSS vs BP are here, for now:
84 InterfacemgrProvider interfaceManager = interfaceManager(mdsalManager, entityOwnershipService, dataBroker, idManager);
85 bind(IInterfaceManager.class).toInstance(interfaceManager);
86 bind(Stopper.class).toInstance(new Stopper(interfaceManager));
90 private static class Stopper {
91 final InterfacemgrProvider interfaceManager;
93 Stopper(InterfacemgrProvider interfaceManager) {
94 this.interfaceManager = interfaceManager;
98 void close() throws Exception {
99 interfaceManager.close();
104 * This method duplicates the logic in {@link InterfacemgrImplModule#createInstance()}.
105 * This isn't ideal, but as interface-manager will hopefully soon be converted from CSS to BP,
106 * at which point this can be simplified to be based on @Inject etc. just like e.g. the AclServiceModule
107 * and AclServiceTestModule or ElanServiceTestModule, we do it like this, for now.
109 private InterfacemgrProvider interfaceManager(IMdsalApiManager mdsalManager,
110 EntityOwnershipService entityOwnershipService, DataBroker dataBroker, IdManagerService idManager) {
112 InterfacemgrProvider provider = new InterfacemgrProvider();
114 RpcProviderRegistry rpcProviderRegistry = mock(RpcProviderRegistry.class /* TODO how-to? exception() */);
115 when(rpcProviderRegistry.getRpcService(IdManagerService.class)).thenReturn(idManager);
117 provider.setRpcProviderRegistry(rpcProviderRegistry);
118 provider.setMdsalManager(mdsalManager);
119 provider.setEntityOwnershipService(entityOwnershipService);
120 provider.setNotificationService(mock(NotificationService.class));
122 // TODO just use rpcProviderRegistry, which IS-A ProviderContext here?
123 ProviderContext session = new TestProviderContext(dataBroker);
124 provider.onSessionInitiated(session);
129 static class TestProviderContext implements ProviderContext {
131 private final DataBroker dataBroker;
133 TestProviderContext(DataBroker dataBroker) {
134 this.dataBroker = dataBroker;
138 @SuppressWarnings("unchecked")
139 public <T extends BindingAwareService> T getSALService(Class<T> service) {
140 if (service.equals(DataBroker.class)) {
141 return (T) dataBroker;
143 throw new UnsupportedOperationException(service.toGenericString());
148 @SuppressWarnings("unchecked")
149 public <T extends RpcService> T getRpcService(Class<T> serviceInterface) {
150 if (serviceInterface.equals(OpendaylightPortStatisticsService.class)) {
151 return (T) mock(MockOpendaylightPortStatisticsRpcService.class);
152 } else if (serviceInterface.equals(OpendaylightFlowTableStatisticsService.class)) {
153 return (T) mock(MockOpendaylightFlowTableStatisticsRpcService.class);
155 throw new UnsupportedOperationException(serviceInterface.toGenericString());
160 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> serviceInterface,
161 T implementation) throws IllegalStateException {
162 throw new UnsupportedOperationException();
166 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> serviceInterface,
167 T implementation) throws IllegalStateException {
168 throw new UnsupportedOperationException();
172 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
173 registerRouteChangeListener(L listener) {
174 throw new UnsupportedOperationException();
179 interface MockOpendaylightPortStatisticsRpcService extends OpendaylightPortStatisticsService, RpcService { }
181 interface MockOpendaylightFlowTableStatisticsRpcService extends OpendaylightFlowTableStatisticsService, RpcService { }