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.md.sal.dom.broker.spi.rpc;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Optional;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
21 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
22 private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
23 "2013-07-09", "context-reference").intern();
25 private final QName identifier;
27 private RpcRoutingStrategy(final QName identifier) {
28 this.identifier = requireNonNull(identifier);
32 * Returns leaf QName in which RPC Route is stored.
34 * @return leaf QName in which RPC Route is stored
35 * @throws UnsupportedOperationException If RPC is not content routed.
36 * ({@link #isContextBasedRouted()} returned <code>false</code>)
38 public abstract QName getLeaf();
41 * Returns identity QName which represents RPC Routing context.
43 * @return identity QName which represents RPC Routing context
44 * @throws UnsupportedOperationException If RPC is not content routed.
45 * ({@link #isContextBasedRouted()} returned <code>false</code>)
47 public abstract QName getContext();
50 public final QName getIdentifier() {
55 * Returns true if RPC is routed by context.
57 * @return true if RPC is routed by content.
59 public abstract boolean isContextBasedRouted();
61 public static RpcRoutingStrategy from(final RpcDefinition rpc) {
62 ContainerSchemaNode input = rpc.getInput();
64 for (DataSchemaNode schemaNode : input.getChildNodes()) {
65 Optional<QName> context = getRoutingContext(schemaNode);
66 if (context.isPresent()) {
67 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
71 return new GlobalRpcStrategy(rpc.getQName());
74 public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
75 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
76 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
77 return Optional.fromNullable(extension.getQName());
80 return Optional.absent();
83 private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
84 private final QName context;
85 private final QName leaf;
87 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
89 this.context = requireNonNull(ctx);
90 this.leaf = requireNonNull(leaf);
94 public QName getContext() {
99 public QName getLeaf() {
104 public boolean isContextBasedRouted() {
109 private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
111 GlobalRpcStrategy(final QName identifier) {
116 public boolean isContextBasedRouted() {
121 public QName getContext() {
122 throw new UnsupportedOperationException("Non-routed strategy does not have a context");
126 public QName getLeaf() {
127 throw new UnsupportedOperationException("Non-routed strategy does not have a context");