Code Clean Up
[bgpcep.git] / bgp / bmp-impl / src / main / java / org / opendaylight / controller / config / yang / bmp / impl / BmpDispatcherImplModule.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, 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
9 package org.opendaylight.controller.config.yang.bmp.impl;
10
11 import com.google.common.reflect.AbstractInvocationHandler;
12 import com.google.common.reflect.Reflection;
13 import java.lang.reflect.Method;
14 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
15 import org.opendaylight.protocol.bmp.api.BmpDispatcher;
16 import org.osgi.framework.BundleContext;
17
18 /**
19  * @deprecated Replaced by blueprint wiring but remains for backwards compatibility until downstream users
20  *             of the provided config system service are converted to blueprint.
21  */
22 @Deprecated
23 public class BmpDispatcherImplModule extends AbstractBmpDispatcherImplModule {
24     private BundleContext bundleContext;
25
26     public BmpDispatcherImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
27         super(identifier, dependencyResolver);
28     }
29
30     public BmpDispatcherImplModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final org.opendaylight.controller.config.yang.bmp.impl.BmpDispatcherImplModule oldModule, final java.lang.AutoCloseable oldInstance) {
31         super(identifier, dependencyResolver, oldModule, oldInstance);
32     }
33
34     @Override
35     public void customValidation() {
36         // add custom validation form module attributes here.
37     }
38
39     @Override
40     public AutoCloseable createInstance() {
41         // The BmpDispatcher instance is created and advertised as an OSGi service via blueprint
42         // so obtain it here (waiting if necessary).
43         final WaitingServiceTracker<BmpDispatcher> tracker =
44                 WaitingServiceTracker.create(BmpDispatcher.class, this.bundleContext);
45         final BmpDispatcher service = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
46
47         // Create a proxy to override close to close the ServiceTracker. The actual BmpDispatcher
48         // instance will be closed via blueprint.
49         return Reflection.newProxy(BmpDispatcher.class, new AbstractInvocationHandler() {
50             @Override
51             protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
52                 if (method.getName().equals("close")) {
53                     tracker.close();
54                     return null;
55                 } else {
56                     return method.invoke(service, args);
57                 }
58             }
59         });
60     }
61
62     void setBundleContext(BundleContext bundleContext) {
63         this.bundleContext = bundleContext;
64     }
65 }