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 java.util.Collection;
11 import java.util.HashSet;
13 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
14 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
17 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.RpcService;
21 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.osgi.service.blueprint.container.ComponentDefinitionException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Factory metadata corresponding to the "rpc-service" element that gets an RPC service implementation from
33 * the RpcProviderRegistry and provides it to the Blueprint container.
35 * @author Thomas Pantelis
37 class RpcServiceMetadata extends AbstractDependentComponentFactoryMetadata {
38 private static final Logger LOG = LoggerFactory.getLogger(RpcServiceMetadata.class);
40 private final String interfaceName;
41 private volatile Set<SchemaPath> rpcSchemaPaths;
42 private volatile RpcProviderRegistry rpcRegistry;
43 private volatile ListenerRegistration<DOMRpcAvailabilityListener> rpcListenerReg;
44 private volatile Class<RpcService> rpcInterface;
46 RpcServiceMetadata(final String id, final String interfaceName) {
48 this.interfaceName = interfaceName;
51 @SuppressWarnings({ "checkstyle:IllegalCatch", "unchecked" })
53 public void init(final ExtendedBlueprintContainer container) {
54 super.init(container);
56 final Class<?> interfaceClass;
58 interfaceClass = container().getBundleContext().getBundle().loadClass(interfaceName);
59 } catch (Exception e) {
60 throw new ComponentDefinitionException(String.format("%s: Error obtaining interface class %s",
61 logName(), interfaceName), e);
64 if (!RpcService.class.isAssignableFrom(interfaceClass)) {
65 throw new ComponentDefinitionException(String.format(
66 "%s: The specified interface %s is not an RpcService", logName(), interfaceName));
69 rpcInterface = (Class<RpcService>)interfaceClass;
73 protected void startTracking() {
74 // First get the SchemaContext. This will be used to get the RPC SchemaPaths.
76 retrieveService("SchemaService", SchemaService.class,
77 service -> retrievedSchemaContext(((SchemaService)service).getGlobalContext()));
80 private void retrievedSchemaContext(final SchemaContext schemaContext) {
81 LOG.debug("{}: retrievedSchemaContext", logName());
83 QNameModule moduleName = BindingReflections.getQNameModule(rpcInterface);
84 Module module = schemaContext.findModuleByNamespaceAndRevision(moduleName.getNamespace(),
85 moduleName.getRevision());
87 LOG.debug("{}: Got Module: {}", logName(), module);
89 rpcSchemaPaths = new HashSet<>();
90 for (RpcDefinition rpcDef : module.getRpcs()) {
91 rpcSchemaPaths.add(rpcDef.getPath());
94 LOG.debug("{}: Got SchemaPaths: {}", logName(), rpcSchemaPaths);
96 // First get the DOMRpcService OSGi service. This will be used to register a listener to be notified
97 // when the underlying DOM RPC service is available.
99 retrieveService("DOMRpcService", DOMRpcService.class,
100 service -> retrievedDOMRpcService((DOMRpcService)service));
103 private void retrievedDOMRpcService(final DOMRpcService domRpcService) {
104 LOG.debug("{}: retrievedDOMRpcService", logName());
106 setDependendencyDesc("Available DOM RPC for binding RPC: " + rpcInterface);
107 rpcListenerReg = domRpcService.registerRpcListener(new DOMRpcAvailabilityListener() {
109 public void onRpcAvailable(final Collection<DOMRpcIdentifier> rpcs) {
110 onRpcsAvailable(rpcs);
114 public void onRpcUnavailable(final Collection<DOMRpcIdentifier> rpcs) {
119 protected void onRpcsAvailable(final Collection<DOMRpcIdentifier> rpcs) {
120 for (DOMRpcIdentifier identifier : rpcs) {
121 if (rpcSchemaPaths.contains(identifier.getType())) {
122 LOG.debug("{}: onRpcsAvailable - found SchemaPath {}", logName(), identifier.getType());
124 retrieveService("RpcProviderRegistry", RpcProviderRegistry.class, service -> {
125 rpcRegistry = (RpcProviderRegistry)service;
134 @SuppressWarnings("checkstyle:IllegalCatch")
136 public Object create() throws ComponentDefinitionException {
137 LOG.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
142 RpcService rpcService = rpcRegistry.getRpcService(rpcInterface);
144 LOG.debug("{}: create returning service {}", logName(), rpcService);
147 } catch (RuntimeException e) {
148 throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
153 public void stopTracking() {
154 super.stopTracking();
155 closeRpcListenerReg();
158 private void closeRpcListenerReg() {
159 if (rpcListenerReg != null) {
160 rpcListenerReg.close();
161 rpcListenerReg = null;
166 public void destroy(final Object instance) {
167 super.destroy(instance);
168 closeRpcListenerReg();
172 public String toString() {
173 return "RpcServiceMetadata [id=" + getId() + ", interfaceName=" + interfaceName + "]";