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

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