BUG-2573: create DOMRpcRouter
[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.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.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
21 import org.opendaylight.controller.md.sal.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, List<DOMRpcImplementation>> impls) {
31         this.schemaPath = Preconditions.checkNotNull(schemaPath);
32         this.impls = Preconditions.checkNotNull(impls);
33     }
34
35     protected final SchemaPath getSchemaPath() {
36         return schemaPath;
37     }
38
39     protected final List<DOMRpcImplementation> getImplementations(final YangInstanceIdentifier context) {
40         return impls.get(context);
41     }
42
43     final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> getImplementations() {
44         return impls;
45     }
46
47     public boolean containsContext(final YangInstanceIdentifier contextReference) {
48         return impls.containsKey(contextReference);
49     }
50
51     final Set<YangInstanceIdentifier> registeredIdentifiers() {
52         return impls.keySet();
53     }
54
55     final AbstractDOMRpcRoutingTableEntry add(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> newRpcs) {
56         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
57         for (Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
58             if (newRpcs.remove(ve.getKey())) {
59                 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue().size() + 1);
60                 i.addAll(ve.getValue());
61                 i.add(implementation);
62                 vb.put(ve.getKey(), i);
63             } else {
64                 vb.put(ve);
65             }
66         }
67
68         return newInstance(vb.build());
69     }
70
71     final AbstractDOMRpcRoutingTableEntry remove(final DOMRpcImplementation implementation, final List<YangInstanceIdentifier> removed) {
72         final Builder<YangInstanceIdentifier, List<DOMRpcImplementation>> vb = ImmutableMap.builder();
73         for (Entry<YangInstanceIdentifier, List<DOMRpcImplementation>> ve : impls.entrySet()) {
74             if (removed.remove(ve.getKey())) {
75                 final ArrayList<DOMRpcImplementation> i = new ArrayList<>(ve.getValue());
76                 i.remove(implementation);
77                 // We could trimToSize(), but that may perform another copy just to get rid
78                 // of a single element. That is probably not worth the trouble.
79                 if (!i.isEmpty()) {
80                     vb.put(ve.getKey(), i);
81                 }
82             } else {
83                 vb.put(ve);
84             }
85         }
86
87         final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> v = vb.build();
88         return v.isEmpty() ? null : newInstance(v);
89     }
90
91     protected abstract CheckedFuture<DOMRpcResult, DOMRpcException> invokeRpc(final NormalizedNode<?, ?> input);
92     protected abstract AbstractDOMRpcRoutingTableEntry newInstance(final Map<YangInstanceIdentifier, List<DOMRpcImplementation>> impls);
93 }

©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.