Merge "Fixed for bug 1168 : Issue while update subnet"
[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 org.opendaylight.yangtools.concepts.Identifiable;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
15 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
16
17 import com.google.common.base.Optional;
18
19 public abstract class RpcRoutingStrategy implements Identifiable<QName> {
20
21     private final QName identifier;
22     private static final QName CONTEXT_REFERENCE = QName.create("urn:opendaylight:yang:extension:yang-ext",
23             "2013-07-09", "context-reference");
24
25     private RpcRoutingStrategy(final QName identifier) {
26         super();
27         this.identifier = identifier;
28     }
29
30     /**
31      * Returns leaf QName in which RPC Route is stored
32      *
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 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 createRoutedStrategy(rpc, context.get(), schemaNode.getQName());
68                 }
69             }
70         }
71         return createGlobalStrategy(rpc);
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 RpcRoutingStrategy createRoutedStrategy(final RpcDefinition rpc, final QName context, final QName leafNode) {
84         return new RoutedRpcStrategy(rpc.getQName(), context, leafNode);
85     }
86
87
88
89     private static RpcRoutingStrategy createGlobalStrategy(final RpcDefinition rpc) {
90         GlobalRpcStrategy ret = new GlobalRpcStrategy(rpc.getQName());
91         return ret;
92     }
93
94     private static class RoutedRpcStrategy extends RpcRoutingStrategy {
95
96         final QName context;
97         private final QName leaf;
98
99         private RoutedRpcStrategy(final QName identifier, final QName ctx, final QName leaf) {
100             super(identifier);
101             this.context = ctx;
102             this.leaf = leaf;
103         }
104
105         @Override
106         public QName getContext() {
107             return context;
108         }
109
110         @Override
111         public QName getLeaf() {
112             return leaf;
113         }
114
115         @Override
116         public boolean isContextBasedRouted() {
117             return true;
118         }
119     }
120
121     private static class GlobalRpcStrategy extends RpcRoutingStrategy {
122
123         public GlobalRpcStrategy(final QName identifier) {
124             super(identifier);
125         }
126
127         @Override
128         public boolean isContextBasedRouted() {
129             return false;
130         }
131
132         @Override
133         public QName getContext() {
134             throw new UnsupportedOperationException("Not routed strategy does not have context.");
135         }
136
137         @Override
138         public QName getLeaf() {
139             throw new UnsupportedOperationException("Not routed strategy does not have context.");
140         }
141     }
142 }