2 * Copyright (c) 2014 Cisco 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.sal.dom.broker.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
17 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
18 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
19 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
20 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration;
21 import org.opendaylight.controller.sal.core.api.Broker.RpcRegistration;
22 import org.opendaylight.controller.sal.core.api.RoutedRpcDefaultImplementation;
23 import org.opendaylight.controller.sal.core.api.RpcImplementation;
24 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
25 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
26 import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter;
27 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
28 import org.opendaylight.yangtools.concepts.Identifiable;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
34 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.base.Optional;
46 import com.google.common.collect.ImmutableSet;
48 public class SchemaAwareRpcBroker implements RpcRouter, Identifiable<String>, RoutedRpcDefaultImplementation {
50 private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareRpcBroker.class);
52 private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
53 "2013-07-09", "context-reference");
54 private final ListenerRegistry<RpcRegistrationListener> rpcRegistrationListeners = new ListenerRegistry<>();
55 private final ListenerRegistry<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeChangeListeners = new ListenerRegistry<>();
58 private final String identifier;
59 private final ConcurrentMap<QName, RpcImplementation> implementations = new ConcurrentHashMap<>();
60 private RpcImplementation defaultImplementation;
61 private SchemaContextProvider schemaProvider;
62 private RoutedRpcDefaultImplementation defaultDelegate;
64 public SchemaAwareRpcBroker(String identifier, SchemaContextProvider schemaProvider) {
66 this.identifier = identifier;
67 this.schemaProvider = schemaProvider;
70 public RpcImplementation getDefaultImplementation() {
71 return defaultImplementation;
74 public void setDefaultImplementation(RpcImplementation defaultImplementation) {
75 this.defaultImplementation = defaultImplementation;
78 public SchemaContextProvider getSchemaProvider() {
79 return schemaProvider;
82 public void setSchemaProvider(SchemaContextProvider schemaProvider) {
83 this.schemaProvider = schemaProvider;
86 public RoutedRpcDefaultImplementation getRoutedRpcDefaultDelegate() {
87 return defaultDelegate;
91 public void setRoutedRpcDefaultDelegate(RoutedRpcDefaultImplementation defaultDelegate) {
92 this.defaultDelegate = defaultDelegate;
96 public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
97 checkArgument(rpcType != null, "RPC Type should not be null");
98 checkArgument(implementation != null, "RPC Implementatoin should not be null");
99 return getOrCreateRoutedRpcRouter(rpcType).addRoutedRpcImplementation(rpcType, implementation);
102 private RoutedRpcSelector getOrCreateRoutedRpcRouter(QName rpcType) {
103 RoutedRpcSelector potential = getRoutedRpcRouter(rpcType);
104 if (potential != null) {
107 synchronized (implementations) {
108 potential = getRoutedRpcRouter(rpcType);
109 if (potential != null) {
112 RpcDefinition definition = findRpcDefinition(rpcType);
113 RoutingStrategy strategy = getRoutingStrategy(definition);
114 checkState(strategy instanceof RoutedRpcStrategy, "Rpc %s is not routed.", rpcType);
115 potential = new RoutedRpcSelector((RoutedRpcStrategy) strategy, this);
116 implementations.put(rpcType, potential);
121 private RoutedRpcSelector getRoutedRpcRouter(QName rpcType) {
122 RpcImplementation potential = implementations.get(rpcType);
123 if (potential != null) {
124 checkState(potential instanceof RoutedRpcSelector, "Rpc %s is not routed.", rpcType);
125 return (RoutedRpcSelector) potential;
132 public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
133 throws IllegalArgumentException {
134 checkArgument(rpcType != null, "RPC Type should not be null");
135 checkArgument(implementation != null, "RPC Implementatoin should not be null");
136 checkState(!hasRpcImplementation(rpcType), "Implementation already registered");
137 RpcDefinition definition = findRpcDefinition(rpcType);
138 checkArgument(!isRoutedRpc(definition), "RPC Type must not be routed.");
139 GlobalRpcRegistration reg = new GlobalRpcRegistration(rpcType, implementation, this);
140 implementations.putIfAbsent(rpcType, implementation);
144 private boolean isRoutedRpc(RpcDefinition definition) {
145 return getRoutingStrategy(definition) instanceof RoutedRpcStrategy;
149 public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(RpcRegistrationListener listener) {
150 return rpcRegistrationListeners.register(listener);
154 public String getIdentifier() {
159 public Set<QName> getSupportedRpcs() {
160 return ImmutableSet.copyOf(implementations.keySet());
164 public RpcResult<CompositeNode> invokeRpc(QName rpc, CompositeNode input) {
165 return findRpcImplemention(rpc).invokeRpc(rpc, input);
168 private RpcImplementation findRpcImplemention(QName rpc) {
169 checkArgument(rpc != null, "Rpc name should not be null");
170 RpcImplementation potentialImpl = implementations.get(rpc);
171 if (potentialImpl != null) {
172 return potentialImpl;
174 potentialImpl = defaultImplementation;
175 checkState(potentialImpl != null, "Implementation is not available.");
176 return potentialImpl;
179 private boolean hasRpcImplementation(QName rpc) {
180 return implementations.containsKey(rpc);
183 private RpcDefinition findRpcDefinition(QName rpcType) {
184 checkArgument(rpcType != null, "Rpc name must be supplied.");
185 checkState(schemaProvider != null, "Schema Provider is not available.");
186 SchemaContext ctx = schemaProvider.getSchemaContext();
187 checkState(ctx != null, "YANG Schema Context is not available.");
188 Module module = ctx.findModuleByNamespaceAndRevision(rpcType.getNamespace(), rpcType.getRevision());
189 checkState(module != null, "YANG Module is not available.");
190 return findRpcDefinition(rpcType, module.getRpcs());
193 static private RpcDefinition findRpcDefinition(QName rpcType, Set<RpcDefinition> rpcs) {
194 checkState(rpcs != null, "Rpc schema is not available.");
195 for (RpcDefinition rpc : rpcs) {
196 if (rpcType.equals(rpc.getQName())) {
200 throw new IllegalArgumentException("Supplied Rpc Type is not defined.");
203 private RoutingStrategy getRoutingStrategy(RpcDefinition rpc) {
204 ContainerSchemaNode input = rpc.getInput();
206 for (DataSchemaNode schemaNode : input.getChildNodes()) {
207 Optional<QName> context = getRoutingContext(schemaNode);
208 if (context.isPresent()) {
209 return createRoutedStrategy(rpc, context.get(), schemaNode.getQName());
213 return createGlobalStrategy(rpc);
216 private static RoutingStrategy createRoutedStrategy(RpcDefinition rpc, QName context, QName leafNode) {
217 return new RoutedRpcStrategy(rpc.getQName(), context, leafNode);
220 private Optional<QName> getRoutingContext(DataSchemaNode schemaNode) {
221 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
222 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
223 return Optional.fromNullable(extension.getQName());
227 return Optional.absent();
230 private static RoutingStrategy createGlobalStrategy(RpcDefinition rpc) {
231 GlobalRpcStrategy ret = new GlobalRpcStrategy(rpc.getQName());
236 public RpcResult<CompositeNode> invokeRpc(QName rpc, InstanceIdentifier identifier, CompositeNode input) {
237 checkState(defaultDelegate != null);
238 return defaultDelegate.invokeRpc(rpc, identifier, input);
241 private static abstract class RoutingStrategy implements Identifiable<QName> {
243 private final QName identifier;
245 public RoutingStrategy(QName identifier) {
247 this.identifier = identifier;
251 public QName getIdentifier() {
256 private static class GlobalRpcStrategy extends RoutingStrategy {
258 public GlobalRpcStrategy(QName identifier) {
263 private static class RoutedRpcStrategy extends RoutingStrategy {
265 private final QName context;
266 private final QName leaf;
268 public RoutedRpcStrategy(QName identifier, QName ctx, QName leaf) {
274 public QName getContext() {
278 public QName getLeaf() {
283 private static class RoutedRpcSelector implements RpcImplementation, AutoCloseable, Identifiable<QName> {
285 private final RoutedRpcStrategy strategy;
286 private final Set<QName> supportedRpcs;
287 private RpcImplementation defaultDelegate;
288 private final ConcurrentMap<InstanceIdentifier, RoutedRpcRegImpl> implementations = new ConcurrentHashMap<>();
289 private SchemaAwareRpcBroker router;
291 public RoutedRpcSelector(RoutedRpcStrategy strategy, SchemaAwareRpcBroker router) {
293 this.strategy = strategy;
294 supportedRpcs = ImmutableSet.of(strategy.getIdentifier());
295 this.router = router;
299 public QName getIdentifier() {
300 return strategy.getIdentifier();
304 public void close() throws Exception {
309 public Set<QName> getSupportedRpcs() {
310 return supportedRpcs;
313 public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
314 return new RoutedRpcRegImpl(rpcType, implementation, this);
318 public RpcResult<CompositeNode> invokeRpc(QName rpc, CompositeNode input) {
319 CompositeNode inputContainer = input.getFirstCompositeByName(QName.create(rpc,"input"));
320 checkArgument(inputContainer != null, "Rpc payload must contain input element");
321 SimpleNode<?> routeContainer = inputContainer.getFirstSimpleByName(strategy.getLeaf());
322 checkArgument(routeContainer != null, "Leaf %s must be set with value", strategy.getLeaf());
323 Object route = routeContainer.getValue();
324 checkArgument(route instanceof InstanceIdentifier);
325 RpcImplementation potential = null;
327 RoutedRpcRegImpl potentialReg = implementations.get(route);
328 if (potentialReg != null) {
329 potential = potentialReg.getInstance();
332 if (potential == null) {
333 return router.invokeRpc(rpc, (InstanceIdentifier) route, input);
335 checkState(potential != null, "No implementation is available for rpc:%s path:%s", rpc, route);
336 return potential.invokeRpc(rpc, input);
339 public void addPath(QName context, InstanceIdentifier path, RoutedRpcRegImpl routedRpcRegImpl) {
340 //checkArgument(strategy.getContext().equals(context),"Supplied context is not supported.");
341 RoutedRpcRegImpl previous = implementations.put(path, routedRpcRegImpl);
342 if (previous == null) {
343 router.notifyPathAnnouncement(context,strategy.getIdentifier(), path);
348 public void removePath(QName context, InstanceIdentifier path, RoutedRpcRegImpl routedRpcRegImpl) {
349 boolean removed = implementations.remove(path, routedRpcRegImpl);
351 router.notifyPathWithdrawal(context, strategy.getIdentifier(), path);
356 private static class GlobalRpcRegistration extends AbstractObjectRegistration<RpcImplementation> implements
358 private final QName type;
359 private SchemaAwareRpcBroker router;
361 public GlobalRpcRegistration(QName type, RpcImplementation instance, SchemaAwareRpcBroker router) {
364 this.router = router;
368 public QName getType() {
373 protected void removeRegistration() {
374 if (router != null) {
381 private static class RoutedRpcRegImpl extends AbstractObjectRegistration<RpcImplementation> implements
382 RoutedRpcRegistration {
384 private final QName type;
385 private RoutedRpcSelector router;
387 public RoutedRpcRegImpl(QName rpcType, RpcImplementation implementation, RoutedRpcSelector routedRpcSelector) {
388 super(implementation);
390 router = routedRpcSelector;
394 public void registerPath(QName context, InstanceIdentifier path) {
395 router.addPath(context, path, this);
399 public void unregisterPath(QName context, InstanceIdentifier path) {
400 router.removePath(context, path, this);
404 protected void removeRegistration() {
409 public QName getType() {
415 private void remove(GlobalRpcRegistration registration) {
416 implementations.remove(registration.getType(), registration);
419 private void notifyPathAnnouncement(QName context, QName identifier, InstanceIdentifier path) {
420 RpcRoutingContext contextWrapped = RpcRoutingContext.create(context, identifier);
421 RouteChange<RpcRoutingContext, InstanceIdentifier> change = RoutingUtils.announcementChange(contextWrapped , path);
422 for(ListenerRegistration<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeListener : routeChangeListeners) {
424 routeListener.getInstance().onRouteChange(change);
425 } catch (Exception e) {
426 LOG.error("Unhandled exception during invoking onRouteChange for {}",routeListener.getInstance(),e);
435 private void notifyPathWithdrawal(QName context,QName identifier, InstanceIdentifier path) {
436 RpcRoutingContext contextWrapped = RpcRoutingContext.create(context, identifier);
437 RouteChange<RpcRoutingContext, InstanceIdentifier> change = RoutingUtils.removalChange(contextWrapped , path);
438 for(ListenerRegistration<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeListener : routeChangeListeners) {
440 routeListener.getInstance().onRouteChange(change);
441 } catch (Exception e) {
442 LOG.error("Unhandled exception during invoking onRouteChange for {}",routeListener.getInstance(),e);
448 public <L extends RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> ListenerRegistration<L> registerRouteChangeListener(
450 return routeChangeListeners.registerWithType(listener);