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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.concepts.Identifiable;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
16 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
18 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
20 private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
21 "2013-07-09", "context-reference").intern();
22 private final QName identifier;
24 private RpcRoutingStrategy(final QName identifier) {
25 this.identifier = Preconditions.checkNotNull(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 for (DataSchemaNode schemaNode : rpc.getInput().getChildNodes()) {
58 Optional<QName> context = getRoutingContext(schemaNode);
59 if (context.isPresent()) {
60 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
63 return new GlobalRpcStrategy(rpc.getQName());
66 public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
67 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
68 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
69 return Optional.fromNullable(extension.getQName());
72 return Optional.absent();
75 private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
76 private final QName context;
77 private final QName leaf;
79 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
81 this.context = Preconditions.checkNotNull(ctx);
82 this.leaf = Preconditions.checkNotNull(leaf);
86 public QName getContext() {
91 public QName getLeaf() {
96 public boolean isContextBasedRouted() {
101 private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
103 GlobalRpcStrategy(final QName identifier) {
108 public boolean isContextBasedRouted() {
113 public QName getContext() {
114 throw new UnsupportedOperationException("Non-routed strategy does not have a context");
118 public QName getLeaf() {
119 throw new UnsupportedOperationException("Non-routed strategy does not have a context");