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.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
15 import java.util.function.Predicate;
16 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
17 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
18 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
19 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
20 import org.opendaylight.mdsal.dom.api.DOMRpcService;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.mdsal.dom.spi.RpcRoutingStrategy;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.RpcService;
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;
29 abstract class AbstractInvokableServiceMetadata extends AbstractDependentComponentFactoryMetadata {
30 private final String interfaceName;
32 private ListenerRegistration<DOMRpcAvailabilityListener> rpcListenerReg;
33 private RpcConsumerRegistry rpcRegistry;
34 private Class<RpcService> rpcInterface;
35 private Set<SchemaPath> rpcSchemaPaths;
37 AbstractInvokableServiceMetadata(final String id, final String interfaceName) {
39 this.interfaceName = Preconditions.checkNotNull(interfaceName);
42 Class<RpcService> rpcInterface() {
46 @SuppressWarnings({ "checkstyle:IllegalCatch", "unchecked" })
48 public final void init(final ExtendedBlueprintContainer container) {
49 super.init(container);
51 final Class<?> interfaceClass;
53 interfaceClass = container().getBundleContext().getBundle().loadClass(interfaceName);
54 } catch (final Exception e) {
55 throw new ComponentDefinitionException(String.format("%s: Error obtaining interface class %s",
56 logName(), interfaceName), e);
59 if (!RpcService.class.isAssignableFrom(interfaceClass)) {
60 throw new ComponentDefinitionException(String.format(
61 "%s: The specified interface %s is not an RpcService", logName(), interfaceName));
64 rpcInterface = (Class<RpcService>)interfaceClass;
68 protected final void startTracking() {
69 // Request RpcProviderRegistry first ...
70 retrieveService("RpcConsumerRegistry", RpcConsumerRegistry.class, this::onRpcRegistry);
73 private void onRpcRegistry(final Object service) {
74 log.debug("{}: Retrieved RpcProviderRegistry {}", logName(), service);
75 rpcRegistry = (RpcConsumerRegistry)service;
77 // Now acquire SchemaService...
78 retrieveService("SchemaService", DOMSchemaService.class, this::onSchemaService);
81 private void onSchemaService(final Object service) {
82 log.debug("{}: Retrieved SchemaService {}", logName(), service);
84 // Now get the SchemaContext and trigger RPC resolution
85 retrievedSchemaContext(((DOMSchemaService)service).getGlobalContext());
88 private void retrievedSchemaContext(final SchemaContext schemaContext) {
89 log.debug("{}: retrievedSchemaContext", logName());
91 final Collection<SchemaPath> schemaPaths = RpcUtil.decomposeRpcService(rpcInterface, schemaContext,
93 if (schemaPaths.isEmpty()) {
94 log.debug("{}: interface {} has no acceptable entries, assuming it is satisfied", logName(), rpcInterface);
99 rpcSchemaPaths = ImmutableSet.copyOf(schemaPaths);
100 log.debug("{}: Got SchemaPaths: {}", logName(), rpcSchemaPaths);
102 // First get the DOMRpcService OSGi service. This will be used to register a listener to be notified
103 // when the underlying DOM RPC service is available.
104 retrieveService("DOMRpcService", DOMRpcService.class, this::retrievedDOMRpcService);
107 private void retrievedDOMRpcService(final Object service) {
108 log.debug("{}: retrievedDOMRpcService {}", logName(), service);
109 final DOMRpcService domRpcService = (DOMRpcService)service;
111 setDependencyDesc("Available DOM RPC for binding RPC: " + rpcInterface);
112 rpcListenerReg = domRpcService.registerRpcListener(new DOMRpcAvailabilityListener() {
114 public void onRpcAvailable(final Collection<DOMRpcIdentifier> rpcs) {
115 onRpcsAvailable(rpcs);
119 public void onRpcUnavailable(final Collection<DOMRpcIdentifier> rpcs) {
124 abstract Predicate<RpcRoutingStrategy> rpcFilter();
126 @SuppressWarnings("checkstyle:IllegalCatch")
128 public final Object create() throws ComponentDefinitionException {
129 log.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
134 RpcService rpcService = rpcRegistry.getRpcService(rpcInterface);
136 log.debug("{}: create returning service {}", logName(), rpcService);
139 } catch (final RuntimeException e) {
140 throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
144 protected final void onRpcsAvailable(final Collection<DOMRpcIdentifier> rpcs) {
145 for (DOMRpcIdentifier identifier : rpcs) {
146 if (rpcSchemaPaths.contains(identifier.getType())) {
147 log.debug("{}: onRpcsAvailable - found SchemaPath {}", logName(), identifier.getType());
155 public final void stopTracking() {
156 super.stopTracking();
157 closeRpcListenerReg();
160 private void closeRpcListenerReg() {
161 if (rpcListenerReg != null) {
162 rpcListenerReg.close();
163 rpcListenerReg = null;
168 public final void destroy(final Object instance) {
169 super.destroy(instance);
170 closeRpcListenerReg();
174 public final String toString() {
175 return MoreObjects.toStringHelper(this).add("id", getId()).add("interfaceName", interfaceName).toString();