2 * Copyright (c) 2016 Brocade Communications 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.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;
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.
25 * @author Thomas Pantelis
27 class RpcServiceMetadata implements ComponentFactoryMetadata {
28 private static final Logger LOG = LoggerFactory.getLogger(RpcServiceMetadata.class);
30 private final String id;
31 private final String interfaceName;
32 private ExtendedBlueprintContainer container;
34 RpcServiceMetadata(String id, String interfaceName) {
36 this.interfaceName = interfaceName;
40 public String getId() {
45 public int getActivation() {
46 return ACTIVATION_LAZY;
50 public List<String> getDependsOn() {
51 return Collections.singletonList(OpendaylightNamespaceHandler.RPC_REGISTRY_NAME);
55 public void init(ExtendedBlueprintContainer container) {
56 this.container = container;
58 LOG.debug("{}: In init", logName());
61 @SuppressWarnings("unchecked")
63 public Object create() throws ComponentDefinitionException {
64 LOG.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
66 RpcProviderRegistry rpcRegistry = (RpcProviderRegistry) container.getComponentInstance(
67 OpendaylightNamespaceHandler.RPC_REGISTRY_NAME);
70 Class<?> rpcInterface = container.getBundleContext().getBundle().loadClass(interfaceName);
71 Preconditions.checkArgument(RpcService.class.isAssignableFrom(rpcInterface),
72 "Specified interface %s is not an RpcService", interfaceName);
74 RpcService rpcService = rpcRegistry.getRpcService((Class<RpcService>)rpcInterface);
76 LOG.debug("{}: create returning service {}", logName(), rpcService);
79 } catch(Exception e) {
80 throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
85 public void destroy(Object instance) {
88 private String logName() {
89 return (container != null ? container.getBundleContext().getBundle().getSymbolicName() : "") +
94 public String toString() {
95 StringBuilder builder = new StringBuilder();
96 builder.append("RpcServiceMetadata [id=").append(id).append(", interfaceName=").append(interfaceName)
98 return builder.toString();