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