Deprecate old MD-SAL APIs for removal
[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.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;
20
21 @Deprecated(forRemoval = true)
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();
25
26     private final QName identifier;
27
28     private RpcRoutingStrategy(final QName identifier) {
29         this.identifier = requireNonNull(identifier);
30     }
31
32     /**
33      * Returns leaf QName in which RPC Route is stored.
34      *
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>)
38      */
39     public abstract QName getLeaf();
40
41     /**
42      * Returns identity QName which represents RPC Routing context.
43      *
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>)
47      */
48     public abstract QName getContext();
49
50     @Override
51     public final QName getIdentifier() {
52         return identifier;
53     }
54
55     /**
56      * Returns true if RPC is routed by context.
57      *
58      * @return true if RPC is routed by content.
59      */
60     public abstract boolean isContextBasedRouted();
61
62     public static RpcRoutingStrategy from(final RpcDefinition rpc) {
63         ContainerSchemaNode input = rpc.getInput();
64         if (input != null) {
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());
69                 }
70             }
71         }
72         return new GlobalRpcStrategy(rpc.getQName());
73     }
74
75     @SuppressModernizer
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());
80             }
81         }
82         return Optional.absent();
83     }
84
85     private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
86         private final QName context;
87         private final QName leaf;
88
89         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
90             super(identifier);
91             this.context = requireNonNull(ctx);
92             this.leaf = requireNonNull(leaf);
93         }
94
95         @Override
96         public QName getContext() {
97             return context;
98         }
99
100         @Override
101         public QName getLeaf() {
102             return leaf;
103         }
104
105         @Override
106         public boolean isContextBasedRouted() {
107             return true;
108         }
109     }
110
111     private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
112         GlobalRpcStrategy(final QName identifier) {
113             super(identifier);
114         }
115
116         @Override
117         public boolean isContextBasedRouted() {
118             return false;
119         }
120
121         @Override
122         public QName getContext() {
123             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
124         }
125
126         @Override
127         public QName getLeaf() {
128             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
129         }
130     }
131 }