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