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