Use YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMRpcRoutingTable.java
1 /*
2  * Copyright (c) 2015 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.impl;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import com.google.common.collect.LinkedListMultimap;
15 import com.google.common.collect.ListMultimap;
16 import com.google.common.collect.Maps;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Map.Entry;
25 import java.util.Set;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
41
42 final class DOMRpcRoutingTable {
43     private static final QName CONTEXT_REFERENCE = QName.cachedReference(QName.create("urn:opendaylight:yang:extension:yang-ext", "2013-07-09", "context-reference"));
44
45     static final DOMRpcRoutingTable EMPTY = new DOMRpcRoutingTable();
46     private static final Function<AbstractDOMRpcRoutingTableEntry, Set<YangInstanceIdentifier>> EXTRACT_IDENTIFIERS =
47             new Function<AbstractDOMRpcRoutingTableEntry, Set<YangInstanceIdentifier>>() {
48                 @Override
49                 public Set<YangInstanceIdentifier> apply(final AbstractDOMRpcRoutingTableEntry input) {
50                     return input.registeredIdentifiers();
51                 }
52     };
53     private final Map<SchemaPath, AbstractDOMRpcRoutingTableEntry> rpcs;
54     private final SchemaContext schemaContext;
55
56     private DOMRpcRoutingTable() {
57         rpcs = Collections.emptyMap();
58         schemaContext = null;
59     }
60
61     private DOMRpcRoutingTable(final Map<SchemaPath, AbstractDOMRpcRoutingTableEntry> rpcs, final SchemaContext schemaContext) {
62         this.rpcs = Preconditions.checkNotNull(rpcs);
63         this.schemaContext = schemaContext;
64     }
65
66     private static ListMultimap<SchemaPath, YangInstanceIdentifier> decomposeIdentifiers(final Set<DOMRpcIdentifier> rpcs) {
67         final ListMultimap<SchemaPath, YangInstanceIdentifier> ret = LinkedListMultimap.create();
68         for (DOMRpcIdentifier i : rpcs) {
69             ret.put(i.getType(), i.getContextReference());
70         }
71         return ret;
72     }
73
74     DOMRpcRoutingTable add(final DOMRpcImplementation implementation, final Set<DOMRpcIdentifier> rpcs) {
75         if (rpcs.isEmpty()) {
76             return this;
77         }
78
79         // First decompose the identifiers to a multimap
80         final ListMultimap<SchemaPath, YangInstanceIdentifier> toAdd = decomposeIdentifiers(rpcs);
81
82         // Now iterate over existing entries, modifying them as appropriate...
83         final Builder<SchemaPath, AbstractDOMRpcRoutingTableEntry> mb = ImmutableMap.builder();
84         for (Entry<SchemaPath, AbstractDOMRpcRoutingTableEntry> re : this.rpcs.entrySet()) {
85             List<YangInstanceIdentifier> newRpcs = new ArrayList<>(toAdd.removeAll(re.getKey()));
86             if (!newRpcs.isEmpty()) {
87                 final AbstractDOMRpcRoutingTableEntry ne = re.getValue().add(implementation, newRpcs);
88                 mb.put(re.getKey(), ne);
89             } else {
90                 mb.put(re);
91             }
92         }
93
94         // Finally add whatever is left in the decomposed multimap
95         for (Entry<SchemaPath, Collection<YangInstanceIdentifier>> e : toAdd.asMap().entrySet()) {
96             final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
97             final List<DOMRpcImplementation> v = Collections.singletonList(implementation);
98             for (YangInstanceIdentifier i : e.getValue()) {
99                 vb.put(i, v);
100             }
101
102             mb.put(e.getKey(), createRpcEntry(schemaContext, e.getKey(), vb.build()));
103         }
104
105         return new DOMRpcRoutingTable(mb.build(), schemaContext);
106     }
107
108     DOMRpcRoutingTable remove(final DOMRpcImplementation implementation, final Set<DOMRpcIdentifier> rpcs) {
109         if (rpcs.isEmpty()) {
110             return this;
111         }
112
113         // First decompose the identifiers to a multimap
114         final ListMultimap<SchemaPath, YangInstanceIdentifier> toRemove = decomposeIdentifiers(rpcs);
115
116         // Now iterate over existing entries, modifying them as appropriate...
117         final Builder<SchemaPath, AbstractDOMRpcRoutingTableEntry> b = ImmutableMap.builder();
118         for (Entry<SchemaPath, AbstractDOMRpcRoutingTableEntry> e : this.rpcs.entrySet()) {
119             final List<YangInstanceIdentifier> removed = new ArrayList<>(toRemove.removeAll(e.getKey()));
120             if (!removed.isEmpty()) {
121                 final AbstractDOMRpcRoutingTableEntry ne = e.getValue().remove(implementation, removed);
122                 if (ne != null) {
123                     b.put(e.getKey(), ne);
124                 }
125             } else {
126                 b.put(e);
127             }
128         }
129
130         // All done, whatever is in toRemove, was not there in the first place
131         return new DOMRpcRoutingTable(b.build(), schemaContext);
132     }
133
134     boolean contains(final DOMRpcIdentifier input) {
135         final AbstractDOMRpcRoutingTableEntry contexts = rpcs.get(input.getType());
136         return contexts != null && contexts.containsContext(input.getContextReference());
137     }
138
139     Map<SchemaPath, Set<YangInstanceIdentifier>> getRpcs() {
140         return Maps.transformValues(rpcs, EXTRACT_IDENTIFIERS);
141     }
142
143     private static RpcDefinition findRpcDefinition(final SchemaContext context, final SchemaPath schemaPath) {
144         if (context != null) {
145             final QName qname = schemaPath.getPathFromRoot().iterator().next();
146             final Module module = context.findModuleByNamespaceAndRevision(qname.getNamespace(), qname.getRevision());
147             if (module != null && module.getRpcs() != null) {
148                 for (RpcDefinition rpc : module.getRpcs()) {
149                     if (qname.equals(rpc.getQName())) {
150                         return rpc;
151                     }
152                 }
153             }
154         }
155
156         return null;
157     }
158
159     private static AbstractDOMRpcRoutingTableEntry createRpcEntry(final SchemaContext context, final SchemaPath key, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations) {
160         final RpcDefinition rpcDef = findRpcDefinition(context, key);
161         if (rpcDef != null) {
162             final ContainerSchemaNode input = rpcDef.getInput();
163             if (input != null) {
164                 for (DataSchemaNode c : input.getChildNodes()) {
165                     for (UnknownSchemaNode extension : c.getUnknownSchemaNodes()) {
166                         if (CONTEXT_REFERENCE.equals(extension.getNodeType())) {
167                             final YangInstanceIdentifier keyId = YangInstanceIdentifier.of(c.getQName());
168                             return new RoutedDOMRpcRoutingTableEntry(rpcDef, keyId, implementations);
169                         }
170                     }
171                 }
172             }
173
174             return new GlobalDOMRpcRoutingTableEntry(rpcDef, implementations);
175         } else {
176             return new UnknownDOMRpcRoutingTableEntry(key, implementations);
177         }
178     }
179
180     CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
181         final AbstractDOMRpcRoutingTableEntry entry = rpcs.get(type);
182         if (entry == null) {
183             return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
184         }
185
186         return entry.invokeRpc(input);
187     }
188
189     DOMRpcRoutingTable setSchemaContext(final SchemaContext context) {
190         final Builder<SchemaPath, AbstractDOMRpcRoutingTableEntry> b = ImmutableMap.builder();
191
192         for (Entry<SchemaPath, AbstractDOMRpcRoutingTableEntry> e : rpcs.entrySet()) {
193             b.put(e.getKey(), createRpcEntry(context, e.getKey(), e.getValue().getImplementations()));
194         }
195
196         return new DOMRpcRoutingTable(b.build(), context);
197     }
198
199 }

©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.