BUG 2854 : Do not add empty read write transactions to the replicable journal
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / impl / RoutedRpcSelector.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
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.util.concurrent.ListenableFuture;
14
15 import java.util.Set;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
18
19 import org.opendaylight.controller.md.sal.dom.broker.spi.rpc.RpcRoutingStrategy;
20 import org.opendaylight.controller.sal.core.api.Broker.RoutedRpcRegistration;
21 import org.opendaylight.controller.sal.core.api.RpcImplementation;
22 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
23 import org.opendaylight.yangtools.concepts.Identifiable;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29
30 class RoutedRpcSelector implements RpcImplementation, AutoCloseable, Identifiable<RpcRoutingContext> {
31
32     private final RpcRoutingStrategy strategy;
33     private final Set<QName> supportedRpcs;
34     private final RpcRoutingContext identifier;
35     final ConcurrentMap<YangInstanceIdentifier, RoutedRpcRegImpl> implementations = new ConcurrentHashMap<>();
36     private final SchemaAwareRpcBroker router;
37
38     public RoutedRpcSelector(final RpcRoutingStrategy strategy, final SchemaAwareRpcBroker router) {
39         super();
40         this.strategy = strategy;
41         supportedRpcs = ImmutableSet.of(strategy.getIdentifier());
42         identifier = RpcRoutingContext.create(strategy.getContext(), strategy.getIdentifier());
43         this.router = router;
44     }
45
46     @Override
47     public RpcRoutingContext getIdentifier() {
48         return identifier;
49     }
50
51     @Override
52     public void close() throws Exception {
53
54     }
55
56     @Override
57     public Set<QName> getSupportedRpcs() {
58         return supportedRpcs;
59     }
60
61     public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
62         return new RoutedRpcRegImpl(rpcType, implementation, this);
63     }
64
65     @Override
66     public ListenableFuture<RpcResult<CompositeNode>> invokeRpc(final QName rpc, final CompositeNode input) {
67         CompositeNode inputContainer = input.getFirstCompositeByName(QName.create(rpc,"input"));
68         checkArgument(inputContainer != null, "Rpc payload must contain input element");
69         SimpleNode<?> routeContainer = inputContainer.getFirstSimpleByName(strategy.getLeaf());
70         checkArgument(routeContainer != null, "Leaf %s must be set with value", strategy.getLeaf());
71         Object route = routeContainer.getValue();
72         checkArgument(route instanceof YangInstanceIdentifier,
73                       "The routed node %s is not an instance identifier", route);
74         RpcImplementation potential = null;
75         if (route != null) {
76             RoutedRpcRegImpl potentialReg = implementations.get(route);
77             if (potentialReg != null) {
78                 potential = potentialReg.getInstance();
79             }
80         }
81         if (potential == null) {
82             return router.invokeRpc(rpc, (YangInstanceIdentifier) route, input);
83         } else {
84             return potential.invokeRpc(rpc, input);
85         }
86     }
87
88     public void addPath(final QName context, final YangInstanceIdentifier path, final RoutedRpcRegImpl routedRpcRegImpl) {
89         //checkArgument(strategy.getContext().equals(context),"Supplied context is not supported.");
90         RoutedRpcRegImpl previous = implementations.put(path, routedRpcRegImpl);
91         if (previous == null) {
92             router.notifyPathAnnouncement(context,strategy.getIdentifier(), path);
93         }
94
95     }
96
97     public void removePath(final QName context, final YangInstanceIdentifier path, final RoutedRpcRegImpl routedRpcRegImpl) {
98         boolean removed = implementations.remove(path, routedRpcRegImpl);
99         if (removed) {
100             router.notifyPathWithdrawal(context, strategy.getIdentifier(), path);
101         }
102     }
103 }