2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.api;
10 import java.util.Collection;
11 import java.util.Collections;
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;
20 public abstract class AbstractBindingAwareProvider implements BindingAwareProvider, BundleActivator {
23 * Initializes the bundle.
25 * Implementation of initialization get's the Binding Aware Broker from
26 * service registry and register itself as a {@link BindingAwareProvider}
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()}
40 public final void start(BundleContext context) throws Exception {
42 ServiceReference<BindingAwareBroker> brokerRef = context.getServiceReference(BindingAwareBroker.class);
43 BindingAwareBroker broker = context.getService(brokerRef);
45 ProviderContext ctx = broker.registerProvider(this, context);
46 registerRpcImplementations(ctx);
47 registerFunctionality(ctx);
50 private void registerFunctionality(ProviderContext ctx) {
51 Collection<? extends ProviderFunctionality> functionality = this.getFunctionality();
52 if (functionality == null || functionality.isEmpty()) {
55 for (ProviderFunctionality providerFunctionality : functionality) {
56 ctx.registerFunctionality(providerFunctionality);
61 private void registerRpcImplementations(ProviderContext ctx) {
62 Collection<? extends RpcService> rpcs = this.getImplementations();
63 if (rpcs == null || rpcs.isEmpty()) {
66 for (RpcService rpcService : rpcs) {
67 // ctx.addRpcImplementation(type, implementation);
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
80 * This method must complete and return to its caller in a timely manner.
83 * The execution context of the bundle being started.
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.
90 protected void startImpl(BundleContext context) {
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
103 * This method must complete and return to its caller in a timely manner.
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.
111 protected void stopImpl(BundleContext context) {
116 * Bundle stop callback
119 * Custom implementation of bundle stop could be carried by overriding
120 * {@link #stopImpl(BundleContext)} method.
124 public final void stop(BundleContext context) throws Exception {
129 public Collection<? extends ProviderFunctionality> getFunctionality() {
130 return Collections.emptySet();
134 public Collection<? extends RpcService> getImplementations() {
135 return Collections.emptySet();
139 * Initialization of consumer context.
141 * {@link ProviderContext} is replacement of {@link ConsumerContext}
142 * so this method is not needed in case of Provider.
147 public final void onSessionInitialized(ConsumerContext session) {