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.ContainerSchemaNode;
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> {
21 private static final QName CONTEXT_REFERENCE = QName.cachedReference(QName.create("urn:opendaylight:yang:extension:yang-ext",
22 "2013-07-09", "context-reference"));
23 private final QName identifier;
25 private RpcRoutingStrategy(final QName identifier) {
26 this.identifier = Preconditions.checkNotNull(identifier);
30 * Returns leaf QName in which RPC Route is stored
33 * @return leaf QName in which RPC Route is stored
34 * @throws UnsupportedOperationException If RPC is not content routed.
35 * ({@link #isContextBasedRouted()} returned <code>false</code>)
37 public abstract QName getLeaf();
40 * Returns identity QName which represents RPC Routing context
42 * @return identity QName which represents RPC Routing context
43 * @throws UnsupportedOperationException If RPC is not content routed.
44 * ({@link #isContextBasedRouted()} returned <code>false</code>)
46 public abstract QName getContext();
49 public final QName getIdentifier() {
54 * Returns true if RPC is routed by context.
56 * @return true if RPC is routed by content.
58 public abstract boolean isContextBasedRouted();
60 public static RpcRoutingStrategy from(final RpcDefinition rpc) {
61 ContainerSchemaNode input = rpc.getInput();
63 for (DataSchemaNode schemaNode : input.getChildNodes()) {
64 Optional<QName> context = getRoutingContext(schemaNode);
65 if (context.isPresent()) {
66 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
70 return new GlobalRpcStrategy(rpc.getQName());
73 public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
74 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
75 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
76 return Optional.fromNullable(extension.getQName());
79 return Optional.absent();
82 private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
83 private final QName context;
84 private final QName leaf;
86 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
88 this.context = Preconditions.checkNotNull(ctx);
89 this.leaf = Preconditions.checkNotNull(leaf);
93 public QName getContext() {
98 public QName getLeaf() {
103 public boolean isContextBasedRouted() {
108 private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
110 public GlobalRpcStrategy(final QName identifier) {
115 public boolean isContextBasedRouted() {
120 public QName getContext() {
121 throw new UnsupportedOperationException("Non-routed strategy does not have a context");
125 public QName getLeaf() {
126 throw new UnsupportedOperationException("Non-routed strategy does not have a context");