Merge "Re-added config.version to config-module-archetype."
[controller.git] / opendaylight / md-sal / sal-binding-util / src / main / java / org / opendaylight / controller / md / sal / binding / util / AbstractBindingSalConsumerInstance.java
1 /*
2  * Copyright (c) 2014 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.util;
9
10 import org.opendaylight.controller.sal.binding.api.NotificationListener;
11 import org.opendaylight.controller.sal.binding.api.NotificationService;
12 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
13 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
14 import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
15 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
16 import org.opendaylight.yangtools.concepts.ListenerRegistration;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20 import org.opendaylight.yangtools.yang.binding.RpcService;
21
22 import com.google.common.base.Preconditions;
23
24 public abstract class AbstractBindingSalConsumerInstance<D extends DataBrokerService, N extends NotificationService, R extends RpcConsumerRegistry> //
25         implements //
26         RpcConsumerRegistry, //
27         NotificationService, //
28         DataBrokerService {
29
30     private final R rpcRegistry;
31     private final N notificationBroker;
32     private final D dataBroker;
33
34     protected final R getRpcRegistry() {
35         return rpcRegistry;
36     }
37
38     protected final N getNotificationBroker() {
39         return notificationBroker;
40     }
41
42     protected final D getDataBroker() {
43         return dataBroker;
44     }
45
46     protected final R getRpcRegistryChecked() {
47         Preconditions.checkState(rpcRegistry != null,"Rpc Registry is not available.");
48         return rpcRegistry;
49     }
50
51     protected final N getNotificationBrokerChecked() {
52         Preconditions.checkState(notificationBroker != null,"Notification Broker is not available.");
53         return notificationBroker;
54     }
55
56     protected final D getDataBrokerChecked() {
57         Preconditions.checkState(dataBroker != null, "Data Broker is not available");
58         return dataBroker;
59     }
60
61
62     protected AbstractBindingSalConsumerInstance(R rpcRegistry, N notificationBroker, D dataBroker) {
63         this.rpcRegistry = rpcRegistry;
64         this.notificationBroker = notificationBroker;
65         this.dataBroker = dataBroker;
66     }
67
68     @Override
69     public <T extends RpcService> T getRpcService(Class<T> module) {
70         return getRpcRegistryChecked().getRpcService(module);
71     }
72
73     @Override
74     public <T extends Notification> ListenerRegistration<NotificationListener<T>> registerNotificationListener(
75             Class<T> notificationType, NotificationListener<T> listener) {
76         return getNotificationBrokerChecked().registerNotificationListener(notificationType, listener);
77     }
78
79     @Override
80     public ListenerRegistration<org.opendaylight.yangtools.yang.binding.NotificationListener> registerNotificationListener(
81             org.opendaylight.yangtools.yang.binding.NotificationListener listener) {
82         return getNotificationBrokerChecked().registerNotificationListener(listener);
83     }
84
85     @Override
86     public DataModificationTransaction beginTransaction() {
87         return getDataBrokerChecked().beginTransaction();
88     }
89
90     @Override
91     @Deprecated
92     public DataObject readConfigurationData(InstanceIdentifier<? extends DataObject> path) {
93         return getDataBrokerChecked().readConfigurationData(path);
94     }
95
96     @Override
97     public DataObject readOperationalData(InstanceIdentifier<? extends DataObject> path) {
98         return getDataBrokerChecked().readOperationalData(path);
99     }
100
101     @Override
102     @Deprecated
103     public ListenerRegistration<DataChangeListener> registerDataChangeListener(
104             InstanceIdentifier<? extends DataObject> path, DataChangeListener listener) {
105         return getDataBrokerChecked().registerDataChangeListener(path, listener);
106     }
107 }