2 * Copyright (c) 2017 Cisco 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.collect.Collections2;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
14 import org.opendaylight.mdsal.binding.api.RpcProviderService;
15 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
17 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.mdsal.dom.spi.RpcRoutingStrategy;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
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;
30 * Blueprint bean corresponding to the "action-provider" element that registers the promise to instantiate action
31 * instances with RpcProviderRegistry.
34 * This bean has two distinct facets:
35 * - if a reference bean is provided, it registers it with {@link RpcProviderService}
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
39 * @author Robert Varga
41 public class ActionProviderBean {
42 static final String ACTION_PROVIDER = "action-provider";
44 private static final Logger LOG = LoggerFactory.getLogger(ActionProviderBean.class);
46 private DOMRpcProviderService domRpcProvider;
47 private RpcProviderService bindingRpcProvider;
48 private DOMSchemaService schemaService;
49 private RpcService implementation;
50 private String interfaceName;
51 private Registration reg;
52 private Bundle bundle;
54 public void setBundle(final Bundle bundle) {
58 public void setInterfaceName(final String interfaceName) {
59 this.interfaceName = interfaceName;
62 public void setImplementation(final RpcService implementation) {
63 this.implementation = implementation;
66 public void setDomRpcProvider(final DOMRpcProviderService rpcProviderService) {
67 this.domRpcProvider = rpcProviderService;
70 public void setBindingRpcProvider(final RpcProviderService rpcProvider) {
71 this.bindingRpcProvider = rpcProvider;
74 public void setSchemaService(final DOMSchemaService schemaService) {
75 this.schemaService = schemaService;
79 // First resolve the interface class
80 final Class<RpcService> interfaceClass = getRpcClass();
82 LOG.debug("{}: resolved interface {} to {}", ACTION_PROVIDER, interfaceName, interfaceClass);
84 if (implementation != null) {
85 registerImplementation(interfaceClass);
87 registerFallback(interfaceClass);
91 @SuppressWarnings("checkstyle:IllegalCatch")
92 public void destroy() {
96 } catch (final Exception e) {
97 LOG.warn("{}: error while unregistering", ACTION_PROVIDER, e);
104 @SuppressWarnings("unchecked")
105 private Class<RpcService> getRpcClass() {
106 final Class<?> iface;
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);
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));
120 return (Class<RpcService>) iface;
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);
131 final Set<DOMRpcIdentifier> rpcs = ImmutableSet.copyOf(Collections2.transform(paths, DOMRpcIdentifier::create));
132 reg = domRpcProvider.registerRpcImplementation(
133 (rpc, input) -> FluentFutures.immediateFailedFluentFuture(new DOMRpcImplementationNotAvailableException(
134 "Action %s has no instance matching %s", rpc, input)), rpcs);
135 LOG.debug("Registered provider for {}", interfaceName);
138 private void registerImplementation(final Class<RpcService> interfaceClass) {
139 if (!interfaceClass.isInstance(implementation)) {
140 throw new ComponentDefinitionException(String.format(
141 "The specified \"interface\" %s for \"%s\" is not implemented by RpcService \"ref\" %s",
142 interfaceName, ACTION_PROVIDER, implementation.getClass()));
145 reg = bindingRpcProvider.registerRpcImplementation(interfaceClass, implementation);
146 LOG.debug("Registered implementation {} for {}", implementation, interfaceName);