Mass-migrate to java.util.Optional
[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 com.google.common.base.Preconditions;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.concepts.Identifiable;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
16 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
17
18 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
19
20     private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
21             "2013-07-09", "context-reference").intern();
22     private final QName identifier;
23
24     private RpcRoutingStrategy(final QName identifier) {
25         this.identifier = Preconditions.checkNotNull(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         for (DataSchemaNode schemaNode : rpc.getInput().getChildNodes()) {
58             Optional<QName> context = getRoutingContext(schemaNode);
59             if (context.isPresent()) {
60                 return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
61             }
62         }
63         return new GlobalRpcStrategy(rpc.getQName());
64     }
65
66     public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
67         for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
68             if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
69                 return Optional.ofNullable(extension.getQName());
70             }
71         }
72         return Optional.empty();
73     }
74
75     private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
76         private final QName context;
77         private final QName leaf;
78
79         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
80             super(identifier);
81             this.context = Preconditions.checkNotNull(ctx);
82             this.leaf = Preconditions.checkNotNull(leaf);
83         }
84
85         @Override
86         public QName getContext() {
87             return context;
88         }
89
90         @Override
91         public QName getLeaf() {
92             return leaf;
93         }
94
95         @Override
96         public boolean isContextBasedRouted() {
97             return true;
98         }
99     }
100
101     private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
102
103         GlobalRpcStrategy(final QName identifier) {
104             super(identifier);
105         }
106
107         @Override
108         public boolean isContextBasedRouted() {
109             return false;
110         }
111
112         @Override
113         public QName getContext() {
114             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
115         }
116
117         @Override
118         public QName getLeaf() {
119             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
120         }
121     }
122 }