f8059fed41ec07fdb39bc0eb09408b6d8c60d576
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / impl / MountPointManagerImpl.java
1 package org.opendaylight.controller.sal.binding.impl;
2
3 import java.util.concurrent.ConcurrentHashMap;
4 import java.util.concurrent.ConcurrentMap;
5
6 import org.opendaylight.controller.md.sal.binding.util.AbstractBindingSalProviderInstance;
7 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
8 import org.opendaylight.controller.sal.binding.api.mount.MountProviderInstance;
9 import org.opendaylight.controller.sal.binding.api.mount.MountProviderService;
10 import org.opendaylight.yangtools.concepts.ListenerRegistration;
11 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import com.google.common.util.concurrent.ListeningExecutorService;
16
17 public class MountPointManagerImpl implements MountProviderService {
18
19     public final Logger LOG = LoggerFactory.getLogger(MountPointManagerImpl.class);
20
21     private final ConcurrentMap<InstanceIdentifier<?>, BindingMountPointImpl> mountPoints;
22     private final ListenerRegistry<MountProvisionListener> listeners = ListenerRegistry.create();
23     
24     private ListeningExecutorService notificationExecutor;
25     private ListeningExecutorService dataCommitExecutor;
26
27     public MountPointManagerImpl() {
28         mountPoints = new ConcurrentHashMap<>();
29     }
30
31     public ListeningExecutorService getNotificationExecutor() {
32         return notificationExecutor;
33     }
34
35     public void setNotificationExecutor(ListeningExecutorService notificationExecutor) {
36         this.notificationExecutor = notificationExecutor;
37     }
38
39     public ListeningExecutorService getDataCommitExecutor() {
40         return dataCommitExecutor;
41     }
42
43     public void setDataCommitExecutor(ListeningExecutorService dataCommitExecutor) {
44         this.dataCommitExecutor = dataCommitExecutor;
45     }
46
47     @Override
48     public synchronized BindingMountPointImpl createMountPoint(InstanceIdentifier<?> path) {
49         BindingMountPointImpl potential = mountPoints.get(path);
50         if (potential != null) {
51             throw new IllegalStateException("Mount point already exists.");
52         }
53         return createOrGetMountPointImpl(path);
54     }
55
56     @Override
57     public BindingMountPointImpl createOrGetMountPoint(InstanceIdentifier<?> path) {
58         BindingMountPointImpl potential = getMountPoint(path);
59         if (potential != null) {
60             return potential;
61         }
62         return createOrGetMountPointImpl(path);
63     }
64
65     @Override
66     public BindingMountPointImpl getMountPoint(InstanceIdentifier<?> path) {
67         return mountPoints.get(path);
68     }
69
70     private synchronized BindingMountPointImpl createOrGetMountPointImpl(InstanceIdentifier<?> path) {
71         BindingMountPointImpl potential = getMountPoint(path);
72         if (potential != null) {
73             return potential;
74         }
75         RpcProviderRegistryImpl rpcRegistry = new RpcProviderRegistryImpl("mount");
76         NotificationBrokerImpl notificationBroker = new NotificationBrokerImpl();
77         notificationBroker.setExecutor(getNotificationExecutor());
78         DataBrokerImpl dataBroker = new DataBrokerImpl();
79         dataBroker.setExecutor(getDataCommitExecutor());
80         BindingMountPointImpl mountInstance = new BindingMountPointImpl(path, rpcRegistry, notificationBroker,
81                 dataBroker);
82         mountPoints.putIfAbsent(path, mountInstance);
83         notifyMountPointCreated(path);
84         return mountInstance;
85     }
86
87     private void notifyMountPointCreated(InstanceIdentifier<?> path) {
88         for (ListenerRegistration<MountProvisionListener> listener : listeners) {
89             try {
90                 listener.getInstance().onMountPointCreated(path);
91             } catch (Exception e) {
92                 LOG.error("Unhandled exception during invoking listener.", e);
93             }
94         }
95     }
96
97     @Override
98     public ListenerRegistration<MountProvisionListener> registerProvisionListener(MountProvisionListener listener) {
99         return listeners.register(listener);
100     }
101
102     public class BindingMountPointImpl extends
103             AbstractBindingSalProviderInstance<DataBrokerImpl, NotificationBrokerImpl, RpcProviderRegistryImpl>
104     implements MountProviderInstance {
105
106         private InstanceIdentifier<?> identifier;
107
108         public BindingMountPointImpl(org.opendaylight.yangtools.yang.binding.InstanceIdentifier<?> identifier,
109                 RpcProviderRegistryImpl rpcRegistry, NotificationBrokerImpl notificationBroker,
110                 DataBrokerImpl dataBroker) {
111             super(rpcRegistry, notificationBroker, dataBroker);
112             this.identifier = identifier;
113         }
114         
115         @Override
116         public InstanceIdentifier<?> getIdentifier() {
117             return this.identifier;
118         }
119     }
120 }