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