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.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
17 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
20 // FIXME: deprecate context-reference
21 private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
22 "2013-07-09", "context-reference").intern();
23 private final QName identifier;
25 private RpcRoutingStrategy(final QName identifier) {
26 this.identifier = requireNonNull(identifier);
30 * Returns leaf QName in which RPC Route is stored.
31 * @return leaf QName in which RPC Route is stored
32 * @throws UnsupportedOperationException If RPC is not content routed.
33 * ({@link #isContextBasedRouted()} returned <code>false</code>)
35 public abstract QName getLeaf();
38 * Returns identity QName which represents RPC Routing context.
39 * @return identity QName which represents RPC Routing context
40 * @throws UnsupportedOperationException If RPC is not content routed.
41 * ({@link #isContextBasedRouted()} returned <code>false</code>)
43 public abstract QName getContext();
46 public final QName getIdentifier() {
51 * Returns true if RPC is routed by context.
53 * @return true if RPC is routed by content.
55 public abstract boolean isContextBasedRouted();
57 public static RpcRoutingStrategy from(final RpcDefinition rpc) {
58 for (DataSchemaNode schemaNode : rpc.getInput().getChildNodes()) {
59 Optional<QName> context = getRoutingContext(schemaNode);
60 if (context.isPresent()) {
61 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
64 return new GlobalRpcStrategy(rpc.getQName());
67 public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
68 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
69 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
70 return Optional.ofNullable(extension.getQName());
73 return Optional.empty();
76 private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
77 private final QName context;
78 private final QName leaf;
80 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
82 this.context = requireNonNull(ctx);
83 this.leaf = requireNonNull(leaf);
87 public QName getContext() {
92 public QName getLeaf() {
97 public boolean isContextBasedRouted() {
102 private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
104 GlobalRpcStrategy(final QName identifier) {
109 public boolean isContextBasedRouted() {
114 public QName getContext() {
115 throw new UnsupportedOperationException("Non-routed strategy does not have a context");
119 public QName getLeaf() {
120 throw new UnsupportedOperationException("Non-routed strategy does not have a context");