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.mdsal.dom.spi;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.odlext.model.api.ContextReferenceEffectiveStatement;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
21 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
22 private final @NonNull QName identifier;
24 private RpcRoutingStrategy(final QName identifier) {
25 this.identifier = requireNonNull(identifier);
29 * Returns leaf QName in which RPC Route is stored.
30 * @return leaf QName in which RPC Route is stored
31 * @throws UnsupportedOperationException If RPC is not content routed.
32 * ({@link #isContextBasedRouted()} returned <code>false</code>)
34 public abstract QName getLeaf();
37 * Returns identity QName which represents RPC Routing context.
38 * @return identity QName which represents RPC Routing context
39 * @throws UnsupportedOperationException If RPC is not content routed.
40 * ({@link #isContextBasedRouted()} returned <code>false</code>)
42 public abstract QName getContext();
45 public final QName getIdentifier() {
50 * Returns true if RPC is routed by context.
52 * @return true if RPC is routed by content.
54 public abstract boolean isContextBasedRouted();
56 public static RpcRoutingStrategy from(final RpcDefinition rpc) {
57 // FIXME: deprecate context-reference
58 for (EffectiveStatement<?, ?> stmt : rpc.getInput().asEffectiveStatement().effectiveSubstatements()) {
59 if (stmt instanceof SchemaTreeEffectiveStatement) {
60 final Optional<QName> context =
61 stmt.findFirstEffectiveSubstatementArgument(ContextReferenceEffectiveStatement.class);
62 if (context.isPresent()) {
63 return new RoutedRpcStrategy(rpc.getQName(), context.orElseThrow(),
64 ((SchemaTreeEffectiveStatement<?>) stmt).argument());
68 return new GlobalRpcStrategy(rpc.getQName());
71 private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
72 private final QName context;
73 private final QName leaf;
75 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
77 this.context = requireNonNull(ctx);
78 this.leaf = requireNonNull(leaf);
82 public QName getContext() {
87 public QName getLeaf() {
92 public boolean isContextBasedRouted() {
97 private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
99 GlobalRpcStrategy(final QName identifier) {
104 public boolean isContextBasedRouted() {
109 public QName getContext() {
110 throw new UnsupportedOperationException("Non-routed strategy does not have a context");
114 public QName getLeaf() {
115 throw new UnsupportedOperationException("Non-routed strategy does not have a context");