checkStyleViolationSeverity=error implemented for mdsal-dom-broker
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / AbstractDOMRpcRoutingTableEntry.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19 import org.opendaylight.mdsal.dom.api.DOMRpcException;
20 import org.opendaylight.mdsal.dom.api.DOMRpcImplementation;
21 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25
26 abstract class AbstractDOMRpcRoutingTableEntry {
27     private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls;
28     private final SchemaPath schemaPath;
29
30     protected AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath, final Map<YangInstanceIdentifier,
31             List<DOMRpcImplementation>> impls) {
32         this.schemaPath = Preconditions.checkNotNull(schemaPath);
33         this.impls = Preconditions.checkNotNull(impls);
34     }
35
36     protected final SchemaPath getSchemaPath() {
37         return schemaPath;
38     }
39
40     protected final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
41         return impls.get(context);
42     }
43
44     final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
45         return impls;
46     }
47
48     public boolean containsContext(final YangInstanceIdentifier contextReference) {
49         return impls.containsKey(contextReference);
50     }
51
52     final Set<YangInstanceIdentifier> registeredIdentifiers() {
53         return impls.keySet();
54     }
55
56     /**
57      * This method adds the given DOMRpcImplementation instance for the given list RPC identifiers.
58      *
59      * @param implementation the DOMRpcImplementation instance to add
60      * @param newRpcs the List of new RPCs that the DOMRpcImplementation provides, must be mutable
61      * @return a new instance of AbstractDOMRpcRoutingTableEntry with the additions
62      */
63     final AbstractDOMRpcRoutingTableEntry add(
64             final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
65         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
66         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
67             if (newRpcs.remove(ve.getKey())) {
68                 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
69                 i.addAll(ve.getValue());
70                 i.add(implementation);
71                 vb.put(ve.getKey(), i);
72             } else {
73                 vb.put(ve);
74             }
75         }
76         for (final YangInstanceIdentifier ii : newRpcs) {
77             final ArrayList<DOMRpcImplementation> impl = new ArrayList<>(1);
78             impl.add(implementation);
79             vb.put(ii,impl);
80         }
81
82         return newInstance(vb.build());
83     }
84
85     final AbstractDOMRpcRoutingTableEntry remove(
86             final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
87         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
88         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
89             if (removed.remove(ve.getKey())) {
90                 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
91                 i.remove(implementation);
92                 // We could trimToSize(), but that may perform another copy just to get rid
93                 // of a single element. That is probably not worth the trouble.
94                 if (!i.isEmpty()) {
95                     vb.put(ve.getKey(), i);
96                 }
97             } else {
98                 vb.put(ve);
99             }
100         }
101
102         final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
103         return v.isEmpty() ? null : newInstance(v);
104     }
105
106     protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
107
108     protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
109             final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);
110 }