f0fac75ded532a24998ffb75f29cea94247c0293
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / AbstractDOMRoutingTable.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.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;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Set;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30
31 /**
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
39  */
40 @Beta
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;
45
46     AbstractDOMRoutingTable(final Map<K, E> operations, final EffectiveModelContext schemaContext) {
47         this.operations = requireNonNull(operations);
48         this.schemaContext = schemaContext;
49     }
50
51     AbstractDOMRoutingTable<I, D, M, L, K, E> setSchemaContext(final EffectiveModelContext context) {
52         final Builder<K, E> b = ImmutableMap.builder();
53
54         for (Entry<K, E> e : operations.entrySet()) {
55             final E entry = createOperationEntry(context, e.getKey(), e.getValue().getImplementations());
56             if (entry != null) {
57                 b.put(e.getKey(), entry);
58             }
59         }
60
61         return newInstance(b.build(), context);
62     }
63
64     AbstractDOMRoutingTable<I, D, M, L, K, E> add(final M implementation, final Set<I> oprsToAdd) {
65         if (oprsToAdd.isEmpty()) {
66             return this;
67         }
68
69         // First decompose the identifiers to a multimap
70         final ListMultimap<K, D> toAdd = decomposeIdentifiers(oprsToAdd);
71
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);
79             } else {
80                 mb.put(re);
81             }
82         }
83
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()) {
89                 vb.put(i, v);
90             }
91
92             final E entry = createOperationEntry(schemaContext, e.getKey(),
93                 vb.build());
94             if (entry != null) {
95                 mb.put(e.getKey(), entry);
96             }
97         }
98
99         return newInstance(mb.build(), schemaContext);
100     }
101
102     AbstractDOMRoutingTable<I, D, M, L, K, E> remove(final M implementation, final Set<I> instances) {
103         if (instances.isEmpty()) {
104             return this;
105         }
106
107         // First decompose the identifiers to a multimap
108         final ListMultimap<K, D> toRemove = decomposeIdentifiers(instances);
109
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);
116                 if (ne != null) {
117                     b.put(e.getKey(), ne);
118                 }
119             } else {
120                 b.put(e);
121             }
122         }
123
124         // All done, whatever is in toRemove, was not there in the first place
125         return newInstance(b.build(), schemaContext);
126     }
127
128     @VisibleForTesting
129     Map<K, Set<D>> getOperations() {
130         return Maps.transformValues(operations, AbstractDOMRoutingTableEntry::registeredIdentifiers);
131     }
132
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);
139             }
140         }
141
142         return ret;
143     }
144
145     @Nullable AbstractDOMRoutingTableEntry<D, M, L, K> getEntry(final @NonNull K type) {
146         return operations.get(type);
147     }
148
149     protected abstract AbstractDOMRoutingTable<I, D, M, L, K, E> newInstance(Map<K, E> operations,
150             EffectiveModelContext schemaContext);
151
152     abstract ListMultimap<K, D> decomposeIdentifiers(Set<I> instances);
153
154     abstract E createOperationEntry(EffectiveModelContext context, K key, Map<D, List<M>> implementations);
155 }