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