Do not use ListenerRegistration in DOMSchemaService
[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.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.concepts.Identifiable;
15 import org.opendaylight.yangtools.odlext.model.api.ContextReferenceEffectiveStatement;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
20
21 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
22     private final @NonNull QName identifier;
23
24     private RpcRoutingStrategy(final QName identifier) {
25         this.identifier = requireNonNull(identifier);
26     }
27
28     /**
29      * Returns leaf QName in which RPC Route is stored.
30      * @return leaf QName in which RPC Route is stored
31      * @throws UnsupportedOperationException If RPC is not content routed.
32      *      ({@link #isContextBasedRouted()} returned <code>false</code>)
33      */
34     public abstract QName getLeaf();
35
36     /**
37      * Returns identity QName which represents RPC Routing context.
38      * @return identity QName which represents RPC Routing context
39      * @throws UnsupportedOperationException If RPC is not content routed.
40      *      ({@link #isContextBasedRouted()} returned <code>false</code>)
41      */
42     public abstract QName getContext();
43
44     @Override
45     public final QName getIdentifier() {
46         return identifier;
47     }
48
49     /**
50      * Returns true if RPC is routed by context.
51      *
52      * @return true if RPC is routed by content.
53      */
54     public abstract boolean isContextBasedRouted();
55
56     public static RpcRoutingStrategy from(final RpcDefinition rpc) {
57         // FIXME: deprecate context-reference
58         for (EffectiveStatement<?, ?> stmt : rpc.getInput().asEffectiveStatement().effectiveSubstatements()) {
59             if (stmt instanceof SchemaTreeEffectiveStatement) {
60                 final Optional<QName> context =
61                     stmt.findFirstEffectiveSubstatementArgument(ContextReferenceEffectiveStatement.class);
62                 if (context.isPresent()) {
63                     return new RoutedRpcStrategy(rpc.getQName(), context.orElseThrow(),
64                         ((SchemaTreeEffectiveStatement<?>) stmt).argument());
65                 }
66             }
67         }
68         return new GlobalRpcStrategy(rpc.getQName());
69     }
70
71     private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
72         private final QName context;
73         private final QName leaf;
74
75         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
76             super(identifier);
77             this.context = requireNonNull(ctx);
78             this.leaf = requireNonNull(leaf);
79         }
80
81         @Override
82         public QName getContext() {
83             return context;
84         }
85
86         @Override
87         public QName getLeaf() {
88             return leaf;
89         }
90
91         @Override
92         public boolean isContextBasedRouted() {
93             return true;
94         }
95     }
96
97     private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
98
99         GlobalRpcStrategy(final QName identifier) {
100             super(identifier);
101         }
102
103         @Override
104         public boolean isContextBasedRouted() {
105             return false;
106         }
107
108         @Override
109         public QName getContext() {
110             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
111         }
112
113         @Override
114         public QName getLeaf() {
115             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
116         }
117     }
118 }