96bf5a8b64da2388b87b86a04faf94687f037a60
[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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.Optional;
13 import org.opendaylight.yangtools.concepts.Identifiable;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
18 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
19
20 @Deprecated
21 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
22     private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
23             "2013-07-09", "context-reference").intern();
24
25     private final QName identifier;
26
27     private RpcRoutingStrategy(final QName identifier) {
28         this.identifier = requireNonNull(identifier);
29     }
30
31     /**
32      * Returns leaf QName in which RPC Route is stored.
33      *
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>)
37      */
38     public abstract QName getLeaf();
39
40     /**
41      * Returns identity QName which represents RPC Routing context.
42      *
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>)
46      */
47     public abstract QName getContext();
48
49     @Override
50     public final QName getIdentifier() {
51         return identifier;
52     }
53
54     /**
55      * Returns true if RPC is routed by context.
56      *
57      * @return true if RPC is routed by content.
58      */
59     public abstract boolean isContextBasedRouted();
60
61     public static RpcRoutingStrategy from(final RpcDefinition rpc) {
62         ContainerSchemaNode input = rpc.getInput();
63         if (input != null) {
64             for (DataSchemaNode schemaNode : input.getChildNodes()) {
65                 Optional<QName> context = getRoutingContext(schemaNode);
66                 if (context.isPresent()) {
67                     return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
68                 }
69             }
70         }
71         return new GlobalRpcStrategy(rpc.getQName());
72     }
73
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());
78             }
79         }
80         return Optional.absent();
81     }
82
83     private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
84         private final QName context;
85         private final QName leaf;
86
87         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
88             super(identifier);
89             this.context = requireNonNull(ctx);
90             this.leaf = requireNonNull(leaf);
91         }
92
93         @Override
94         public QName getContext() {
95             return context;
96         }
97
98         @Override
99         public QName getLeaf() {
100             return leaf;
101         }
102
103         @Override
104         public boolean isContextBasedRouted() {
105             return true;
106         }
107     }
108
109     private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
110
111         GlobalRpcStrategy(final QName identifier) {
112             super(identifier);
113         }
114
115         @Override
116         public boolean isContextBasedRouted() {
117             return false;
118         }
119
120         @Override
121         public QName getContext() {
122             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
123         }
124
125         @Override
126         public QName getLeaf() {
127             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
128         }
129     }
130 }