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.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMap.Builder;
17 import com.google.common.collect.ListMultimap;
18 import com.google.common.collect.Maps;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.EventListener;
22 import java.util.HashMap;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 * Abstract routing table definition for Action and RPC.
33 * @param <I> instance type of RPC or Acton
34 * @param <D> identifier type of RPC or Acton
35 * @param <M> implementation type of RPC or Acton
36 * @param <L> listener type of RPC or Acton
37 * @param <E> routing entry type of RPC or Acton
38 * @param <K> routing key type
41 abstract class AbstractDOMRoutingTable<I, D, M, L extends EventListener, K,
42 E extends AbstractDOMRoutingTableEntry<D, M, L, K>> {
43 private final Map<K, E> operations;
44 private final EffectiveModelContext schemaContext;
46 AbstractDOMRoutingTable(final Map<K, E> operations, final EffectiveModelContext schemaContext) {
47 this.operations = requireNonNull(operations);
48 this.schemaContext = schemaContext;
51 AbstractDOMRoutingTable<I, D, M, L, K, E> setSchemaContext(final EffectiveModelContext context) {
52 final Builder<K, E> b = ImmutableMap.builder();
54 for (Entry<K, E> e : operations.entrySet()) {
55 final E entry = createOperationEntry(context, e.getKey(), e.getValue().getImplementations());
57 b.put(e.getKey(), entry);
61 return newInstance(b.build(), context);
64 AbstractDOMRoutingTable<I, D, M, L, K, E> add(final M implementation, final Set<I> oprsToAdd) {
65 if (oprsToAdd.isEmpty()) {
69 // First decompose the identifiers to a multimap
70 final ListMultimap<K, D> toAdd = decomposeIdentifiers(oprsToAdd);
72 // Now iterate over existing entries, modifying them as appropriate...
73 final Builder<K, E> mb = ImmutableMap.builder();
74 for (Entry<K, E> re : this.operations.entrySet()) {
75 List<D> newOperations = new ArrayList<>(toAdd.removeAll(re.getKey()));
76 if (!newOperations.isEmpty()) {
77 final E ne = (E) re.getValue().add(implementation, newOperations);
78 mb.put(re.getKey(), ne);
84 // Finally add whatever is left in the decomposed multimap
85 for (Entry<K, Collection<D>> e : toAdd.asMap().entrySet()) {
86 final Builder<D, List<M>> vb = ImmutableMap.builder();
87 final List<M> v = ImmutableList.of(implementation);
88 for (D i : e.getValue()) {
92 final E entry = createOperationEntry(schemaContext, e.getKey(),
95 mb.put(e.getKey(), entry);
99 return newInstance(mb.build(), schemaContext);
102 AbstractDOMRoutingTable<I, D, M, L, K, E> remove(final M implementation, final Set<I> instances) {
103 if (instances.isEmpty()) {
107 // First decompose the identifiers to a multimap
108 final ListMultimap<K, D> toRemove = decomposeIdentifiers(instances);
110 // Now iterate over existing entries, modifying them as appropriate...
111 final Builder<K, E> b = ImmutableMap.builder();
112 for (Entry<K, E> e : this.operations.entrySet()) {
113 final List<D> removed = new ArrayList<>(toRemove.removeAll(e.getKey()));
114 if (!removed.isEmpty()) {
115 final E ne = (E) e.getValue().remove(implementation, removed);
117 b.put(e.getKey(), ne);
124 // All done, whatever is in toRemove, was not there in the first place
125 return newInstance(b.build(), schemaContext);
129 Map<K, Set<D>> getOperations() {
130 return Maps.transformValues(operations, AbstractDOMRoutingTableEntry::registeredIdentifiers);
133 Map<K, Set<D>> getOperations(final L listener) {
134 final Map<K, Set<D>> ret = new HashMap<>(operations.size());
135 for (Entry<K, E> e : operations.entrySet()) {
136 final Set<D> ids = e.getValue().registeredIdentifiers(listener);
137 if (!ids.isEmpty()) {
138 ret.put(e.getKey(), ids);
145 @Nullable AbstractDOMRoutingTableEntry<D, M, L, K> getEntry(final @NonNull K type) {
146 return operations.get(type);
149 protected abstract AbstractDOMRoutingTable<I, D, M, L, K, E> newInstance(Map<K, E> operations,
150 EffectiveModelContext schemaContext);
152 abstract ListMultimap<K, D> decomposeIdentifiers(Set<I> instances);
154 abstract E createOperationEntry(EffectiveModelContext context, K key, Map<D, List<M>> implementations);