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