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 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.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 = Preconditions.checkNotNull(identifier);
30 * Returns leaf QName in which RPC Route is stored.
32 * @return leaf QName in which RPC Route is stored
33 * @throws UnsupportedOperationException If RPC is not content routed.
34 * ({@link #isContextBasedRouted()} returned <code>false</code>)
36 public abstract QName getLeaf();
39 * Returns identity QName which represents RPC Routing context.
41 * @return identity QName which represents RPC Routing context
42 * @throws UnsupportedOperationException If RPC is not content routed.
43 * ({@link #isContextBasedRouted()} returned <code>false</code>)
45 public abstract QName getContext();
48 public final QName getIdentifier() {
53 * Returns true if RPC is routed by context.
55 * @return true if RPC is routed by content.
57 public abstract boolean isContextBasedRouted();
59 public static RpcRoutingStrategy from(final RpcDefinition rpc) {
60 ContainerSchemaNode input = rpc.getInput();
62 for (DataSchemaNode schemaNode : input.getChildNodes()) {
63 Optional<QName> context = getRoutingContext(schemaNode);
64 if (context.isPresent()) {
65 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
69 return new GlobalRpcStrategy(rpc.getQName());
72 public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
73 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
74 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
75 return Optional.fromNullable(extension.getQName());
78 return Optional.absent();
81 private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
82 private final QName context;
83 private final QName leaf;
85 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
87 this.context = Preconditions.checkNotNull(ctx);
88 this.leaf = Preconditions.checkNotNull(leaf);
92 public QName getContext() {
97 public QName getLeaf() {
102 public boolean isContextBasedRouted() {
107 private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
109 GlobalRpcStrategy(final QName identifier) {
114 public boolean isContextBasedRouted() {
119 public QName getContext() {
120 throw new UnsupportedOperationException("Non-routed strategy does not have a context");
124 public QName getLeaf() {
125 throw new UnsupportedOperationException("Non-routed strategy does not have a context");