Simplify code using Java 8 features
[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.Set;
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;
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  * @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 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;
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 setDomRpcProvider(final DOMRpcProviderService rpcProviderService) {
67         this.domRpcProvider = rpcProviderService;
68     }
69
70     public void setBindingRpcProvider(final RpcProviderService rpcProvider) {
71         this.bindingRpcProvider = rpcProvider;
72     }
73
74     public void setSchemaService(final DOMSchemaService 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 = 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);
136     }
137
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()));
143         }
144
145         reg = bindingRpcProvider.registerRpcImplementation(interfaceClass, implementation);
146         LOG.debug("Registered implementation {} for {}", implementation, interfaceName);
147     }
148 }