Fix checkstyle issues in module sal-dom-broker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / 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.controller.md.sal.dom.broker.impl;
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.collect.Maps;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.util.ArrayList;
16 import java.util.Comparator;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Map.Entry;
20 import java.util.Set;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28
29 abstract class AbstractDOMRpcRoutingTableEntry {
30     private final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls;
31     private final SchemaPath schemaPath;
32
33     AbstractDOMRpcRoutingTableEntry(final SchemaPath schemaPath,
34         final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls) {
35         this.schemaPath = Preconditions.checkNotNull(schemaPath);
36         this.impls = Preconditions.checkNotNull(impls);
37     }
38
39     final SchemaPath getSchemaPath() {
40         return schemaPath;
41     }
42
43     final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
44         return impls.get(context);
45     }
46
47     final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
48         return impls;
49     }
50
51     final boolean containsContext(final YangInstanceIdentifier contextReference) {
52         return impls.containsKey(contextReference);
53     }
54
55     final Set<YangInstanceIdentifier> registeredIdentifiers(final DOMRpcAvailabilityListener listener) {
56         return Maps.filterValues(impls, list -> list.stream().anyMatch(listener::acceptsImplementation)).keySet();
57     }
58
59     /**
60      * Adds an entry to the DOM RPC routing table.
61      *
62      * @param implementation RPC implementation
63      * @param newRpcs List of new RPCs, must be mutable
64      */
65     final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation,
66             final List<YangInstanceIdentifier> newRpcs) {
67         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
68         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
69             if (newRpcs.remove(ve.getKey())) {
70                 final List<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
71                 i.addAll(ve.getValue());
72                 i.add(implementation);
73
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(Comparator.comparingLong(DOMRpcImplementation::invocationCost));
77                 vb.put(ve.getKey(), i);
78             } else {
79                 vb.put(ve);
80             }
81         }
82         for (final YangInstanceIdentifier ii : newRpcs) {
83             final List<DOMRpcImplementation> impl = new ArrayList<>(1);
84             impl.add(implementation);
85             vb.put(ii, impl);
86         }
87
88         return newInstance(vb.build());
89     }
90
91     final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation,
92             final List<YangInstanceIdentifier> removed) {
93         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
94         for (final Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
95             if (removed.remove(ve.getKey())) {
96                 final List<DOMRpcImplementation> 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.
100                 if (!i.isEmpty()) {
101                     vb.put(ve.getKey(), i);
102                 }
103             } else {
104                 vb.put(ve);
105             }
106         }
107
108         final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
109         return v.isEmpty() ? null : newInstance(v);
110     }
111
112     protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(NormalizedNode<?, ?> input);
113
114     protected abstract AbstractDOMRpcRoutingTableEntry newInstance(
115             Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);
116 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.