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;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.concurrent.ConcurrentHashMap;
17 import java.util.concurrent.ConcurrentMap;
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;
30 class RoutedRpcSelector implements RpcImplementation, AutoCloseable, Identifiable<RpcRoutingContext> {
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;
38 public RoutedRpcSelector(final RpcRoutingStrategy strategy, final SchemaAwareRpcBroker router) {
40 this.strategy = strategy;
41 supportedRpcs = ImmutableSet.of(strategy.getIdentifier());
42 identifier = RpcRoutingContext.create(strategy.getContext(), strategy.getIdentifier());
47 public RpcRoutingContext getIdentifier() {
52 public void close() throws Exception {
57 public Set<QName> getSupportedRpcs() {
61 public RoutedRpcRegistration addRoutedRpcImplementation(final QName rpcType, final RpcImplementation implementation) {
62 return new RoutedRpcRegImpl(rpcType, implementation, this);
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;
76 RoutedRpcRegImpl potentialReg = implementations.get(route);
77 if (potentialReg != null) {
78 potential = potentialReg.getInstance();
81 if (potential == null) {
82 return router.invokeRpc(rpc, (YangInstanceIdentifier) route, input);
84 return potential.invokeRpc(rpc, input);
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);
97 public void removePath(final QName context, final YangInstanceIdentifier path, final RoutedRpcRegImpl routedRpcRegImpl) {
98 boolean removed = implementations.remove(path, routedRpcRegImpl);
100 router.notifyPathWithdrawal(context, strategy.getIdentifier(), path);