blueprint: final parameters
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / ActionProviderBean.java
1 /*
2  * Copyright (c) 2017 Cisco 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.collect.Collections2;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.Collection;
14 import java.util.Set;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
18 import org.opendaylight.controller.md.sal.dom.broker.spi.rpc.RpcRoutingStrategy;
19 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
20 import org.opendaylight.controller.sal.core.api.model.SchemaService;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24 import org.osgi.framework.Bundle;
25 import org.osgi.service.blueprint.container.ComponentDefinitionException;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Blueprint bean corresponding to the "action-provider" element that registers the promise to instantiate action
31  * instances with RpcProviderRegistry.
32  *
33  * <p>
34  * This bean has two distinct facets:
35  * - if a reference bean is provided, it registers it with {@link RpcProviderRegistry}
36  * - if a reference bean is not provided, it registers the corresponding no-op implementation with
37  *   {@link DOMRpcProviderService} for all action (Routed RPC) elements in the provided interface
38  *
39  * @author Robert Varga
40  */
41 public class ActionProviderBean {
42     static final String ACTION_PROVIDER = "action-provider";
43
44     private static final Logger LOG = LoggerFactory.getLogger(ActionProviderBean.class);
45
46     private DOMRpcProviderService rpcProviderService;
47     private RpcProviderRegistry rpcRegistry;
48     private SchemaService schemaService;
49     private RpcService implementation;
50     private String interfaceName;
51     private Registration reg;
52     private Bundle bundle;
53
54     public void setBundle(final Bundle bundle) {
55         this.bundle = bundle;
56     }
57
58     public void setInterfaceName(final String interfaceName) {
59         this.interfaceName = interfaceName;
60     }
61
62     public void setImplementation(final RpcService implementation) {
63         this.implementation = implementation;
64     }
65
66     public void setRpcProviderService(final DOMRpcProviderService rpcProviderService) {
67         this.rpcProviderService = rpcProviderService;
68     }
69
70     public void setRpcRegistry(final RpcProviderRegistry rpcRegistry) {
71         this.rpcRegistry = rpcRegistry;
72     }
73
74     public void setSchemaService(final SchemaService schemaService) {
75         this.schemaService = schemaService;
76     }
77
78     public void init() {
79         // First resolve the interface class
80         final Class<RpcService> interfaceClass = getRpcClass();
81
82         LOG.debug("{}: resolved interface {} to {}", ACTION_PROVIDER, interfaceName, interfaceClass);
83
84         if (implementation != null) {
85             registerImplementation(interfaceClass);
86         } else {
87             registerFallback(interfaceClass);
88         }
89     }
90
91     @SuppressWarnings("checkstyle:IllegalCatch")
92     public void destroy() {
93         if (reg != null) {
94             try {
95                 reg.close();
96             } catch (final Exception e) {
97                 LOG.warn("{}: error while unregistering", ACTION_PROVIDER, e);
98             } finally {
99                 reg = null;
100             }
101         }
102     }
103
104     @SuppressWarnings("unchecked")
105     private Class<RpcService> getRpcClass() {
106         final Class<?> iface;
107
108         try {
109             iface = bundle.loadClass(interfaceName);
110         } catch (final ClassNotFoundException e) {
111             throw new ComponentDefinitionException(String.format(
112                 "The specified \"interface\" for %s \"%s\" does not refer to an available class", interfaceName,
113                 ACTION_PROVIDER), e);
114         }
115         if (!RpcService.class.isAssignableFrom(iface)) {
116             throw new ComponentDefinitionException(String.format(
117                 "The specified \"interface\" %s for \"%s\" is not an RpcService", interfaceName, ACTION_PROVIDER));
118         }
119
120         return (Class<RpcService>) iface;
121     }
122
123     private void registerFallback(final Class<RpcService> interfaceClass) {
124         final Collection<SchemaPath> paths = RpcUtil.decomposeRpcService(interfaceClass,
125             schemaService.getGlobalContext(), RpcRoutingStrategy::isContextBasedRouted);
126         if (paths.isEmpty()) {
127             LOG.warn("{}: interface {} has no actions defined", ACTION_PROVIDER, interfaceClass);
128             return;
129         }
130
131         final Set<DOMRpcIdentifier> rpcs = ImmutableSet.copyOf(Collections2.transform(paths, DOMRpcIdentifier::create));
132         reg = rpcProviderService.registerRpcImplementation((rpc, input) -> {
133             return Futures.immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException(
134                 "Action %s has no instance matching %s", rpc, input));
135         }, rpcs);
136         LOG.debug("Registered provider for {}", interfaceName);
137     }
138
139     private void registerImplementation(final Class<RpcService> interfaceClass) {
140         if (!interfaceClass.isInstance(implementation)) {
141             throw new ComponentDefinitionException(String.format(
142                 "The specified \"interface\" %s for \"%s\" is not implemented by RpcService \"ref\" %s",
143                 interfaceName, ACTION_PROVIDER, implementation.getClass()));
144         }
145
146         reg = rpcRegistry.addRpcImplementation(interfaceClass, implementation);
147         LOG.debug("Registered implementation {} for {}", implementation, interfaceName);
148     }
149 }