Drop unnecessary "throws Exception" declarations
[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 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.binding.test.DataBrokerTestModule;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.controller.sal.binding.api.rpc.RpcContextIdentifier;
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 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Dependency Injection Wiring for {@link InterfaceManagerConfigurationTest}.
51  *
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.
54  *
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.
57  *
58  * <p>For "proper" *Module examples, please see the AclServiceModule and
59  * AclServiceTestModule or ElanServiceTestModule instead.
60  *
61  * @author Michael Vorburger
62  */
63 public class InterfaceManagerTestModule extends AbstractGuiceJsr250Module {
64
65     private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerTestModule.class);
66
67     @Override
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)
73
74         DataBroker dataBroker = DataBrokerTestModule.dataBroker();
75         bind(DataBroker.class).toInstance(dataBroker);
76
77         LockManagerService lockManager = new LockManager(dataBroker);
78         bind(LockManagerService.class).toInstance(lockManager);
79
80         IdUtils idUtils = new IdUtils();
81         IdManagerService idManager;
82         try {
83             idManager = new IdManager(dataBroker, lockManager, idUtils);
84         } catch (ReadFailedException e) {
85             // TODO Support AbstractGuiceJsr250Module
86             throw new ModuleSetupRuntimeException(e);
87         }
88         bind(IdManagerService.class).toInstance(idManager);
89
90         TestIMdsalApiManager mdsalManager = TestIMdsalApiManager.newInstance();
91         bind(IMdsalApiManager.class).toInstance(mdsalManager);
92         bind(TestIMdsalApiManager.class).toInstance(mdsalManager);
93
94         EntityOwnershipService entityOwnershipService = TestEntityOwnershipService.newInstance();
95         bind(EntityOwnershipService.class).toInstance(entityOwnershipService);
96
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,
99                 idManager);
100         bind(IInterfaceManager.class).toInstance(interfaceManager);
101         bind(Stopper.class).toInstance(new Stopper(interfaceManager));
102     }
103
104     @Singleton
105     private static class Stopper {
106         final InterfacemgrProvider interfaceManager;
107
108         Stopper(InterfacemgrProvider interfaceManager) {
109             this.interfaceManager = interfaceManager;
110         }
111
112         @PreDestroy
113         @SuppressWarnings("checkstyle:IllegalCatch")
114         void close() {
115             try {
116                 interfaceManager.close();
117             } catch (Exception e) {
118                 LOG.error("close() failed", e);
119             }
120         }
121     }
122
123     /**
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.
128      */
129     private InterfacemgrProvider interfaceManager(IMdsalApiManager mdsalManager,
130             EntityOwnershipService entityOwnershipService, DataBroker dataBroker, IdManagerService idManager) {
131
132         InterfacemgrProvider provider = new InterfacemgrProvider();
133
134         RpcProviderRegistry rpcProviderRegistry = mock(RpcProviderRegistry.class /* TODO how-to? exception() */);
135         when(rpcProviderRegistry.getRpcService(IdManagerService.class)).thenReturn(idManager);
136
137         provider.setRpcProviderRegistry(rpcProviderRegistry);
138         provider.setMdsalManager(mdsalManager);
139         provider.setEntityOwnershipService(entityOwnershipService);
140         provider.setNotificationService(mock(NotificationService.class));
141
142         // TODO just use rpcProviderRegistry, which IS-A ProviderContext here?
143         ProviderContext session = new TestProviderContext(dataBroker);
144         provider.onSessionInitiated(session);
145
146         return provider;
147     }
148
149     static class TestProviderContext implements ProviderContext {
150
151         private final DataBroker dataBroker;
152
153         TestProviderContext(DataBroker dataBroker) {
154             this.dataBroker = dataBroker;
155         }
156
157         @Override
158         @SuppressWarnings("unchecked")
159         public <T extends BindingAwareService> T getSALService(Class<T> service) {
160             if (service.equals(DataBroker.class)) {
161                 return (T) dataBroker;
162             } else {
163                 throw new UnsupportedOperationException(service.toGenericString());
164             }
165         }
166
167         @Override
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);
174             } else {
175                 throw new UnsupportedOperationException(serviceInterface.toGenericString());
176             }
177         }
178
179         @Override
180         public <T extends RpcService> RpcRegistration<T> addRpcImplementation(Class<T> serviceInterface,
181                 T implementation) throws IllegalStateException {
182             throw new UnsupportedOperationException();
183         }
184
185         @Override
186         public <T extends RpcService> RoutedRpcRegistration<T> addRoutedRpcImplementation(Class<T> serviceInterface,
187                 T implementation) throws IllegalStateException {
188             throw new UnsupportedOperationException();
189         }
190
191         @Override
192         public <L extends RouteChangeListener<RpcContextIdentifier, InstanceIdentifier<?>>> ListenerRegistration<L>
193             registerRouteChangeListener(L listener) {
194             throw new UnsupportedOperationException();
195         }
196
197     }
198
199     interface MockOpendaylightPortStatisticsRpcService extends OpendaylightPortStatisticsService, RpcService {
200     }
201
202     interface MockOpendaylightFlowTableStatisticsRpcService extends OpendaylightFlowTableStatisticsService, RpcService {
203     }
204
205 }