8fc6fe229500ecc39506fcc5b215b82cfdd9cb5e
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / SchemaAwareRpcBroker.java
1 /*
2  * Copyright (c) 2014 Cisco 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.sal.dom.broker.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.Set;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
16
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;
44
45 import com.google.common.base.Optional;
46 import com.google.common.collect.FluentIterable;
47 import com.google.common.collect.ImmutableMap;
48 import com.google.common.collect.ImmutableSet;
49 import com.google.common.util.concurrent.ListenableFuture;
50
51 public class SchemaAwareRpcBroker implements RpcRouter, Identifiable<String>, RoutedRpcDefaultImplementation {
52
53     private static final Logger LOG = LoggerFactory.getLogger(SchemaAwareRpcBroker.class);
54
55     private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
56             "2013-07-09", "context-reference");
57     private final ListenerRegistry<RpcRegistrationListener> rpcRegistrationListeners = new ListenerRegistry<>();
58     private final ListenerRegistry<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeChangeListeners = new ListenerRegistry<>();
59
60
61     private final String identifier;
62     private final ConcurrentMap<QName, RpcImplementation> implementations = new ConcurrentHashMap<>();
63     private RpcImplementation defaultImplementation;
64     private SchemaContextProvider schemaProvider;
65     private RoutedRpcDefaultImplementation defaultDelegate;
66
67     public SchemaAwareRpcBroker(String identifier, SchemaContextProvider schemaProvider) {
68         super();
69         this.identifier = identifier;
70         this.schemaProvider = schemaProvider;
71     }
72
73     public RpcImplementation getDefaultImplementation() {
74         return defaultImplementation;
75     }
76
77     public void setDefaultImplementation(RpcImplementation defaultImplementation) {
78         this.defaultImplementation = defaultImplementation;
79     }
80
81     public SchemaContextProvider getSchemaProvider() {
82         return schemaProvider;
83     }
84
85     public void setSchemaProvider(SchemaContextProvider schemaProvider) {
86         this.schemaProvider = schemaProvider;
87     }
88
89     public RoutedRpcDefaultImplementation getRoutedRpcDefaultDelegate() {
90         return defaultDelegate;
91     }
92
93     @Override
94     public void setRoutedRpcDefaultDelegate(RoutedRpcDefaultImplementation defaultDelegate) {
95         this.defaultDelegate = defaultDelegate;
96     }
97
98     @Override
99     public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
100         checkArgument(rpcType != null, "RPC Type should not be null");
101         checkArgument(implementation != null, "RPC Implementatoin should not be null");
102         return getOrCreateRoutedRpcRouter(rpcType).addRoutedRpcImplementation(rpcType, implementation);
103     }
104
105     private RoutedRpcSelector getOrCreateRoutedRpcRouter(QName rpcType) {
106         RoutedRpcSelector potential = getRoutedRpcRouter(rpcType);
107         if (potential != null) {
108             return potential;
109         }
110         synchronized (implementations) {
111             potential = getRoutedRpcRouter(rpcType);
112             if (potential != null) {
113                 return potential;
114             }
115             RpcDefinition definition = findRpcDefinition(rpcType);
116             RoutingStrategy strategy = getRoutingStrategy(definition);
117             checkState(strategy instanceof RoutedRpcStrategy, "Rpc %s is not routed.", rpcType);
118             potential = new RoutedRpcSelector((RoutedRpcStrategy) strategy, this);
119             implementations.put(rpcType, potential);
120             return potential;
121         }
122     }
123
124     private RoutedRpcSelector getRoutedRpcRouter(QName rpcType) {
125         RpcImplementation potential = implementations.get(rpcType);
126         if (potential != null) {
127             checkState(potential instanceof RoutedRpcSelector, "Rpc %s is not routed.", rpcType);
128             return (RoutedRpcSelector) potential;
129         }
130         return null;
131
132     }
133
134     @Override
135     public RpcRegistration addRpcImplementation(QName rpcType, RpcImplementation implementation)
136             throws IllegalArgumentException {
137         checkArgument(rpcType != null, "RPC Type should not be null");
138         checkArgument(implementation != null, "RPC Implementatoin should not be null");
139         checkState(!hasRpcImplementation(rpcType), "Implementation already registered");
140         RpcDefinition definition = findRpcDefinition(rpcType);
141         checkArgument(!isRoutedRpc(definition), "RPC Type must not be routed.");
142         GlobalRpcRegistration reg = new GlobalRpcRegistration(rpcType, implementation, this);
143         implementations.putIfAbsent(rpcType, implementation);
144         return reg;
145     }
146
147     private boolean isRoutedRpc(RpcDefinition definition) {
148         return getRoutingStrategy(definition) instanceof RoutedRpcStrategy;
149     }
150
151     @Override
152     public ListenerRegistration<RpcRegistrationListener> addRpcRegistrationListener(RpcRegistrationListener listener) {
153         return rpcRegistrationListeners.register(listener);
154     }
155
156     @Override
157     public String getIdentifier() {
158         return identifier;
159     }
160
161     @Override
162     public Set<QName> getSupportedRpcs() {
163         return ImmutableSet.copyOf(implementations.keySet());
164     }
165
166     @Override
167     public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
168         return findRpcImplemention(rpc).invokeRpc(rpc, input);
169     }
170
171     private RpcImplementation findRpcImplemention(QName rpc) {
172         checkArgument(rpc != null, "Rpc name should not be null");
173         RpcImplementation potentialImpl = implementations.get(rpc);
174         if (potentialImpl != null) {
175             return potentialImpl;
176         }
177
178         potentialImpl = defaultImplementation;
179         if( potentialImpl == null ) {
180             throw new UnsupportedOperationException( "No implementation for this operation is available." );
181         }
182
183         return potentialImpl;
184     }
185
186     private boolean hasRpcImplementation(QName rpc) {
187         return implementations.containsKey(rpc);
188     }
189
190     private RpcDefinition findRpcDefinition(QName rpcType) {
191         checkArgument(rpcType != null, "Rpc name must be supplied.");
192         checkState(schemaProvider != null, "Schema Provider is not available.");
193         SchemaContext ctx = schemaProvider.getSchemaContext();
194         checkState(ctx != null, "YANG Schema Context is not available.");
195         Module module = ctx.findModuleByNamespaceAndRevision(rpcType.getNamespace(), rpcType.getRevision());
196         checkState(module != null, "YANG Module is not available.");
197         return findRpcDefinition(rpcType, module.getRpcs());
198     }
199
200     static private RpcDefinition findRpcDefinition(QName rpcType, Set<RpcDefinition> rpcs) {
201         checkState(rpcs != null, "Rpc schema is not available.");
202         for (RpcDefinition rpc : rpcs) {
203             if (rpcType.equals(rpc.getQName())) {
204                 return rpc;
205             }
206         }
207         throw new IllegalArgumentException("Supplied Rpc Type is not defined.");
208     }
209
210     private RoutingStrategy getRoutingStrategy(RpcDefinition rpc) {
211         ContainerSchemaNode input = rpc.getInput();
212         if (input != null) {
213             for (DataSchemaNode schemaNode : input.getChildNodes()) {
214                 Optional<QName> context = getRoutingContext(schemaNode);
215                 if (context.isPresent()) {
216                     return createRoutedStrategy(rpc, context.get(), schemaNode.getQName());
217                 }
218             }
219         }
220         return createGlobalStrategy(rpc);
221     }
222
223     private static RoutingStrategy createRoutedStrategy(RpcDefinition rpc, QName context, QName leafNode) {
224         return new RoutedRpcStrategy(rpc.getQName(), context, leafNode);
225     }
226
227     private Optional<QName> getRoutingContext(DataSchemaNode schemaNode) {
228         for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
229             if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
230                 return Optional.fromNullable(extension.getQName());
231             }
232         }
233         return Optional.absent();
234     }
235
236     private static RoutingStrategy createGlobalStrategy(RpcDefinition rpc) {
237         GlobalRpcStrategy ret = new GlobalRpcStrategy(rpc.getQName());
238         return ret;
239     }
240
241     @Override
242     public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, InstanceIdentifier identifier, CompositeNode input) {
243       checkState(defaultDelegate != null);
244       return defaultDelegate.invokeRpc(rpc, identifier, input);
245     }
246
247     private static abstract class RoutingStrategy implements Identifiable<QName> {
248
249         private final QName identifier;
250
251         public RoutingStrategy(QName identifier) {
252             super();
253             this.identifier = identifier;
254         }
255
256         @Override
257         public QName getIdentifier() {
258             return identifier;
259         }
260     }
261
262     private static class GlobalRpcStrategy extends RoutingStrategy {
263
264         public GlobalRpcStrategy(QName identifier) {
265             super(identifier);
266         }
267     }
268
269     private static class RoutedRpcStrategy extends RoutingStrategy {
270
271         private final QName context;
272         private final QName leaf;
273
274         public RoutedRpcStrategy(QName identifier, QName ctx, QName leaf) {
275             super(identifier);
276             this.context = ctx;
277             this.leaf = leaf;
278         }
279
280         public QName getContext() {
281             return context;
282         }
283
284         public QName getLeaf() {
285             return leaf;
286         }
287     }
288
289     private static class RoutedRpcSelector implements RpcImplementation, AutoCloseable, Identifiable<RpcRoutingContext> {
290
291         private final RoutedRpcStrategy strategy;
292         private final Set<QName> supportedRpcs;
293         private final RpcRoutingContext identifier;
294         private RpcImplementation defaultDelegate;
295         private final ConcurrentMap<InstanceIdentifier, RoutedRpcRegImpl> implementations = new ConcurrentHashMap<>();
296         private final SchemaAwareRpcBroker router;
297
298         public RoutedRpcSelector(RoutedRpcStrategy strategy, SchemaAwareRpcBroker router) {
299             super();
300             this.strategy = strategy;
301             supportedRpcs = ImmutableSet.of(strategy.getIdentifier());
302             identifier = RpcRoutingContext.create(strategy.context, strategy.getIdentifier());
303             this.router = router;
304         }
305
306         @Override
307         public RpcRoutingContext getIdentifier() {
308             return identifier;
309         }
310
311         @Override
312         public void close() throws Exception {
313
314         }
315
316         @Override
317         public Set<QName> getSupportedRpcs() {
318             return supportedRpcs;
319         }
320
321         public RoutedRpcRegistration addRoutedRpcImplementation(QName rpcType, RpcImplementation implementation) {
322             return new RoutedRpcRegImpl(rpcType, implementation, this);
323         }
324
325         @Override
326         public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(QName rpc, CompositeNode input) {
327             CompositeNode inputContainer = input.getFirstCompositeByName(QName.create(rpc,"input"));
328             checkArgument(inputContainer != null, "Rpc payload must contain input element");
329             SimpleNode<?> routeContainer = inputContainer.getFirstSimpleByName(strategy.getLeaf());
330             checkArgument(routeContainer != null, "Leaf %s must be set with value", strategy.getLeaf());
331             Object route = routeContainer.getValue();
332             checkArgument(route instanceof InstanceIdentifier,
333                           "The routed node %s is not an instance identifier", route);
334             RpcImplementation potential = null;
335             if (route != null) {
336                 RoutedRpcRegImpl potentialReg = implementations.get(route);
337                 if (potentialReg != null) {
338                     potential = potentialReg.getInstance();
339                 }
340             }
341             if (potential == null) {
342                 return router.invokeRpc(rpc, (InstanceIdentifier) route, input);
343             }
344             checkState(potential != null, "No implementation is available for rpc:%s path:%s", rpc, route);
345             return potential.invokeRpc(rpc, input);
346         }
347
348         public void addPath(QName context, InstanceIdentifier path, RoutedRpcRegImpl routedRpcRegImpl) {
349             //checkArgument(strategy.getContext().equals(context),"Supplied context is not supported.");
350             RoutedRpcRegImpl previous = implementations.put(path, routedRpcRegImpl);
351             if (previous == null) {
352                 router.notifyPathAnnouncement(context,strategy.getIdentifier(), path);
353             }
354
355         }
356
357         public void removePath(QName context, InstanceIdentifier path, RoutedRpcRegImpl routedRpcRegImpl) {
358             boolean removed = implementations.remove(path, routedRpcRegImpl);
359             if (removed) {
360                 router.notifyPathWithdrawal(context, strategy.getIdentifier(), path);
361             }
362         }
363     }
364
365     private static class GlobalRpcRegistration extends AbstractObjectRegistration<RpcImplementation> implements
366             RpcRegistration {
367         private final QName type;
368         private SchemaAwareRpcBroker router;
369
370         public GlobalRpcRegistration(QName type, RpcImplementation instance, SchemaAwareRpcBroker router) {
371             super(instance);
372             this.type = type;
373             this.router = router;
374         }
375
376         @Override
377         public QName getType() {
378             return type;
379         }
380
381         @Override
382         protected void removeRegistration() {
383             if (router != null) {
384                 router.remove(this);
385                 router = null;
386             }
387         }
388     }
389
390     private static class RoutedRpcRegImpl extends AbstractObjectRegistration<RpcImplementation> implements
391             RoutedRpcRegistration {
392
393         private final QName type;
394         private final RoutedRpcSelector router;
395
396         public RoutedRpcRegImpl(QName rpcType, RpcImplementation implementation, RoutedRpcSelector routedRpcSelector) {
397             super(implementation);
398             this.type = rpcType;
399             router = routedRpcSelector;
400         }
401
402         @Override
403         public void registerPath(QName context, InstanceIdentifier path) {
404             router.addPath(context, path, this);
405         }
406
407         @Override
408         public void unregisterPath(QName context, InstanceIdentifier path) {
409             router.removePath(context, path, this);
410         }
411
412         @Override
413         protected void removeRegistration() {
414
415         }
416
417         @Override
418         public QName getType() {
419             return type;
420         }
421
422     }
423
424     private void remove(GlobalRpcRegistration registration) {
425         implementations.remove(registration.getType(), registration);
426     }
427
428     private void notifyPathAnnouncement(QName context, QName identifier, InstanceIdentifier path) {
429         RpcRoutingContext contextWrapped = RpcRoutingContext.create(context, identifier);
430         RouteChange<RpcRoutingContext, InstanceIdentifier> change = RoutingUtils.announcementChange(contextWrapped , path);
431         for(ListenerRegistration<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeListener : routeChangeListeners) {
432             try {
433                 routeListener.getInstance().onRouteChange(change);
434             } catch (Exception e) {
435                 LOG.error("Unhandled exception during invoking onRouteChange for {}",routeListener.getInstance(),e);
436
437             }
438         }
439
440     }
441
442
443
444     private void notifyPathWithdrawal(QName context,QName identifier, InstanceIdentifier path) {
445         RpcRoutingContext contextWrapped = RpcRoutingContext.create(context, identifier);
446         RouteChange<RpcRoutingContext, InstanceIdentifier> change = RoutingUtils.removalChange(contextWrapped , path);
447         for(ListenerRegistration<RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> routeListener : routeChangeListeners) {
448             try {
449                 routeListener.getInstance().onRouteChange(change);
450             } catch (Exception e) {
451                 LOG.error("Unhandled exception during invoking onRouteChange for {}",routeListener.getInstance(),e);
452             }
453         }
454     }
455
456     @Override
457     public <L extends RouteChangeListener<RpcRoutingContext, InstanceIdentifier>> ListenerRegistration<L> registerRouteChangeListener(
458             L listener) {
459         ListenerRegistration<L> reg = routeChangeListeners.registerWithType(listener);
460         RouteChange<RpcRoutingContext, InstanceIdentifier> initial = createInitialRouteChange();
461         try {
462         listener.onRouteChange(initial);
463         } catch (Exception e) {
464             LOG.error("Unhandled exception during sending initial route change event {} to {}",initial,listener, e);
465         }
466         return reg;
467     }
468
469     private RouteChange<RpcRoutingContext, InstanceIdentifier> createInitialRouteChange() {
470         FluentIterable<RoutedRpcSelector> rpcSelectors = FluentIterable.from(implementations.values()).filter(RoutedRpcSelector.class);
471
472
473         ImmutableMap.Builder<RpcRoutingContext, Set<InstanceIdentifier>> announcements = ImmutableMap.builder();
474         ImmutableMap.Builder<RpcRoutingContext, Set<InstanceIdentifier>> removals = ImmutableMap.builder();
475         for (RoutedRpcSelector routedRpcSelector : rpcSelectors) {
476             final RpcRoutingContext context = routedRpcSelector.getIdentifier();
477             final Set<InstanceIdentifier> paths = ImmutableSet.copyOf(routedRpcSelector.implementations.keySet());
478             announcements.put(context, paths);
479         }
480         return RoutingUtils.change(announcements.build(), removals.build());
481     }
482 }