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(RpcProviderRegistry rpcRegistry) {
39 this.rpcRegistry = rpcRegistry;
42 public void setBundle(Bundle bundle) {
46 public void setInterfaceName(String interfaceName) {
47 this.interfaceName = interfaceName;
50 public void setImplementation(RpcService implementation) {
51 this.implementation = implementation;
56 List<Class<RpcService>> rpcInterfaces = getImplementedRpcServiceInterfaces(interfaceName,
57 implementation.getClass(), bundle, RPC_IMPLEMENTATION);
59 LOG.debug("{}: init - adding implementation {} for RpcService interface(s) {}", bundle.getSymbolicName(),
60 implementation, rpcInterfaces);
62 for(Class<RpcService> rpcInterface: rpcInterfaces) {
63 rpcRegistrations.add(rpcRegistry.addRpcImplementation(rpcInterface, implementation));
65 } catch(ComponentDefinitionException e) {
67 } catch(Exception e) {
68 throw new ComponentDefinitionException(String.format(
69 "Error processing \"%s\" for %s", RPC_IMPLEMENTATION, implementation.getClass()), e);
73 public void destroy() {
74 for(RpcRegistration<RpcService> reg: rpcRegistrations) {
79 @SuppressWarnings("unchecked")
80 static List<Class<RpcService>> getImplementedRpcServiceInterfaces(String interfaceName,
81 Class<?> implementationClass, Bundle bundle, String logName) throws ClassNotFoundException {
82 if(!Strings.isNullOrEmpty(interfaceName)) {
83 Class<?> rpcInterface = bundle.loadClass(interfaceName);
85 if(!rpcInterface.isAssignableFrom(implementationClass)) {
86 throw new ComponentDefinitionException(String.format(
87 "The specified \"interface\" %s for \"%s\" is not implemented by RpcService \"ref\" %s",
88 interfaceName, logName, implementationClass));
91 return Collections.singletonList((Class<RpcService>)rpcInterface);
94 List<Class<RpcService>> rpcInterfaces = new ArrayList<>();
95 for(Class<?> intface: implementationClass.getInterfaces()) {
96 if(RpcService.class.isAssignableFrom(intface)) {
97 rpcInterfaces.add((Class<RpcService>) intface);
101 if(rpcInterfaces.isEmpty()) {
102 throw new ComponentDefinitionException(String.format(
103 "The \"ref\" instance %s for \"%s\" does not implemented any RpcService interfaces",
104 implementationClass, logName));
107 return rpcInterfaces;