Update to MD-SAL APIs
[controller.git] / opendaylight / md-sal / sal-binding-api / src / main / java / org / opendaylight / controller / sal / binding / api / AbstractBindingAwareProvider.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.controller.sal.binding.api;
9
10 import java.util.Collection;
11 import java.util.Collections;
12
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.yangtools.yang.binding.RpcService;
16 import org.osgi.framework.BundleActivator;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.framework.ServiceReference;
19
20 public abstract class AbstractBindingAwareProvider implements BindingAwareProvider, BundleActivator {
21
22     /**
23      * Initializes the bundle.
24      * 
25      * Implementation of initialization get's the Binding Aware Broker from
26      * service registry and register itself as a {@link BindingAwareProvider}
27      * 
28      * Callback order is:
29      * <ol>
30      * <li>{@link #startImpl(BundleContext)}
31      * <li>{@link #onSessionInitiated(ProviderContext)}
32      * <li>Registration of global {@link RpcService} implementations returned by
33      * {@link #getImplementations()}
34      * <li>Registration of {@link ProviderFunctionality} implementations
35      * returned by {@link #getFunctionality()}
36      * </ol>
37      * 
38      */
39     @Override
40     public final void start(BundleContext context) throws Exception {
41         startImpl(context);
42         ServiceReference<BindingAwareBroker> brokerRef = context.getServiceReference(BindingAwareBroker.class);
43         BindingAwareBroker broker = context.getService(brokerRef);
44
45         ProviderContext ctx = broker.registerProvider(this, context);
46         registerRpcImplementations(ctx);
47         registerFunctionality(ctx);
48     }
49
50     private void registerFunctionality(ProviderContext ctx) {
51         Collection<? extends ProviderFunctionality> functionality = this.getFunctionality();
52         if (functionality == null || functionality.isEmpty()) {
53             return;
54         }
55         for (ProviderFunctionality providerFunctionality : functionality) {
56             ctx.registerFunctionality(providerFunctionality);
57         }
58
59     }
60
61     private void registerRpcImplementations(ProviderContext ctx) {
62         Collection<? extends RpcService> rpcs = this.getImplementations();
63         if (rpcs == null || rpcs.isEmpty()) {
64             return;
65         }
66         for (RpcService rpcService : rpcs) {
67             // ctx.addRpcImplementation(type, implementation);
68         }
69
70     }
71
72     /**
73      * Called when this bundle is started (before
74      * {@link #onSessionInitiated(ProviderContext)} so the Framework can perform
75      * the bundle-specific activities necessary to start this bundle. This
76      * method can be used to register services or to allocate any resources that
77      * this bundle needs.
78      * 
79      * <p>
80      * This method must complete and return to its caller in a timely manner.
81      * 
82      * @param context
83      *            The execution context of the bundle being started.
84      * @throws Exception
85      *             If this method throws an exception, this bundle is marked as
86      *             stopped and the Framework will remove this bundle's
87      *             listeners, unregister all services registered by this bundle,
88      *             and release all services used by this bundle.
89      */
90     protected void startImpl(BundleContext context) {
91         // NOOP
92     }
93
94     /**
95      * Called when this bundle is stopped so the Framework can perform the
96      * bundle-specific activities necessary to stop the bundle. In general, this
97      * method should undo the work that the {@code BundleActivator.start} method
98      * started. There should be no active threads that were started by this
99      * bundle when this bundle returns. A stopped bundle must not call any
100      * Framework objects.
101      * 
102      * <p>
103      * This method must complete and return to its caller in a timely manner.
104      * 
105      * @param context The execution context of the bundle being stopped.
106      * @throws Exception If this method throws an exception, the bundle is still
107      *         marked as stopped, and the Framework will remove the bundle's
108      *         listeners, unregister all services registered by the bundle, and
109      *         release all services used by the bundle.
110      */
111     protected void stopImpl(BundleContext context) {
112         // NOOP
113     }
114
115     /**
116      * Bundle stop callback
117      * 
118      * 
119      * Custom implementation of bundle stop could be carried by overriding
120      * {@link #stopImpl(BundleContext)} method.
121      * 
122      */
123     @Override
124     public final void stop(BundleContext context) throws Exception {
125         stopImpl(context);
126     }
127
128     @Override
129     public Collection<? extends ProviderFunctionality> getFunctionality() {
130         return Collections.emptySet();
131     }
132
133     @Override
134     public Collection<? extends RpcService> getImplementations() {
135         return Collections.emptySet();
136     }
137     
138     /**
139      * Initialization of consumer context.
140      * 
141      * {@link ProviderContext} is replacement of {@link ConsumerContext}
142      * so this method is not needed in case of Provider.
143      * 
144      */
145     @Deprecated
146     @Override
147     public final void onSessionInitialized(ConsumerContext session) {
148         // NOOP
149     }
150 }