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