Switch default stream output to Magnesium
[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 static java.util.Objects.requireNonNull;
11
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.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashMap;
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.DOMRpcAvailabilityListener;
27 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
28 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
29 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
30 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
31 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
32 import org.opendaylight.controller.md.sal.dom.broker.spi.rpc.RpcRoutingStrategy;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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
41 @Deprecated(forRemoval = true)
42 final class DOMRpcRoutingTable {
43
44     static final DOMRpcRoutingTable EMPTY = new DOMRpcRoutingTable(ImmutableMap.of(), null);
45
46     private final Map<SchemaPath, AbstractDOMRpcRoutingTableEntry> rpcs;
47     private final SchemaContext schemaContext;
48
49     private DOMRpcRoutingTable(final Map<SchemaPath, AbstractDOMRpcRoutingTableEntry> rpcs,
50                                final SchemaContext schemaContext) {
51         this.rpcs = requireNonNull(rpcs);
52         this.schemaContext = schemaContext;
53     }
54
55     static ListMultimap<SchemaPath, YangInstanceIdentifier> decomposeIdentifiers(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 = Collections.singletonList(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     Map<SchemaPath, Set<YangInstanceIdentifier>> getRpcs(final DOMRpcAvailabilityListener listener) {
129         final Map<SchemaPath, Set<YangInstanceIdentifier>> ret = new HashMap<>(rpcs.size());
130         for (Entry<SchemaPath, AbstractDOMRpcRoutingTableEntry> e : rpcs.entrySet()) {
131             final Set<YangInstanceIdentifier> ids = e.getValue().registeredIdentifiers(listener);
132             if (!ids.isEmpty()) {
133                 ret.put(e.getKey(), ids);
134             }
135         }
136
137         return ret;
138     }
139
140     private static RpcDefinition findRpcDefinition(final SchemaContext context, final SchemaPath schemaPath) {
141         if (context != null) {
142             final QName qname = schemaPath.getPathFromRoot().iterator().next();
143             final Module module = context.findModule(qname.getModule()).orElse(null);
144             if (module != null && module.getRpcs() != null) {
145                 for (RpcDefinition rpc : module.getRpcs()) {
146                     if (qname.equals(rpc.getQName())) {
147                         return rpc;
148                     }
149                 }
150             }
151         }
152
153         return null;
154     }
155
156     private static AbstractDOMRpcRoutingTableEntry createRpcEntry(final SchemaContext context, final SchemaPath key,
157             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> implementations) {
158         final RpcDefinition rpcDef = findRpcDefinition(context, key);
159         if (rpcDef == null) {
160             return new UnknownDOMRpcRoutingTableEntry(key, implementations);
161         }
162
163         final RpcRoutingStrategy strategy = RpcRoutingStrategy.from(rpcDef);
164         if (strategy.isContextBasedRouted()) {
165             return new RoutedDOMRpcRoutingTableEntry(rpcDef, YangInstanceIdentifier.of(strategy.getLeaf()),
166                 implementations);
167
168         }
169
170         return new GlobalDOMRpcRoutingTableEntry(rpcDef, implementations);
171     }
172
173     CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final SchemaPath type, final NormalizedNode<?, ?> input) {
174         final AbstractDOMRpcRoutingTableEntry entry = rpcs.get(type);
175         if (entry == null) {
176             return Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(
177                 new DOMRpcImplementationNotAvailableException("No implementation of RPC %s available", type));
178         }
179
180         return entry.invokeRpc(input);
181     }
182
183     DOMRpcRoutingTable setSchemaContext(final SchemaContext context) {
184         final Builder<SchemaPath, AbstractDOMRpcRoutingTableEntry> b = ImmutableMap.builder();
185
186         for (Entry<SchemaPath, AbstractDOMRpcRoutingTableEntry> e : rpcs.entrySet()) {
187             b.put(e.getKey(), createRpcEntry(context, e.getKey(), e.getValue().getImplementations()));
188         }
189
190         return new DOMRpcRoutingTable(b.build(), context);
191     }
192 }

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