873fd00abe3418fa002510de18a6e3940fd0b108
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / AbstractInvokableServiceMetadata.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import java.util.Set;
16 import java.util.function.Predicate;
17 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
18 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
19 import org.opendaylight.mdsal.dom.api.DOMRpcAvailabilityListener;
20 import org.opendaylight.mdsal.dom.api.DOMRpcIdentifier;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.spi.RpcRoutingStrategy;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.RpcService;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 import org.osgi.service.blueprint.container.ComponentDefinitionException;
29
30 abstract class AbstractInvokableServiceMetadata extends AbstractDependentComponentFactoryMetadata {
31     private final String interfaceName;
32
33     private ListenerRegistration<DOMRpcAvailabilityListener> rpcListenerReg;
34     private RpcConsumerRegistry rpcRegistry;
35     private Class<RpcService> rpcInterface;
36     private Set<SchemaPath> rpcSchemaPaths;
37
38     AbstractInvokableServiceMetadata(final String id, final String interfaceName) {
39         super(id);
40         this.interfaceName = requireNonNull(interfaceName);
41     }
42
43     Class<RpcService> rpcInterface() {
44         return rpcInterface;
45     }
46
47     @SuppressWarnings({ "checkstyle:IllegalCatch", "unchecked" })
48     @Override
49     public final void init(final ExtendedBlueprintContainer container) {
50         super.init(container);
51
52         final Class<?> interfaceClass;
53         try {
54             interfaceClass = container().getBundleContext().getBundle().loadClass(interfaceName);
55         } catch (final Exception e) {
56             throw new ComponentDefinitionException(String.format("%s: Error obtaining interface class %s",
57                     logName(), interfaceName), e);
58         }
59
60         if (!RpcService.class.isAssignableFrom(interfaceClass)) {
61             throw new ComponentDefinitionException(String.format(
62                 "%s: The specified interface %s is not an RpcService", logName(), interfaceName));
63         }
64
65         rpcInterface = (Class<RpcService>)interfaceClass;
66     }
67
68     @Override
69     protected final void startTracking() {
70         // Request RpcProviderRegistry first ...
71         retrieveService("RpcConsumerRegistry", RpcConsumerRegistry.class, this::onRpcRegistry);
72     }
73
74     private void onRpcRegistry(final Object service) {
75         log.debug("{}: Retrieved RpcProviderRegistry {}", logName(), service);
76         rpcRegistry = (RpcConsumerRegistry)service;
77
78         // Now acquire SchemaService...
79         retrieveService("SchemaService", DOMSchemaService.class, this::onSchemaService);
80     }
81
82     private void onSchemaService(final Object service) {
83         log.debug("{}: Retrieved SchemaService {}", logName(), service);
84
85         // Now get the SchemaContext and trigger RPC resolution
86         retrievedSchemaContext(((DOMSchemaService)service).getGlobalContext());
87     }
88
89     private void retrievedSchemaContext(final SchemaContext schemaContext) {
90         log.debug("{}: retrievedSchemaContext", logName());
91
92         final Collection<SchemaPath> schemaPaths = RpcUtil.decomposeRpcService(rpcInterface, schemaContext,
93             rpcFilter());
94         if (schemaPaths.isEmpty()) {
95             log.debug("{}: interface {} has no acceptable entries, assuming it is satisfied", logName(), rpcInterface);
96             setSatisfied();
97             return;
98         }
99
100         rpcSchemaPaths = ImmutableSet.copyOf(schemaPaths);
101         log.debug("{}: Got SchemaPaths: {}", logName(), rpcSchemaPaths);
102
103         // First get the DOMRpcService OSGi service. This will be used to register a listener to be notified
104         // when the underlying DOM RPC service is available.
105         retrieveService("DOMRpcService", DOMRpcService.class, this::retrievedDOMRpcService);
106     }
107
108     private void retrievedDOMRpcService(final Object service) {
109         log.debug("{}: retrievedDOMRpcService {}", logName(), service);
110         final DOMRpcService domRpcService = (DOMRpcService)service;
111
112         setDependencyDesc("Available DOM RPC for binding RPC: " + rpcInterface);
113         rpcListenerReg = domRpcService.registerRpcListener(new DOMRpcAvailabilityListener() {
114             @Override
115             public void onRpcAvailable(final Collection<DOMRpcIdentifier> rpcs) {
116                 onRpcsAvailable(rpcs);
117             }
118
119             @Override
120             public void onRpcUnavailable(final Collection<DOMRpcIdentifier> rpcs) {
121             }
122         });
123     }
124
125     abstract Predicate<RpcRoutingStrategy> rpcFilter();
126
127     @SuppressWarnings("checkstyle:IllegalCatch")
128     @Override
129     public final Object create() throws ComponentDefinitionException {
130         log.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
131
132         super.onCreate();
133
134         try {
135             RpcService rpcService = rpcRegistry.getRpcService(rpcInterface);
136
137             log.debug("{}: create returning service {}", logName(), rpcService);
138
139             return rpcService;
140         } catch (final RuntimeException e) {
141             throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
142         }
143     }
144
145     protected final void onRpcsAvailable(final Collection<DOMRpcIdentifier> rpcs) {
146         for (DOMRpcIdentifier identifier : rpcs) {
147             if (rpcSchemaPaths.contains(identifier.getType())) {
148                 log.debug("{}: onRpcsAvailable - found SchemaPath {}", logName(), identifier.getType());
149                 setSatisfied();
150                 break;
151             }
152         }
153     }
154
155     @Override
156     public final void stopTracking() {
157         super.stopTracking();
158         closeRpcListenerReg();
159     }
160
161     private void closeRpcListenerReg() {
162         if (rpcListenerReg != null) {
163             rpcListenerReg.close();
164             rpcListenerReg = null;
165         }
166     }
167
168     @Override
169     public final void destroy(final Object instance) {
170         super.destroy(instance);
171         closeRpcListenerReg();
172     }
173
174     @Override
175     public final String toString() {
176         return MoreObjects.toStringHelper(this).add("id", getId()).add("interfaceName", interfaceName).toString();
177     }
178 }