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.gaul.modernizer_maven_annotations.SuppressModernizer;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
19 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
22 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
23 private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
24 "2013-07-09", "context-reference").intern();
26 private final QName identifier;
28 private RpcRoutingStrategy(final QName identifier) {
29 this.identifier = requireNonNull(identifier);
33 * Returns leaf QName in which RPC Route is stored.
35 * @return leaf QName in which RPC Route is stored
36 * @throws UnsupportedOperationException If RPC is not content routed.
37 * ({@link #isContextBasedRouted()} returned <code>false</code>)
39 public abstract QName getLeaf();
42 * Returns identity QName which represents RPC Routing context.
44 * @return identity QName which represents RPC Routing context
45 * @throws UnsupportedOperationException If RPC is not content routed.
46 * ({@link #isContextBasedRouted()} returned <code>false</code>)
48 public abstract QName getContext();
51 public final QName getIdentifier() {
56 * Returns true if RPC is routed by context.
58 * @return true if RPC is routed by content.
60 public abstract boolean isContextBasedRouted();
62 public static RpcRoutingStrategy from(final RpcDefinition rpc) {
63 ContainerSchemaNode input = rpc.getInput();
65 for (DataSchemaNode schemaNode : input.getChildNodes()) {
66 Optional<QName> context = getRoutingContext(schemaNode);
67 if (context.isPresent()) {
68 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
72 return new GlobalRpcStrategy(rpc.getQName());
76 public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
77 for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
78 if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
79 return Optional.fromNullable(extension.getQName());
82 return Optional.absent();
85 private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
86 private final QName context;
87 private final QName leaf;
89 private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
91 this.context = requireNonNull(ctx);
92 this.leaf = requireNonNull(leaf);
96 public QName getContext() {
101 public QName getLeaf() {
106 public boolean isContextBasedRouted() {
111 private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
112 GlobalRpcStrategy(final QName identifier) {
117 public boolean isContextBasedRouted() {
122 public QName getContext() {
123 throw new UnsupportedOperationException("Non-routed strategy does not have a context");
127 public QName getLeaf() {
128 throw new UnsupportedOperationException("Non-routed strategy does not have a context");