Merge "Cleanup RpcRoutingStrategy definition"
[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.cachedReference(QName.create("urn:opendaylight:yang:extension:yang-ext",
22             "2013-07-09", "context-reference"));
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      *
33      * @return leaf QName in which RPC Route is stored
34      * @throws UnsupportedOperationException If RPC is not content routed.
35      *  ({@link #isContextBasedRouted()} returned <code>false</code>)
36      */
37     public abstract QName getLeaf();
38
39     /**
40      * Returns identity QName which represents RPC Routing context
41      *
42      * @return identity QName which represents RPC Routing context
43      * @throws UnsupportedOperationException If RPC is not content routed.
44      *  ({@link #isContextBasedRouted()} returned <code>false</code>)
45      */
46     public abstract QName getContext();
47
48     @Override
49     public final QName getIdentifier() {
50         return identifier;
51     }
52
53     /**
54      * Returns true if RPC is routed by context.
55      *
56      * @return true if RPC is routed by content.
57      */
58     public abstract boolean isContextBasedRouted();
59
60     public static RpcRoutingStrategy from(final RpcDefinition rpc) {
61         ContainerSchemaNode input = rpc.getInput();
62         if (input != null) {
63             for (DataSchemaNode schemaNode : input.getChildNodes()) {
64                 Optional<QName> context = getRoutingContext(schemaNode);
65                 if (context.isPresent()) {
66                     return new RoutedRpcStrategy(rpc.getQName(), context.get(), schemaNode.getQName());
67                 }
68             }
69         }
70         return new GlobalRpcStrategy(rpc.getQName());
71     }
72
73     public static Optional<QName> getRoutingContext(final DataSchemaNode schemaNode) {
74         for (UnknownSchemaNode extension : schemaNode.getUnknownSchemaNodes()) {
75             if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
76                 return Optional.fromNullable(extension.getQName());
77             }
78         }
79         return Optional.absent();
80     }
81
82     private static final class RoutedRpcStrategy extends RpcRoutingStrategy {
83         private final QName context;
84         private final QName leaf;
85
86         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
87             super(identifier);
88             this.context = Preconditions.checkNotNull(ctx);
89             this.leaf = Preconditions.checkNotNull(leaf);
90         }
91
92         @Override
93         public QName getContext() {
94             return context;
95         }
96
97         @Override
98         public QName getLeaf() {
99             return leaf;
100         }
101
102         @Override
103         public boolean isContextBasedRouted() {
104             return true;
105         }
106     }
107
108     private static final class GlobalRpcStrategy extends RpcRoutingStrategy {
109
110         public GlobalRpcStrategy(final QName identifier) {
111             super(identifier);
112         }
113
114         @Override
115         public boolean isContextBasedRouted() {
116             return false;
117         }
118
119         @Override
120         public QName getContext() {
121             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
122         }
123
124         @Override
125         public QName getLeaf() {
126             throw new UnsupportedOperationException("Non-routed strategy does not have a context");
127         }
128     }
129 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.