1 package org.opendaylight.controller.sal.dom.broker.impl;
3 import static com.google.common.base.Preconditions.checkArgument;
4 import static com.google.common.base.Preconditions.checkState;
7 import java.util.concurrent.ConcurrentHashMap;
8 import java.util.concurrent.ConcurrentMap;
10 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
11 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
12 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
13 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration;
14 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
15 import org.opendaylight.controller.sal.core.api.RpcImplementation;
16 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
17 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
18 import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter;
19 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
20 import org.opendaylight.yangtools.concepts.Identifiable;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
26 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.common.base.Optional;
38 import com.google.common.collect.ImmutableSet;
40 public class SchemaAwareRpcBroker implements RpcRouter, Identifiable<String> {
42 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareRpcBroker.class);
44 private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
45 "2013-07-09", "context-reference");
46 private final ListenerRegistry<RpcRegistrationListener> rpcRegistrationListeners = new ListenerRegistry<>();
47 private final ListenerRegistry<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeChangeListeners = new ListenerRegistry<>();
50 private final String identifier;
51 private final ConcurrentMap<QName, RpcImplementation> implementations = new ConcurrentHashMap<>();
52 private RpcImplementation defaultImplementation;
53 private SchemaContextProvider schemaProvider;
55 public SchemaAwareRpcBroker(String identifier, SchemaContextProvider schemaProvider) {
57 this.identifier = identifier;
58 this.schemaProvider = schemaProvider;
61 public RpcImplementation getDefaultImplementation() {
62 return defaultImplementation;
65 public void setDefaultImplementation(RpcImplementation defaultImplementation) {
66 this.defaultImplementation = defaultImplementation;
69 public SchemaContextProvider getSchemaProvider() {
70 return schemaProvider;
73 public void setSchemaProvider(SchemaContextProvider schemaProvider) {
74 this.schemaProvider = schemaProvider;
78 public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
79 checkArgument(rpcType != null, "RPC Type should not be null");
80 checkArgument(implementation != null, "RPC Implementatoin should not be null");
81 return getOrCreateRoutedRpcRouter(rpcType).addRoutedRpcImplementation(rpcType, implementation);
84 private RoutedRpcSelector getOrCreateRoutedRpcRouter(QName rpcType) {
85 RoutedRpcSelector potential = getRoutedRpcRouter(rpcType);
86 if (potential != null) {
89 synchronized (implementations) {
90 potential = getRoutedRpcRouter(rpcType);
91 if (potential != null) {
94 RpcDefinition definition = findRpcDefinition(rpcType);
95 RoutingStrategy strategy = getRoutingStrategy(definition);
96 checkState(strategy instanceof RoutedRpcStrategy, "Rpc %s is not routed.", rpcType);
97 potential = new RoutedRpcSelector((RoutedRpcStrategy) strategy, this);
98 implementations.put(rpcType, potential);
103 private RoutedRpcSelector getRoutedRpcRouter(QName rpcType) {
104 RpcImplementation potential = implementations.get(rpcType);
105 if (potential != null) {
106 checkState(potential instanceof RoutedRpcSelector, "Rpc %s is not routed.", rpcType);
107 return (RoutedRpcSelector) potential;
114 public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
115 throws IllegalArgumentException {
116 checkArgument(rpcType != null, "RPC Type should not be null");
117 checkArgument(implementation != null, "RPC Implementatoin should not be null");
118 checkState(!hasRpcImplementation(rpcType), "Implementation already registered");
119 RpcDefinition definition = findRpcDefinition(rpcType);
120 checkArgument(!isRoutedRpc(definition), "RPC Type must not be routed.");
121 GlobalRpcRegistration reg = new GlobalRpcRegistration(rpcType, implementation, this);
125 private boolean isRoutedRpc(RpcDefinition definition) {
126 return getRoutingStrategy(definition) instanceof RoutedRpcStrategy;
130 public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(RpcRegistrationListener listener) {
131 return rpcRegistrationListeners.register(listener);
135 public String getIdentifier() {
140 public Set<QName> getSupportedRpcs() {
141 return ImmutableSet.copyOf(implementations.keySet());
145 public RpcResult<CompositeNode> invokeRpc(QName rpc, CompositeNode input) {
146 return findRpcImplemention(rpc).invokeRpc(rpc, input);
149 private RpcImplementation findRpcImplemention(QName rpc) {
150 checkArgument(rpc != null, "Rpc name should not be null");
151 RpcImplementation potentialImpl = implementations.get(rpc);
152 if (potentialImpl != null) {
153 return potentialImpl;
155 potentialImpl = defaultImplementation;
156 checkState(potentialImpl != null, "Implementation is not available.");
157 return potentialImpl;
160 private boolean hasRpcImplementation(QName rpc) {
161 return implementations.containsKey(rpc);
164 private RpcDefinition findRpcDefinition(QName rpcType) {
165 checkArgument(rpcType != null, "Rpc name must be supplied.");
166 checkState(schemaProvider != null, "Schema Provider is not available.");
167 SchemaContext ctx = schemaProvider.getSchemaContext();
168 checkState(ctx != null, "YANG Schema Context is not available.");
169 Module module = ctx.findModuleByNamespaceAndRevision(rpcType.getNamespace(), rpcType.getRevision());
170 checkState(module != null, "YANG Module is not available.");
171 return findRpcDefinition(rpcType, module.getRpcs());
174 static private RpcDefinition findRpcDefinition(QName rpcType, Set<RpcDefinition> rpcs) {
175 checkState(rpcs != null, "Rpc schema is not available.");
176 for (RpcDefinition rpc : rpcs) {
177 if (rpcType.equals(rpc.getQName())) {
181 throw new IllegalArgumentException("Supplied Rpc Type is not defined.");
184 private RoutingStrategy getRoutingStrategy(RpcDefinition rpc) {
185 ContainerSchemaNode input = rpc.getInput();
187 for (DataSchemaNode schemaNode : input.getChildNodes()) {
188 Optional<QName> context = getRoutingContext(schemaNode);
189 if (context.isPresent()) {
190 return createRoutedStrategy(rpc, context.get(), schemaNode.getQName());
194 return createGlobalStrategy(rpc);
197 private static RoutingStrategy createRoutedStrategy(RpcDefinition rpc, QName context, QName leafNode) {
198 return new RoutedRpcStrategy(rpc.getQName(), context, leafNode);
201 private Optional<QName> getRoutingContext(DataSchemaNode schemaNode) {
202 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
203 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
204 return Optional.fromNullable(extension.getQName());
208 return Optional.absent();
211 private static RoutingStrategy createGlobalStrategy(RpcDefinition rpc) {
212 GlobalRpcStrategy ret = new GlobalRpcStrategy(rpc.getQName());
216 private static abstract class RoutingStrategy implements Identifiable<QName> {
218 private final QName identifier;
220 public RoutingStrategy(QName identifier) {
222 this.identifier = identifier;
226 public QName getIdentifier() {
231 private static class GlobalRpcStrategy extends RoutingStrategy {
233 public GlobalRpcStrategy(QName identifier) {
238 private static class RoutedRpcStrategy extends RoutingStrategy {
240 private final QName context;
241 private final QName leaf;
243 public RoutedRpcStrategy(QName identifier, QName ctx, QName leaf) {
249 public QName getContext() {
253 public QName getLeaf() {
258 private static class RoutedRpcSelector implements RpcImplementation, AutoCloseable, Identifiable<QName> {
260 private final RoutedRpcStrategy strategy;
261 private final Set<QName> supportedRpcs;
262 private RpcImplementation defaultDelegate;
263 private final ConcurrentMap<InstanceIdentifier, RoutedRpcRegImpl> implementations = new ConcurrentHashMap<>();
264 private SchemaAwareRpcBroker router;
266 public RoutedRpcSelector(RoutedRpcStrategy strategy, SchemaAwareRpcBroker router) {
268 this.strategy = strategy;
269 supportedRpcs = ImmutableSet.of(strategy.getIdentifier());
270 this.router = router;
274 public QName getIdentifier() {
275 return strategy.getIdentifier();
279 public void close() throws Exception {
284 public Set<QName> getSupportedRpcs() {
285 return supportedRpcs;
288 public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
289 return new RoutedRpcRegImpl(rpcType, implementation, this);
293 public RpcResult<CompositeNode> invokeRpc(QName rpc, CompositeNode input) {
294 CompositeNode inputContainer = input.getFirstCompositeByName(QName.create(rpc,"input"));
295 checkArgument(inputContainer != null, "Rpc payload must contain input element");
296 SimpleNode<?> routeContainer = inputContainer.getFirstSimpleByName(strategy.getLeaf());
297 checkArgument(routeContainer != null, "Leaf %s must be set with value", strategy.getLeaf());
298 Object route = routeContainer.getValue();
299 RpcImplementation potential = null;
301 RoutedRpcRegImpl potentialReg = implementations.get(route);
302 if (potentialReg != null) {
303 potential = potentialReg.getInstance();
306 if (potential == null) {
307 potential = defaultDelegate;
309 checkState(potential != null, "No implementation is available for rpc:%s path:%s", rpc, route);
310 return potential.invokeRpc(rpc, input);
313 public void addPath(QName context, InstanceIdentifier path, RoutedRpcRegImpl routedRpcRegImpl) {
314 //checkArgument(strategy.getContext().equals(context),"Supplied context is not supported.");
315 RoutedRpcRegImpl previous = implementations.put(path, routedRpcRegImpl);
316 if (previous == null) {
317 router.notifyPathAnnouncement(context,strategy.getIdentifier(), path);
322 public void removePath(QName context, InstanceIdentifier path, RoutedRpcRegImpl routedRpcRegImpl) {
323 boolean removed = implementations.remove(path, routedRpcRegImpl);
325 router.notifyPathWithdrawal(context, strategy.getIdentifier(), path);
330 private static class GlobalRpcRegistration extends AbstractObjectRegistration<RpcImplementation> implements
332 private final QName type;
333 private SchemaAwareRpcBroker router;
335 public GlobalRpcRegistration(QName type, RpcImplementation instance, SchemaAwareRpcBroker router) {
338 this.router = router;
342 public QName getType() {
347 protected void removeRegistration() {
348 if (router != null) {
355 private static class RoutedRpcRegImpl extends AbstractObjectRegistration<RpcImplementation> implements
356 RoutedRpcRegistration {
358 private final QName type;
359 private RoutedRpcSelector router;
361 public RoutedRpcRegImpl(QName rpcType, RpcImplementation implementation, RoutedRpcSelector routedRpcSelector) {
362 super(implementation);
364 router = routedRpcSelector;
368 public void registerPath(QName context, InstanceIdentifier path) {
369 router.addPath(context, path, this);
373 public void unregisterPath(QName context, InstanceIdentifier path) {
374 router.removePath(context, path, this);
378 protected void removeRegistration() {
383 public QName getType() {
389 private void remove(GlobalRpcRegistration registration) {
390 implementations.remove(registration.getType(), registration);
393 private void notifyPathAnnouncement(QName context, QName identifier, InstanceIdentifier path) {
394 RpcRoutingContext contextWrapped = RpcRoutingContext.create(context, identifier);
395 RouteChange<RpcRoutingContext, InstanceIdentifier> change = RoutingUtils.announcementChange(contextWrapped , path);
396 for(ListenerRegistration<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeListener : routeChangeListeners) {
398 routeListener.getInstance().onRouteChange(change);
399 } catch (Exception e) {
400 LOG.error("Unhandled exception during invoking onRouteChange for {}",routeListener.getInstance(),e);
409 private void notifyPathWithdrawal(QName context,QName identifier, InstanceIdentifier path) {
410 RpcRoutingContext contextWrapped = RpcRoutingContext.create(context, identifier);
411 RouteChange<RpcRoutingContext, InstanceIdentifier> change = RoutingUtils.removalChange(contextWrapped , path);
412 for(ListenerRegistration<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeListener : routeChangeListeners) {
414 routeListener.getInstance().onRouteChange(change);
415 } catch (Exception e) {
416 LOG.error("Unhandled exception during invoking onRouteChange for {}",routeListener.getInstance(),e);
422 public <L extends RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> ListenerRegistration<L> registerRouteChangeListener(
424 return routeChangeListeners.registerWithType(listener);