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