1daf52d4db9fb18b2d401b7650b95eb363b87723
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / AbstractDOMRpcRoutingTableEntry.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 org.opendaylight.mdsal.dom.api.DOMRpcException;
11 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
12 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
13
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
26
27 abstract class AbstractDOMRpcRoutingTableEntry {
28     private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls;
29     private final SchemaPath schemaPath;
30
31     protected AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
32         this.schemaPath = Preconditions.checkNotNull(schemaPath);
33         this.impls = Preconditions.checkNotNull(impls);
34     }
35
36     protected final SchemaPath getSchemaPath() {
37         return schemaPath;
38     }
39
40     protected final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
41         return impls.get(context);
42     }
43
44     final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
45         return impls;
46     }
47
48     public boolean containsContext(final YangInstanceIdentifier contextReference) {
49         return impls.containsKey(contextReference);
50     }
51
52     final Set<YangInstanceIdentifier> registeredIdentifiers() {
53         return impls.keySet();
54     }
55
56     /**
57      *
58      * @param implementation
59      * @param newRpcs List of new RPCs, must be mutable
60      * @return
61      */
62     final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
63         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
64         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
65             if (newRpcs.remove(ve.getKey())) {
66                 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
67                 i.addAll(ve.getValue());
68                 i.add(implementation);
69                 vb.put(ve.getKey(), i);
70             } else {
71                 vb.put(ve);
72             }
73         }
74         for(final YangInstanceIdentifier ii : newRpcs) {
75             final ArrayList<DOMRpcImplementation> impl = new ArrayList<>(1);
76             impl.add(implementation);
77             vb.put(ii,impl);
78         }
79
80         return newInstance(vb.build());
81     }
82
83     final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
84         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
85         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
86             if (removed.remove(ve.getKey())) {
87                 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
88                 i.remove(implementation);
89                 // We could trimToSize(), but that may perform another copy just to get rid
90                 // of a single element. That is probably not worth the trouble.
91                 if (!i.isEmpty()) {
92                     vb.put(ve.getKey(), i);
93                 }
94             } else {
95                 vb.put(ve);
96             }
97         }
98
99         final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
100         return v.isEmpty() ? null : newInstance(v);
101     }
102
103     protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
104     protected abstract AbstractDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);
105 }