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.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.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
28 import com.google.common.collect.ImmutableSet;
29 import com.google.common.util.concurrent.ListenableFuture;
31 class RoutedRpcSelector implements RpcImplementation, AutoCloseable, Identifiable<RpcRoutingContext> {
33 private final RpcRoutingStrategy strategy;
34 private final Set<QName> supportedRpcs;
35 private final RpcRoutingContext identifier;
36 final ConcurrentMap<InstanceIdentifier, RoutedRpcRegImpl> implementations = new ConcurrentHashMap<>();
37 private final SchemaAwareRpcBroker router;
39 public RoutedRpcSelector(final RpcRoutingStrategy strategy, final SchemaAwareRpcBroker router) {
41 this.strategy = strategy;
42 supportedRpcs = ImmutableSet.of(strategy.getIdentifier());
43 identifier = RpcRoutingContext.create(strategy.getContext(), strategy.getIdentifier());
48 public RpcRoutingContext getIdentifier() {
53 public void close() throws Exception {
58 public Set<QName> getSupportedRpcs() {
62 public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
63 return new RoutedRpcRegImpl(rpcType, implementation, this);
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 InstanceIdentifier,
74 "The routed node %s is not an instance identifier", route);
75 RpcImplementation potential = null;
77 RoutedRpcRegImpl potentialReg = implementations.get(route);
78 if (potentialReg != null) {
79 potential = potentialReg.getInstance();
82 if (potential == null) {
83 return router.invokeRpc(rpc, (InstanceIdentifier) route, input);
85 checkState(potential != null, "No implementation is available for rpc:%s path:%s", rpc, route);
86 return potential.invokeRpc(rpc, input);
89 public void addPath(final QName context, final InstanceIdentifier 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);
98 public void removePath(final QName context, final InstanceIdentifier path, final RoutedRpcRegImpl routedRpcRegImpl) {
99 boolean removed = implementations.remove(path, routedRpcRegImpl);
101 router.notifyPathWithdrawal(context, strategy.getIdentifier(), path);