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.EventListener;
19 import java.util.List;
21 import java.util.Map.Entry;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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
32 abstract class AbstractDOMRoutingTableEntry<D, M, L extends EventListener> {
33 private final Map<D, List<M>> implementations;
34 private final SchemaPath type;
36 AbstractDOMRoutingTableEntry(final SchemaPath type, final Map<D, List<M>> implementations) {
37 this.type = requireNonNull(type);
38 this.implementations = requireNonNull(implementations);
41 SchemaPath getType() {
45 List<M> getImplementations(final D identifier) {
46 return implementations.get(identifier);
49 Map<D, List<M>> getImplementations() {
50 return implementations;
54 Set<D> registeredIdentifiers() {
55 return implementations.keySet();
58 protected abstract Set<D> registeredIdentifiers(L listener);
61 * This method adds the given DOMOperationImplementation instance for the given list operation identifiers.
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
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);
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);
83 for (final D ii : newOprs) {
84 final List<M> impl = new ArrayList<>(1);
85 impl.add(implementation);
89 return newInstance(vb.build());
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.
101 vb.put(ve.getKey(), i);
108 final Map<D, List<M>> v = vb.build();
109 return v.isEmpty() ? null : newInstance(vb.build());
112 protected abstract Comparator<M> implComparator();
114 protected abstract AbstractDOMRoutingTableEntry<D, M, L> newInstance(Map<D, List<M>> impls);