Switch to Objects.requireNonNull
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / AbstractDOMRoutingTableEntry.java
1 /*
2  * Copyright (c) 2018 ZTE Corp. 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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.ImmutableMap.Builder;
16 import java.util.ArrayList;
17 import java.util.Comparator;
18 import java.util.EventListener;
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.model.api.SchemaPath;
24
25 /**
26  * Abstract routing table entry definition for Action and RPC.
27  * @param <D> identifier type of RPC or Acton
28  * @param <M> implementation type of RPC or Acton
29  * @param <L> listener type of RPC or Acton
30  */
31 @Beta
32 abstract class AbstractDOMRoutingTableEntry<D, M, L extends EventListener> {
33     private final Map<D, List<M>> implementations;
34     private final SchemaPath type;
35
36     AbstractDOMRoutingTableEntry(final SchemaPath type, final Map<D, List<M>> implementations) {
37         this.type = requireNonNull(type);
38         this.implementations = requireNonNull(implementations);
39     }
40
41     SchemaPath getType() {
42         return type;
43     }
44
45     List<M> getImplementations(final D identifier) {
46         return implementations.get(identifier);
47     }
48
49     Map<D, List<M>> getImplementations() {
50         return implementations;
51     }
52
53     @VisibleForTesting
54     Set<D> registeredIdentifiers() {
55         return implementations.keySet();
56     }
57
58     protected abstract Set<D> registeredIdentifiers(L listener);
59
60     /**
61      * This method adds the given DOMOperationImplementation instance for the given list operation identifiers.
62      *
63      * @param implementation the DOMOperationImplementation instance to add
64      * @param newOprs  the List of new RPCs/Actions that the DOMOperationImplementation provides, must be mutable
65      * @return a new instance of DOMActionRoutingTableEntry with the additions
66      */
67     AbstractDOMRoutingTableEntry<D, M, L> add(final M implementation, final List<D> newOprs) {
68         final Builder<D, List<M>> vb = ImmutableMap.builder();
69         for (final Entry<D, List<M>> ve : implementations.entrySet()) {
70             if (newOprs.remove(ve.getKey())) {
71                 final List<M> i = new ArrayList<>(ve.getValue().size() + 1);
72                 i.addAll(ve.getValue());
73                 i.add(implementation);
74
75                 // New implementation is at the end, this will move it to be the last among implementations
76                 // with equal cost -- relying on sort() being stable.
77                 i.sort(implComparator());
78                 vb.put(ve.getKey(), i);
79             } else {
80                 vb.put(ve);
81             }
82         }
83         for (final D ii : newOprs) {
84             final List<M> impl = new ArrayList<>(1);
85             impl.add(implementation);
86             vb.put(ii, impl);
87         }
88
89         return newInstance(vb.build());
90     }
91
92     AbstractDOMRoutingTableEntry<D, M, L> remove(final M implementation, final List<D> removed) {
93         final Builder<D, List<M>> vb = ImmutableMap.builder();
94         for (final Entry<D, List<M>> ve : implementations.entrySet()) {
95             if (removed.remove(ve.getKey())) {
96                 final List<M> i = new ArrayList<>(ve.getValue());
97                 i.remove(implementation);
98                 // We could trimToSize(), but that may perform another copy just to get rid
99                 // of a single element. That is probably not worth the trouble.
100                 if (!i.isEmpty()) {
101                     vb.put(ve.getKey(), i);
102                 }
103             } else {
104                 vb.put(ve);
105             }
106         }
107
108         final Map<D, List<M>> v = vb.build();
109         return v.isEmpty() ? null : newInstance(vb.build());
110     }
111
112     protected abstract Comparator<M> implComparator();
113
114     protected abstract AbstractDOMRoutingTableEntry<D, M, L> newInstance(Map<D, List<M>> impls);
115 }