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 org.opendaylight.yangtools.concepts.Identifiable;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
15 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
17 import com.google.common.base.Optional;
19 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
21 private final QName identifier;
22 private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
23 "2013-07-09", "context-reference");
25 private RpcRoutingStrategy(final QName identifier) {
27 this.identifier = identifier;
31 * 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 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 createRoutedStrategy(rpc, context.get(), schemaNode.getQName());
71 return createGlobalStrategy(rpc);
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 RpcRoutingStrategy createRoutedStrategy(final RpcDefinition rpc, final QName context, final QName leafNode) {
84 return new RoutedRpcStrategy(rpc.getQName(), context, leafNode);
89 private static RpcRoutingStrategy createGlobalStrategy(final RpcDefinition rpc) {
90 GlobalRpcStrategy ret = new GlobalRpcStrategy(rpc.getQName());
94 private static class RoutedRpcStrategy extends RpcRoutingStrategy {
97 private final QName leaf;
99 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
106 public QName getContext() {
111 public QName getLeaf() {
116 public boolean isContextBasedRouted() {
121 private static class GlobalRpcStrategy extends RpcRoutingStrategy {
123 public GlobalRpcStrategy(final QName identifier) {
128 public boolean isContextBasedRouted() {
133 public QName getContext() {
134 throw new UnsupportedOperationException("Not routed strategy does not have context.");
138 public QName getLeaf() {
139 throw new UnsupportedOperationException("Not routed strategy does not have context.");