9c3191fb187c672951ecdf62ed29b664277d1419
[controller.git] / opendaylight / md-sal / sal-dom-spi / src / main / java / org / opendaylight / controller / md / sal / dom / broker / spi / rpc / 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.controller.md.sal.dom.broker.spi.rpc;
9
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;
18
19 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
20
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 = Preconditions.checkNotNull(identifier);
27     }
28
29     /**
30      * Returns leaf QName in which RPC Route is stored.
31      *
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>)
35      */
36     public abstract QName getLeaf();
37
38     /**
39      * Returns identity QName which represents RPC Routing context.
40      *
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>)
44      */
45     public abstract QName getContext();
46
47     @Override
48     public final QName getIdentifier() {
49         return identifier;
50     }
51
52     /**
53      * Returns true if RPC is routed by context.
54      *
55      * @return true if RPC is routed by content.
56      */
57     public abstract boolean isContextBasedRouted();
58
59     public static RpcRoutingStrategy from(final RpcDefinition rpc) {
60         ContainerSchemaNode input = rpc.getInput();
61         if (input != null) {
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());
66                 }
67             }
68         }
69         return new GlobalRpcStrategy(rpc.getQName());
70     }
71
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());
76             }
77         }
78         return Optional.absent();
79     }
80
81     private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
82         private final QName context;
83         private final QName leaf;
84
85         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
86             super(identifier);
87             this.context = Preconditions.checkNotNull(ctx);
88             this.leaf = Preconditions.checkNotNull(leaf);
89         }
90
91         @Override
92         public QName getContext() {
93             return context;
94         }
95
96         @Override
97         public QName getLeaf() {
98             return leaf;
99         }
100
101         @Override
102         public boolean isContextBasedRouted() {
103             return true;
104         }
105     }
106
107     private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
108
109         GlobalRpcStrategy(final QName identifier) {
110             super(identifier);
111         }
112
113         @Override
114         public boolean isContextBasedRouted() {
115             return false;
116         }
117
118         @Override
119         public QName getContext() {
120             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
121         }
122
123         @Override
124         public QName getLeaf() {
125             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
126         }
127     }
128 }