2 * Copyright (c) 2016 Brocade Communications 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.blueprint.ext;
10 import com.google.common.base.Strings;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.yangtools.yang.binding.RpcService;
17 import org.osgi.framework.Bundle;
18 import org.osgi.service.blueprint.container.ComponentDefinitionException;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Blueprint bean corresponding to the "rpc-implementation" element that registers an RPC implementation with
24 * the RpcProviderRegistry.
26 * @author Thomas Pantelis
28 public class RpcImplementationBean {
29 private static final Logger LOG = LoggerFactory.getLogger(RpcImplementationBean.class);
30 static final String RPC_IMPLEMENTATION = "rpc-implementation";
32 private RpcProviderRegistry rpcRegistry;
33 private Bundle bundle;
34 private String interfaceName;
35 private RpcService implementation;
36 private final List<RpcRegistration<RpcService>> rpcRegistrations = new ArrayList<>();
38 public void setRpcRegistry(final RpcProviderRegistry rpcRegistry) {
39 this.rpcRegistry = rpcRegistry;
42 public void setBundle(final Bundle bundle) {
46 public void setInterfaceName(final String interfaceName) {
47 this.interfaceName = interfaceName;
50 public void setImplementation(final RpcService implementation) {
51 this.implementation = implementation;
54 @SuppressWarnings("checkstyle:IllegalCatch")
57 List<Class<RpcService>> rpcInterfaces = getImplementedRpcServiceInterfaces(interfaceName,
58 implementation.getClass(), bundle, RPC_IMPLEMENTATION);
60 LOG.debug("{}: init - adding implementation {} for RpcService interface(s) {}", bundle.getSymbolicName(),
61 implementation, rpcInterfaces);
63 for (Class<RpcService> rpcInterface : rpcInterfaces) {
64 rpcRegistrations.add(rpcRegistry.addRpcImplementation(rpcInterface, implementation));
66 } catch (final ComponentDefinitionException e) {
68 } catch (final Exception e) {
69 throw new ComponentDefinitionException(String.format(
70 "Error processing \"%s\" for %s", RPC_IMPLEMENTATION, implementation.getClass()), e);
74 public void destroy() {
75 for (RpcRegistration<RpcService> reg: rpcRegistrations) {
80 @SuppressWarnings("unchecked")
81 static List<Class<RpcService>> getImplementedRpcServiceInterfaces(final String interfaceName,
82 final Class<?> implementationClass, final Bundle bundle, final String logName)
83 throws ClassNotFoundException {
84 if (!Strings.isNullOrEmpty(interfaceName)) {
85 Class<?> rpcInterface = bundle.loadClass(interfaceName);
87 if (!rpcInterface.isAssignableFrom(implementationClass)) {
88 throw new ComponentDefinitionException(String.format(
89 "The specified \"interface\" %s for \"%s\" is not implemented by RpcService \"ref\" %s",
90 interfaceName, logName, implementationClass));
93 return Collections.singletonList((Class<RpcService>)rpcInterface);
96 List<Class<RpcService>> rpcInterfaces = new ArrayList<>();
97 for (Class<?> intface : implementationClass.getInterfaces()) {
98 if (RpcService.class.isAssignableFrom(intface)) {
99 rpcInterfaces.add((Class<RpcService>) intface);
103 if (rpcInterfaces.isEmpty()) {
104 throw new ComponentDefinitionException(String.format(
105 "The \"ref\" instance %s for \"%s\" does not implemented any RpcService interfaces",
106 implementationClass, logName));
109 return rpcInterfaces;