Merge "Fixed testool performance bottleneck due to testtool using NetconfMonitoring...
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / compat / CompositeRoutedRpcRegistration.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.binding.compat;
9
10 import com.google.common.base.Throwables;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMRpcProviderServiceAdapter;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
16 import org.opendaylight.yangtools.concepts.ObjectRegistration;
17 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.RpcService;
20
21 final class CompositeRoutedRpcRegistration<T extends RpcService> implements RoutedRpcRegistration<T> {
22
23     private final Class<T> type;
24     private final T instance;
25     private final BindingDOMRpcProviderServiceAdapter adapter;
26     private final Map<InstanceIdentifier<?>, ObjectRegistration<T>> registrations = new HashMap<>(2);
27
28     CompositeRoutedRpcRegistration(final Class<T> type, final T impl, final BindingDOMRpcProviderServiceAdapter providerAdapter) {
29         this.type = type;
30         this.instance = impl;
31         this.adapter = providerAdapter;
32     }
33
34     @Override
35     public Class<T> getServiceType() {
36         return type;
37     }
38
39     @Override
40     public T getInstance() {
41         return instance;
42     }
43
44     @Deprecated
45     @Override
46     public void registerInstance(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> path) {
47         registerPath(context, path);
48     }
49
50     @Override
51     public synchronized void registerPath(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> path) {
52         if(!registrations.containsKey(path)) {
53             registrations.put(path, adapter.registerRpcImplementation(type, instance, ImmutableSet.<InstanceIdentifier<?>>of(path)));
54         }
55     }
56
57
58     @Override
59     @Deprecated
60     public void unregisterInstance(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> path) {
61         unregisterPath(context, path);
62     }
63
64     @Override
65     public synchronized  void unregisterPath(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> path) {
66         final ObjectRegistration<T> reg = registrations.remove(path);
67         if(reg != null) {
68             try {
69                 reg.close();
70             } catch (final Exception e) {
71                 // FIXME: Once we have proper subclass of ObjectRegistrationo
72                 throw Throwables.propagate(e);
73             }
74         }
75     }
76
77     @Override
78     public synchronized void close() {
79         try {
80             for(final ObjectRegistration<T> reg : registrations.values()) {
81                     reg.close();
82             }
83         } catch (final Exception e) {
84             throw Throwables.propagate(e);
85         }
86     }
87
88 }