Fix CS warnings in blueprint and enable enforcement
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / RoutedRpcMetadata.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 java.util.Arrays;
11 import java.util.List;
12 import org.apache.aries.blueprint.ext.ComponentFactoryMetadata;
13 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.yangtools.yang.binding.RpcService;
17 import org.osgi.service.blueprint.container.ComponentDefinitionException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Factory metadata corresponding to the "routed-rpc-implementation" element that registers an RPC
23  * implementation with the RpcProviderRegistry and provides the RoutedRpcRegistration instance to the
24  * Blueprint container.
25  *
26  * @author Thomas Pantelis
27  */
28 class RoutedRpcMetadata implements ComponentFactoryMetadata {
29     private static final Logger LOG = LoggerFactory.getLogger(RoutedRpcMetadata.class);
30     static final String ROUTED_RPC_IMPLEMENTATION = "routed-rpc-implementation";
31
32     private final String id;
33     private final String interfaceName;
34     private final String implementationRefId;
35     private ExtendedBlueprintContainer container;
36
37     RoutedRpcMetadata(String id, String interfaceName, String implementationRefId) {
38         this.id = id;
39         this.interfaceName = interfaceName;
40         this.implementationRefId = implementationRefId;
41     }
42
43     @Override
44     public String getId() {
45         return id;
46     }
47
48     @Override
49     public int getActivation() {
50         return ACTIVATION_LAZY;
51     }
52
53     @Override
54     public List<String> getDependsOn() {
55         return Arrays.asList(OpendaylightNamespaceHandler.RPC_REGISTRY_NAME, implementationRefId);
56     }
57
58     @Override
59     public void init(ExtendedBlueprintContainer newContainer) {
60         this.container = newContainer;
61
62         LOG.debug("{}: In init", logName());
63     }
64
65     @SuppressWarnings("checkstyle:IllegalCatch")
66     @Override
67     public Object create() throws ComponentDefinitionException {
68         RpcProviderRegistry rpcRegistry = (RpcProviderRegistry) container.getComponentInstance(
69                 OpendaylightNamespaceHandler.RPC_REGISTRY_NAME);
70
71         Object implementation = container.getComponentInstance(implementationRefId);
72
73         try {
74             if (!RpcService.class.isAssignableFrom(implementation.getClass())) {
75                 throw new ComponentDefinitionException(String.format(
76                         "Implementation \"ref\" instance %s for \"%s\" is not an RpcService",
77                         implementation.getClass(), ROUTED_RPC_IMPLEMENTATION));
78             }
79
80             List<Class<RpcService>> rpcInterfaces = RpcImplementationBean.getImplementedRpcServiceInterfaces(
81                     interfaceName, implementation.getClass(), container.getBundleContext().getBundle(),
82                     ROUTED_RPC_IMPLEMENTATION);
83
84             if (rpcInterfaces.size() > 1) {
85                 throw new ComponentDefinitionException(String.format(
86                         "Implementation \"ref\" instance %s for \"%s\" implements more than one RpcService "
87                         + "interface (%s). Please specify the exact \"interface\"", implementation.getClass(),
88                         ROUTED_RPC_IMPLEMENTATION, rpcInterfaces));
89             }
90
91             Class<RpcService> rpcInterface = rpcInterfaces.iterator().next();
92
93             LOG.debug("{}: create - adding routed implementation {} for RpcService {}", logName(),
94                     implementation, rpcInterface);
95
96             return rpcRegistry.addRoutedRpcImplementation(rpcInterface, (RpcService)implementation);
97         } catch (ComponentDefinitionException e) {
98             throw e;
99         } catch (Exception e) {
100             throw new ComponentDefinitionException(String.format(
101                     "Error processing \"%s\" for %s", ROUTED_RPC_IMPLEMENTATION, implementation.getClass()), e);
102         }
103     }
104
105     @Override
106     public void destroy(Object instance) {
107         LOG.debug("{}: In destroy: instance: {}", logName(), instance);
108
109         ((RoutedRpcRegistration<?>)instance).close();
110     }
111
112     private String logName() {
113         return (container != null ? container.getBundleContext().getBundle().getSymbolicName() : "") + " (" + id + ")";
114     }
115 }