Add legacy pre-Boron EntityOwnershipService adapter
[controller.git] / opendaylight / md-sal / sal-dom-config / src / main / java / org / opendaylight / controller / config / yang / config / legacy_entity_ownership_service_provider / LegacyEntityOwnershipServiceProviderModule.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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 package org.opendaylight.controller.config.yang.config.legacy_entity_ownership_service_provider;
9
10 import com.google.common.reflect.AbstractInvocationHandler;
11 import com.google.common.reflect.Reflection;
12 import java.lang.reflect.Method;
13 import org.opendaylight.controller.config.api.DependencyResolver;
14 import org.opendaylight.controller.config.api.ModuleIdentifier;
15 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
17 import org.osgi.framework.BundleContext;
18
19 /**
20  * @deprecated Replaced by blueprint wiring
21  */
22 @Deprecated
23 public class LegacyEntityOwnershipServiceProviderModule extends AbstractLegacyEntityOwnershipServiceProviderModule {
24     private BundleContext bundleContext;
25
26     public LegacyEntityOwnershipServiceProviderModule(ModuleIdentifier identifier, DependencyResolver dependencyResolver) {
27         super(identifier, dependencyResolver);
28     }
29
30     public LegacyEntityOwnershipServiceProviderModule(ModuleIdentifier identifier, DependencyResolver dependencyResolver,
31             LegacyEntityOwnershipServiceProviderModule oldModule, java.lang.AutoCloseable oldInstance) {
32         super(identifier, dependencyResolver, oldModule, oldInstance);
33     }
34
35     @Override
36     public void customValidation() {
37         // add custom validation form module attributes here.
38     }
39
40     @Override
41     public AutoCloseable createInstance() {
42         final WaitingServiceTracker<EntityOwnershipService> tracker = WaitingServiceTracker.create(
43                 EntityOwnershipService.class, bundleContext);
44         final EntityOwnershipService service = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
45
46         return Reflection.newProxy(AutoCloseableEntityOwnershipService.class, new AbstractInvocationHandler() {
47             @Override
48             protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
49                 if (method.getName().equals("close")) {
50                     tracker.close();
51                     return null;
52                 } else {
53                     return method.invoke(service, args);
54                 }
55             }
56         });
57     }
58
59     public void setBundleContext(final BundleContext bundleContext) {
60         this.bundleContext = bundleContext;
61     }
62
63     private static interface AutoCloseableEntityOwnershipService extends EntityOwnershipService, AutoCloseable {
64     }
65
66 }