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;
13 import javax.annotation.PreDestroy;
14 import javax.inject.Singleton;
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.data.ReadFailedException;
20 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
25 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
26 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
27 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
28 import org.opendaylight.genius.idmanager.IdManager;
29 import org.opendaylight.genius.idmanager.IdUtils;
30 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
31 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
32 import org.opendaylight.genius.interfacemanager.test.infra.TestEntityOwnershipService;
33 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
34 import org.opendaylight.genius.mdsalutil.interfaces.testutils.TestIMdsalApiManager;
35 import org.opendaylight.infrautils.inject.ModuleSetupRuntimeException;
36 import org.opendaylight.infrautils.inject.guice.testutils.AbstractGuiceJsr250Module;
37 import org.opendaylight.lockmanager.LockManager;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.table.statistics.rev131215.OpendaylightFlowTableStatisticsService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.impl.rev160406.InterfacemgrImplModule;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.OpendaylightPortStatisticsService;
43 import org.opendaylight.yangtools.concepts.ListenerRegistration;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.RpcService;
48 * Dependency Injection Wiring for {@link InterfaceManagerConfigurationTest}.
50 * <p>This class looks a little bit more complicated than it could and later will be
51 * just because interfacemanager is still using CSS instead of BP with @Inject.
53 * <p>Please DO NOT copy/paste this class as-is into other projects; this is intended
54 * to be temporary, until interfacemanager is switch from CSS to BP.
56 * <p>For "proper" *Module examples, please see the AclServiceModule and
57 * AclServiceTestModule or ElanServiceTestModule instead.
59 * @author Michael Vorburger
61 public class InterfaceManagerTestModule extends AbstractGuiceJsr250Module {
64 protected void configureBindings() throws Exception {
65 // TODO Ordering as below.. hard to do currently, because of interdeps. due to CSS
66 // Bindings for services from this project
67 // Bindings for external services to "real" implementations
68 // Bindings to test infra (fakes & mocks)
70 DataBroker dataBroker = DataBrokerTestModule.dataBroker();
71 bind(DataBroker.class).toInstance(dataBroker);
73 LockManagerService lockManager = new LockManager(dataBroker);
74 bind(LockManagerService.class).toInstance(lockManager);
76 IdUtils idUtils = new IdUtils();
77 IdManagerService idManager;
79 idManager = new IdManager(dataBroker,
80 new SingleTransactionDataBroker(dataBroker), lockManager, idUtils);
81 } catch (ReadFailedException e) {
82 // TODO Support AbstractGuiceJsr250Module
83 throw new ModuleSetupRuntimeException(e);
85 bind(IdManagerService.class).toInstance(idManager);
87 TestIMdsalApiManager mdsalManager = TestIMdsalApiManager.newInstance();
88 bind(IMdsalApiManager.class).toInstance(mdsalManager);
89 bind(TestIMdsalApiManager.class).toInstance(mdsalManager);
91 EntityOwnershipService entityOwnershipService = TestEntityOwnershipService.newInstance();
92 bind(EntityOwnershipService.class).toInstance(entityOwnershipService);
94 // Temporary bindings which normally would be on top, but cauz of CSS vs BP are here, for now:
95 InterfacemgrProvider interfaceManager = interfaceManager(mdsalManager, entityOwnershipService, dataBroker,
97 bind(IInterfaceManager.class).toInstance(interfaceManager);
98 bind(Stopper.class).toInstance(new Stopper(interfaceManager));
102 private static class Stopper {
103 final InterfacemgrProvider interfaceManager;
105 Stopper(InterfacemgrProvider interfaceManager) {
106 this.interfaceManager = interfaceManager;
110 void close() throws Exception {
111 interfaceManager.close();
116 * This method duplicates the logic in {@link InterfacemgrImplModule#createInstance()}.
117 * This isn't ideal, but as interface-manager will hopefully soon be converted from CSS to BP,
118 * at which point this can be simplified to be based on @Inject etc. just like e.g. the AclServiceModule
119 * and AclServiceTestModule or ElanServiceTestModule, we do it like this, for now.
121 private InterfacemgrProvider interfaceManager(IMdsalApiManager mdsalManager,
122 EntityOwnershipService entityOwnershipService, DataBroker dataBroker, IdManagerService idManager) {
124 InterfacemgrProvider provider = new InterfacemgrProvider();
126 RpcProviderRegistry rpcProviderRegistry = mock(RpcProviderRegistry.class /* TODO how-to? exception() */);
127 when(rpcProviderRegistry.getRpcService(IdManagerService.class)).thenReturn(idManager);
129 provider.setRpcProviderRegistry(rpcProviderRegistry);
130 provider.setMdsalManager(mdsalManager);
131 provider.setEntityOwnershipService(entityOwnershipService);
132 provider.setNotificationService(mock(NotificationService.class));
134 // TODO just use rpcProviderRegistry, which IS-A ProviderContext here?
135 ProviderContext session = new TestProviderContext(dataBroker);
136 provider.onSessionInitiated(session);
141 static class TestProviderContext implements ProviderContext {
143 private final DataBroker dataBroker;
145 TestProviderContext(DataBroker dataBroker) {
146 this.dataBroker = dataBroker;
150 @SuppressWarnings("unchecked")
151 public <T extends BindingAwareService> T getSALService(Class<T> service) {
152 if (service.equals(DataBroker.class)) {
153 return (T) dataBroker;
155 throw new UnsupportedOperationException(service.toGenericString());
160 @SuppressWarnings("unchecked")
161 public <T extends RpcService> T getRpcService(Class<T> serviceInterface) {
162 if (serviceInterface.equals(OpendaylightPortStatisticsService.class)) {
163 return (T) mock(MockOpendaylightPortStatisticsRpcService.class);
164 } else if (serviceInterface.equals(OpendaylightFlowTableStatisticsService.class)) {
165 return (T) mock(MockOpendaylightFlowTableStatisticsRpcService.class);
167 throw new UnsupportedOperationException(serviceInterface.toGenericString());
172 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> serviceInterface,
173 T implementation) throws IllegalStateException {
174 throw new UnsupportedOperationException();
178 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> serviceInterface,
179 T implementation) throws IllegalStateException {
180 throw new UnsupportedOperationException();
184 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
185 registerRouteChangeListener(L listener) {
186 throw new UnsupportedOperationException();
191 interface MockOpendaylightPortStatisticsRpcService extends OpendaylightPortStatisticsService, RpcService {
194 interface MockOpendaylightFlowTableStatisticsRpcService extends OpendaylightFlowTableStatisticsService, RpcService {