Fix SnapshotBackedWriteTransaction error message
[mdsal.git] / dom / mdsal-dom-spi / src / main / java / org / opendaylight / mdsal / dom / spi / RpcRoutingStrategy.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Optional;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.yang.common.QName;
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;
18
19 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
20     // FIXME: deprecate context-reference
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;
24
25     private RpcRoutingStrategy(final QName identifier) {
26         this.identifier = requireNonNull(identifier);
27     }
28
29     /**
30      * Returns leaf QName in which RPC Route is stored.
31      * @return leaf QName in which RPC Route is stored
32      * @throws UnsupportedOperationException If RPC is not content routed.
33      *      ({@link #isContextBasedRouted()} returned <code>false</code>)
34      */
35     public abstract QName getLeaf();
36
37     /**
38      * Returns identity QName which represents RPC Routing context.
39      * @return identity QName which represents RPC Routing context
40      * @throws UnsupportedOperationException If RPC is not content routed.
41      *      ({@link #isContextBasedRouted()} returned <code>false</code>)
42      */
43     public abstract QName getContext();
44
45     @Override
46     public final QName getIdentifier() {
47         return identifier;
48     }
49
50     /**
51      * Returns true if RPC is routed by context.
52      *
53      * @return true if RPC is routed by content.
54      */
55     public abstract boolean isContextBasedRouted();
56
57     public static RpcRoutingStrategy from(final RpcDefinition rpc) {
58         for (DataSchemaNode schemaNode : rpc.getInput().getChildNodes()) {
59             Optional<QName> context = getRoutingContext(schemaNode);
60             if (context.isPresent()) {
61                 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
62             }
63         }
64         return new GlobalRpcStrategy(rpc.getQName());
65     }
66
67     public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
68         for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
69             if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
70                 return Optional.ofNullable(extension.getQName());
71             }
72         }
73         return Optional.empty();
74     }
75
76     private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
77         private final QName context;
78         private final QName leaf;
79
80         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
81             super(identifier);
82             this.context = requireNonNull(ctx);
83             this.leaf = requireNonNull(leaf);
84         }
85
86         @Override
87         public QName getContext() {
88             return context;
89         }
90
91         @Override
92         public QName getLeaf() {
93             return leaf;
94         }
95
96         @Override
97         public boolean isContextBasedRouted() {
98             return true;
99         }
100     }
101
102     private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
103
104         GlobalRpcStrategy(final QName identifier) {
105             super(identifier);
106         }
107
108         @Override
109         public boolean isContextBasedRouted() {
110             return false;
111         }
112
113         @Override
114         public QName getContext() {
115             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
116         }
117
118         @Override
119         public QName getLeaf() {
120             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
121         }
122     }
123 }