Update upstreams
[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 java.util.Collection;
13 import java.util.Objects;
14 import java.util.Set;
15 import org.opendaylight.mdsal.binding.api.RpcProviderService;
16 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
17 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
18 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
19 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
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.common.QName;
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 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
38  */
39 public class ActionProviderBean {
40     static final String ACTION_PROVIDER = "action-provider";
41
42     private static final Logger LOG = LoggerFactory.getLogger(ActionProviderBean.class);
43
44     private DOMRpcProviderService domRpcProvider;
45     private RpcProviderService bindingRpcProvider;
46     private DOMSchemaService schemaService;
47     private RpcService implementation;
48     private String interfaceName;
49     private Registration reg;
50     private Bundle bundle;
51
52     public void setBundle(final Bundle bundle) {
53         this.bundle = bundle;
54     }
55
56     public void setInterfaceName(final String interfaceName) {
57         this.interfaceName = interfaceName;
58     }
59
60     public void setImplementation(final RpcService implementation) {
61         this.implementation = implementation;
62     }
63
64     public void setDomRpcProvider(final DOMRpcProviderService rpcProviderService) {
65         domRpcProvider = rpcProviderService;
66     }
67
68     public void setBindingRpcProvider(final RpcProviderService rpcProvider) {
69         bindingRpcProvider = rpcProvider;
70     }
71
72     public void setSchemaService(final DOMSchemaService schemaService) {
73         this.schemaService = schemaService;
74     }
75
76     public void init() {
77         // First resolve the interface class
78         final Class<RpcService> interfaceClass = getRpcClass();
79
80         LOG.debug("{}: resolved interface {} to {}", ACTION_PROVIDER, interfaceName, interfaceClass);
81
82         if (implementation != null) {
83             registerImplementation(interfaceClass);
84         } else {
85             registerFallback(interfaceClass);
86         }
87     }
88
89     @SuppressWarnings("checkstyle:IllegalCatch")
90     public void destroy() {
91         if (reg != null) {
92             try {
93                 reg.close();
94             } catch (final Exception e) {
95                 LOG.warn("{}: error while unregistering", ACTION_PROVIDER, e);
96             } finally {
97                 reg = null;
98             }
99         }
100     }
101
102     @SuppressWarnings("unchecked")
103     private Class<RpcService> getRpcClass() {
104         final Class<?> iface;
105
106         try {
107             iface = bundle.loadClass(interfaceName);
108         } catch (final ClassNotFoundException e) {
109             throw new ComponentDefinitionException(String.format(
110                 "The specified \"interface\" for %s \"%s\" does not refer to an available class", interfaceName,
111                 ACTION_PROVIDER), e);
112         }
113         if (!RpcService.class.isAssignableFrom(iface)) {
114             throw new ComponentDefinitionException(String.format(
115                 "The specified \"interface\" %s for \"%s\" is not an RpcService", interfaceName, ACTION_PROVIDER));
116         }
117
118         return (Class<RpcService>) iface;
119     }
120
121     private void registerFallback(final Class<RpcService> interfaceClass) {
122         final Collection<QName> paths = RpcUtil.decomposeRpcService(interfaceClass,
123             schemaService.getGlobalContext(), Objects::nonNull);
124         if (paths.isEmpty()) {
125             LOG.warn("{}: interface {} has no actions defined", ACTION_PROVIDER, interfaceClass);
126             return;
127         }
128
129         final Set<DOMRpcIdentifier> rpcs = ImmutableSet.copyOf(Collections2.transform(paths, DOMRpcIdentifier::create));
130         reg = domRpcProvider.registerRpcImplementation(
131             (rpc, input) -> FluentFutures.immediateFailedFluentFuture(new DOMRpcImplementationNotAvailableException(
132                 "Action %s has no instance matching %s", rpc, input)), rpcs);
133         LOG.debug("Registered provider for {}", interfaceName);
134     }
135
136     private void registerImplementation(final Class<RpcService> interfaceClass) {
137         if (!interfaceClass.isInstance(implementation)) {
138             throw new ComponentDefinitionException(String.format(
139                 "The specified \"interface\" %s for \"%s\" is not implemented by RpcService \"ref\" %s",
140                 interfaceName, ACTION_PROVIDER, implementation.getClass()));
141         }
142
143         reg = bindingRpcProvider.registerRpcImplementation(interfaceClass, implementation);
144         LOG.debug("Registered implementation {} for {}", implementation, interfaceName);
145     }
146 }