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 java.net.UnknownHostException;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
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.idmanager.IdManager;
28 import org.opendaylight.genius.idmanager.IdUtils;
29 import org.opendaylight.genius.interfacemanager.InterfacemgrProvider;
30 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
31 import org.opendaylight.genius.interfacemanager.test.infra.TemporaryDataBrokerTestModuleWithLocalFixForBug7538;
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;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Dependency Injection Wiring for {@link InterfaceManagerConfigurationTest}.
52 * <p>This class looks a little bit more complicated than it could and later will be
53 * just because interfacemanager is still using CSS instead of BP with @Inject.
55 * <p>Please DO NOT copy/paste this class as-is into other projects; this is intended
56 * to be temporary, until interfacemanager is switch from CSS to BP.
58 * <p>For "proper" *Module examples, please see the AclServiceModule and
59 * AclServiceTestModule or ElanServiceTestModule instead.
61 * @author Michael Vorburger
63 public class InterfaceManagerTestModule extends AbstractGuiceJsr250Module {
65 private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerTestModule.class);
68 protected void configureBindings() throws UnknownHostException {
69 // TODO Ordering as below.. hard to do currently, because of interdeps. due to CSS
70 // Bindings for services from this project
71 // Bindings for external services to "real" implementations
72 // Bindings to test infra (fakes & mocks)
74 DataBroker dataBroker = TemporaryDataBrokerTestModuleWithLocalFixForBug7538.dataBroker();
75 bind(DataBroker.class).toInstance(dataBroker);
77 LockManagerService lockManager = new LockManager(dataBroker);
78 bind(LockManagerService.class).toInstance(lockManager);
80 IdUtils idUtils = new IdUtils();
81 IdManagerService idManager;
83 idManager = new IdManager(dataBroker, lockManager, idUtils);
84 } catch (ReadFailedException e) {
85 // TODO Support AbstractGuiceJsr250Module
86 throw new ModuleSetupRuntimeException(e);
88 bind(IdManagerService.class).toInstance(idManager);
90 TestIMdsalApiManager mdsalManager = TestIMdsalApiManager.newInstance();
91 bind(IMdsalApiManager.class).toInstance(mdsalManager);
92 bind(TestIMdsalApiManager.class).toInstance(mdsalManager);
94 EntityOwnershipService entityOwnershipService = TestEntityOwnershipService.newInstance();
95 bind(EntityOwnershipService.class).toInstance(entityOwnershipService);
97 // Temporary bindings which normally would be on top, but cauz of CSS vs BP are here, for now:
98 InterfacemgrProvider interfaceManager = interfaceManager(mdsalManager, entityOwnershipService, dataBroker,
100 bind(IInterfaceManager.class).toInstance(interfaceManager);
101 bind(Stopper.class).toInstance(new Stopper(interfaceManager));
105 private static class Stopper {
106 final InterfacemgrProvider interfaceManager;
108 Stopper(InterfacemgrProvider interfaceManager) {
109 this.interfaceManager = interfaceManager;
113 @SuppressWarnings("checkstyle:IllegalCatch")
116 interfaceManager.close();
117 } catch (Exception e) {
118 LOG.error("close() failed", e);
124 * This method duplicates the logic in {@link InterfacemgrImplModule#createInstance()}.
125 * This isn't ideal, but as interface-manager will hopefully soon be converted from CSS to BP,
126 * at which point this can be simplified to be based on @Inject etc. just like e.g. the AclServiceModule
127 * and AclServiceTestModule or ElanServiceTestModule, we do it like this, for now.
129 private InterfacemgrProvider interfaceManager(IMdsalApiManager mdsalManager,
130 EntityOwnershipService entityOwnershipService, DataBroker dataBroker, IdManagerService idManager) {
132 InterfacemgrProvider provider = new InterfacemgrProvider();
134 RpcProviderRegistry rpcProviderRegistry = mock(RpcProviderRegistry.class /* TODO how-to? exception() */);
135 when(rpcProviderRegistry.getRpcService(IdManagerService.class)).thenReturn(idManager);
137 provider.setRpcProviderRegistry(rpcProviderRegistry);
138 provider.setMdsalManager(mdsalManager);
139 provider.setEntityOwnershipService(entityOwnershipService);
140 provider.setNotificationService(mock(NotificationService.class));
142 // TODO just use rpcProviderRegistry, which IS-A ProviderContext here?
143 ProviderContext session = new TestProviderContext(dataBroker);
144 provider.onSessionInitiated(session);
149 static class TestProviderContext implements ProviderContext {
151 private final DataBroker dataBroker;
153 TestProviderContext(DataBroker dataBroker) {
154 this.dataBroker = dataBroker;
158 @SuppressWarnings("unchecked")
159 public <T extends BindingAwareService> T getSALService(Class<T> service) {
160 if (service.equals(DataBroker.class)) {
161 return (T) dataBroker;
163 throw new UnsupportedOperationException(service.toGenericString());
168 @SuppressWarnings("unchecked")
169 public <T extends RpcService> T getRpcService(Class<T> serviceInterface) {
170 if (serviceInterface.equals(OpendaylightPortStatisticsService.class)) {
171 return (T) mock(MockOpendaylightPortStatisticsRpcService.class);
172 } else if (serviceInterface.equals(OpendaylightFlowTableStatisticsService.class)) {
173 return (T) mock(MockOpendaylightFlowTableStatisticsRpcService.class);
175 throw new UnsupportedOperationException(serviceInterface.toGenericString());
180 public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> serviceInterface,
181 T implementation) throws IllegalStateException {
182 throw new UnsupportedOperationException();
186 public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> serviceInterface,
187 T implementation) throws IllegalStateException {
188 throw new UnsupportedOperationException();
192 public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
193 registerRouteChangeListener(L listener) {
194 throw new UnsupportedOperationException();
199 interface MockOpendaylightPortStatisticsRpcService extends OpendaylightPortStatisticsService, RpcService {
202 interface MockOpendaylightFlowTableStatisticsRpcService extends OpendaylightFlowTableStatisticsService, RpcService {