2 * Copyright (c) 2018 ZTE Corp. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
10 import static java.util.Objects.requireNonNull;
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.List;
20 import java.util.Map.Entry;
24 * Abstract routing table entry definition for Action and RPC.
25 * @param <D> identifier type of RPC or Acton
26 * @param <M> implementation type of RPC or Acton
27 * @param <L> listener type of RPC or Acton
28 * @param <K> routing key type
31 abstract class AbstractDOMRoutingTableEntry<D, M, L, K> {
32 private final Map<D, List<M>> implementations;
35 AbstractDOMRoutingTableEntry(final K type, final Map<D, List<M>> implementations) {
36 this.type = requireNonNull(type);
37 this.implementations = requireNonNull(implementations);
44 List<M> getImplementations(final D identifier) {
45 return implementations.get(identifier);
48 Map<D, List<M>> getImplementations() {
49 return implementations;
53 Set<D> registeredIdentifiers() {
54 return implementations.keySet();
57 protected abstract Set<D> registeredIdentifiers(L listener);
60 * This method adds the given DOMOperationImplementation instance for the given list operation identifiers.
62 * @param implementation the DOMOperationImplementation instance to add
63 * @param newOprs the List of new RPCs/Actions that the DOMOperationImplementation provides, must be mutable
64 * @return a new instance of DOMActionRoutingTableEntry with the additions
66 AbstractDOMRoutingTableEntry<D, M, L, K> add(final M implementation, final List<D> newOprs) {
67 final Builder<D, List<M>> vb = ImmutableMap.builder();
68 for (final Entry<D, List<M>> ve : implementations.entrySet()) {
69 if (newOprs.remove(ve.getKey())) {
70 final List<M> i = new ArrayList<>(ve.getValue().size() + 1);
71 i.addAll(ve.getValue());
72 i.add(implementation);
74 // New implementation is at the end, this will move it to be the last among implementations
75 // with equal cost -- relying on sort() being stable.
76 i.sort(implComparator());
77 vb.put(ve.getKey(), i);
82 for (final D ii : newOprs) {
83 final List<M> impl = new ArrayList<>(1);
84 impl.add(implementation);
88 return newInstance(vb.build());
91 AbstractDOMRoutingTableEntry<D, M, L, K> remove(final M implementation, final List<D> removed) {
92 final Builder<D, List<M>> vb = ImmutableMap.builder();
93 for (final Entry<D, List<M>> ve : implementations.entrySet()) {
94 if (removed.remove(ve.getKey())) {
95 final List<M> 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.
100 vb.put(ve.getKey(), i);
107 final Map<D, List<M>> v = vb.build();
108 return v.isEmpty() ? null : newInstance(vb.build());
111 protected abstract Comparator<M> implComparator();
113 protected abstract AbstractDOMRoutingTableEntry<D, M, L, K> newInstance(Map<D, List<M>> impls);