338d0a2a2c33619d528b7f0440891511f9741178
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / RpcImplementationBean.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.blueprint.ext;
9
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;
21
22 /**
23  * Blueprint bean corresponding to the "rpc-implementation" element that registers an RPC implementation with
24  * the RpcProviderRegistry.
25  *
26  * @author Thomas Pantelis
27  */
28 public class RpcImplementationBean {
29     private static final Logger LOG = LoggerFactory.getLogger(RpcImplementationBean.class);
30     static final String RPC_IMPLEMENTATION = "rpc-implementation";
31
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<>();
37
38     public void setRpcRegistry(final RpcProviderRegistry rpcRegistry) {
39         this.rpcRegistry = rpcRegistry;
40     }
41
42     public void setBundle(final Bundle bundle) {
43         this.bundle = bundle;
44     }
45
46     public void setInterfaceName(final String interfaceName) {
47         this.interfaceName = interfaceName;
48     }
49
50     public void setImplementation(final RpcService implementation) {
51         this.implementation = implementation;
52     }
53
54     @SuppressWarnings("checkstyle:IllegalCatch")
55     public void init() {
56         try {
57             List<Class<RpcService>> rpcInterfaces = getImplementedRpcServiceInterfaces(interfaceName,
58                     implementation.getClass(), bundle, RPC_IMPLEMENTATION);
59
60             LOG.debug("{}: init - adding implementation {} for RpcService interface(s) {}", bundle.getSymbolicName(),
61                     implementation, rpcInterfaces);
62
63             for (Class<RpcService> rpcInterface : rpcInterfaces) {
64                 rpcRegistrations.add(rpcRegistry.addRpcImplementation(rpcInterface, implementation));
65             }
66         } catch (final ComponentDefinitionException e) {
67             throw e;
68         } catch (final Exception e) {
69             throw new ComponentDefinitionException(String.format(
70                     "Error processing \"%s\" for %s", RPC_IMPLEMENTATION, implementation.getClass()), e);
71         }
72     }
73
74     public void destroy() {
75         for (RpcRegistration<RpcService> reg: rpcRegistrations) {
76             reg.close();
77         }
78     }
79
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);
86
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));
91             }
92
93             return Collections.singletonList((Class<RpcService>)rpcInterface);
94         }
95
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);
100             }
101         }
102
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));
107         }
108
109         return rpcInterfaces;
110     }
111 }