Address comments in prior patches
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / RpcServiceMetadata.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 com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.List;
13 import org.apache.aries.blueprint.ext.ComponentFactoryMetadata;
14 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
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 "rpc-service" element that gets an RPC service implementation from
23  * the RpcProviderRegistry and provides it to the Blueprint container.
24  *
25  * @author Thomas Pantelis
26  */
27 class RpcServiceMetadata implements ComponentFactoryMetadata {
28     private static final Logger LOG = LoggerFactory.getLogger(RpcServiceMetadata.class);
29
30     private final String id;
31     private final String interfaceName;
32     private ExtendedBlueprintContainer container;
33
34     RpcServiceMetadata(String id, String interfaceName) {
35         this.id = id;
36         this.interfaceName = interfaceName;
37     }
38
39     @Override
40     public String getId() {
41         return id;
42     }
43
44     @Override
45     public int getActivation() {
46         return ACTIVATION_LAZY;
47     }
48
49     @Override
50     public List<String> getDependsOn() {
51         return Collections.singletonList(OpendaylightNamespaceHandler.RPC_REGISTRY_NAME);
52     }
53
54     @Override
55     public void init(ExtendedBlueprintContainer container) {
56         this.container = container;
57
58         LOG.debug("{}: In init", logName());
59     }
60
61     @SuppressWarnings("unchecked")
62     @Override
63     public Object create() throws ComponentDefinitionException {
64         LOG.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
65
66         RpcProviderRegistry rpcRegistry = (RpcProviderRegistry) container.getComponentInstance(
67                 OpendaylightNamespaceHandler.RPC_REGISTRY_NAME);
68
69         try {
70             Class<?> rpcInterface = container.getBundleContext().getBundle().loadClass(interfaceName);
71             Preconditions.checkArgument(RpcService.class.isAssignableFrom(rpcInterface),
72                     "Specified interface %s is not an RpcService", interfaceName);
73
74             RpcService rpcService = rpcRegistry.getRpcService((Class<RpcService>)rpcInterface);
75
76             LOG.debug("{}: create returning service {}", logName(), rpcService);
77
78             return rpcService;
79         } catch(Exception e) {
80             throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
81         }
82     }
83
84     @Override
85     public void destroy(Object instance) {
86     }
87
88     private String logName() {
89         return (container != null ? container.getBundleContext().getBundle().getSymbolicName() : "") +
90                 " (" + id + ")";
91     }
92
93     @Override
94     public String toString() {
95         return "RpcServiceMetadata [id=" + id + ", interfaceName=" + interfaceName + ", container=" + container + "]";
96     }
97 }