Configurable update-strategy for clusteredAppConfig
[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(RpcProviderRegistry rpcRegistry) {
39         this.rpcRegistry = rpcRegistry;
40     }
41
42     public void setBundle(Bundle bundle) {
43         this.bundle = bundle;
44     }
45
46     public void setInterfaceName(String interfaceName) {
47         this.interfaceName = interfaceName;
48     }
49
50     public void setImplementation(RpcService implementation) {
51         this.implementation = implementation;
52     }
53
54     public void init() {
55         try {
56             List<Class<RpcService>> rpcInterfaces = getImplementedRpcServiceInterfaces(interfaceName,
57                     implementation.getClass(), bundle, RPC_IMPLEMENTATION);
58
59             LOG.debug("{}: init - adding implementation {} for RpcService interface(s) {}", bundle.getSymbolicName(),
60                     implementation, rpcInterfaces);
61
62             for(Class<RpcService> rpcInterface: rpcInterfaces) {
63                 rpcRegistrations.add(rpcRegistry.addRpcImplementation(rpcInterface, implementation));
64             }
65         } catch(ComponentDefinitionException e) {
66             throw e;
67         } catch(Exception e) {
68             throw new ComponentDefinitionException(String.format(
69                     "Error processing \"%s\" for %s", RPC_IMPLEMENTATION, implementation.getClass()), e);
70         }
71     }
72
73     public void destroy() {
74         for(RpcRegistration<RpcService> reg: rpcRegistrations) {
75             reg.close();
76         }
77     }
78
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);
84
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));
89             }
90
91             return Collections.singletonList((Class<RpcService>)rpcInterface);
92         }
93
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);
98             }
99         }
100
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));
105         }
106
107         return rpcInterfaces;
108     }
109 }