60fa7a5545a8bad88f129dc172a1c14ec5f0a1fa
[genius.git] / interfacemanager / interfacemanager-impl / src / test / java / org / opendaylight / genius / interfacemanager / test / InterfaceManagerTestModule.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.genius.interfacemanager.test;
9
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
12
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;
46
47 /**
48  * Dependency Injection Wiring for {@link InterfaceManagerConfigurationTest}.
49  *
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.
52  *
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.
55  *
56  * <p>For "proper" *Module examples, please see the AclServiceModule and
57  * AclServiceTestModule or ElanServiceTestModule instead.
58  *
59  * @author Michael Vorburger
60  */
61 public class InterfaceManagerTestModule extends AbstractGuiceJsr250Module {
62
63     @Override
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)
69
70         DataBroker dataBroker = DataBrokerTestModule.dataBroker();
71         bind(DataBroker.class).toInstance(dataBroker);
72
73         LockManagerService lockManager = new LockManager(dataBroker);
74         bind(LockManagerService.class).toInstance(lockManager);
75
76         IdUtils idUtils = new IdUtils();
77         IdManagerService idManager;
78         try {
79             idManager = new IdManager(dataBroker,
80                     new SingleTransactionDataBroker(dataBroker), lockManager, idUtils);
81         } catch (ReadFailedException e) {
82             // TODO Support AbstractGuiceJsr250Module
83             throw new ModuleSetupRuntimeException(e);
84         }
85         bind(IdManagerService.class).toInstance(idManager);
86
87         TestIMdsalApiManager mdsalManager = TestIMdsalApiManager.newInstance();
88         bind(IMdsalApiManager.class).toInstance(mdsalManager);
89         bind(TestIMdsalApiManager.class).toInstance(mdsalManager);
90
91         EntityOwnershipService entityOwnershipService = TestEntityOwnershipService.newInstance();
92         bind(EntityOwnershipService.class).toInstance(entityOwnershipService);
93
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,
96                 idManager);
97         bind(IInterfaceManager.class).toInstance(interfaceManager);
98         bind(Stopper.class).toInstance(new Stopper(interfaceManager));
99     }
100
101     @Singleton
102     private static class Stopper {
103         final InterfacemgrProvider interfaceManager;
104
105         Stopper(InterfacemgrProvider interfaceManager) {
106             this.interfaceManager = interfaceManager;
107         }
108
109         @PreDestroy
110         void close() throws Exception {
111             interfaceManager.close();
112         }
113     }
114
115     /**
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.
120      */
121     private InterfacemgrProvider interfaceManager(IMdsalApiManager mdsalManager,
122             EntityOwnershipService entityOwnershipService, DataBroker dataBroker, IdManagerService idManager) {
123
124         InterfacemgrProvider provider = new InterfacemgrProvider();
125
126         RpcProviderRegistry rpcProviderRegistry = mock(RpcProviderRegistry.class /* TODO how-to? exception() */);
127         when(rpcProviderRegistry.getRpcService(IdManagerService.class)).thenReturn(idManager);
128
129         provider.setRpcProviderRegistry(rpcProviderRegistry);
130         provider.setMdsalManager(mdsalManager);
131         provider.setEntityOwnershipService(entityOwnershipService);
132         provider.setNotificationService(mock(NotificationService.class));
133
134         // TODO just use rpcProviderRegistry, which IS-A ProviderContext here?
135         ProviderContext session = new TestProviderContext(dataBroker);
136         provider.onSessionInitiated(session);
137
138         return provider;
139     }
140
141     static class TestProviderContext implements ProviderContext {
142
143         private final DataBroker dataBroker;
144
145         TestProviderContext(DataBroker dataBroker) {
146             this.dataBroker = dataBroker;
147         }
148
149         @Override
150         @SuppressWarnings("unchecked")
151         public <T extends BindingAwareService> T getSALService(Class<T> service) {
152             if (service.equals(DataBroker.class)) {
153                 return (T) dataBroker;
154             } else {
155                 throw new UnsupportedOperationException(service.toGenericString());
156             }
157         }
158
159         @Override
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);
166             } else {
167                 throw new UnsupportedOperationException(serviceInterface.toGenericString());
168             }
169         }
170
171         @Override
172         public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> serviceInterface,
173                 T implementation) throws IllegalStateException {
174             throw new UnsupportedOperationException();
175         }
176
177         @Override
178         public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> serviceInterface,
179                 T implementation) throws IllegalStateException {
180             throw new UnsupportedOperationException();
181         }
182
183         @Override
184         public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
185             registerRouteChangeListener(L listener) {
186             throw new UnsupportedOperationException();
187         }
188
189     }
190
191     interface MockOpendaylightPortStatisticsRpcService extends OpendaylightPortStatisticsService, RpcService {
192     }
193
194     interface MockOpendaylightFlowTableStatisticsRpcService extends OpendaylightFlowTableStatisticsService, RpcService {
195     }
196
197 }