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