Fix modules Restconf call for mounted devices
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / BackwardsCompatibleMountPointManager.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.dom.broker;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import com.google.common.base.Optional;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
17 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
18 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
19 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.util.ListenerRegistry;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23
24 @Deprecated
25 public class BackwardsCompatibleMountPointManager implements MountProvisionService, MountProvisionListener {
26
27     private final ListenerRegistry<MountProvisionListener> listeners = ListenerRegistry.create();
28     private final ConcurrentMap<YangInstanceIdentifier, MountProvisionInstance> mounts = new ConcurrentHashMap<>();
29
30     private final DOMMountPointService domMountPointService;
31
32     public BackwardsCompatibleMountPointManager(final DOMMountPointService domMountPointService) {
33         this.domMountPointService = domMountPointService;
34     }
35
36     @Override
37     public MountProvisionInstance createMountPoint(final YangInstanceIdentifier path) {
38         checkState(!mounts.containsKey(path), "Mount already created");
39         // Create mount point instance, wrap instance of new API with BackwardsCompatibleMountPoint to preserve backwards comatibility
40         final BackwardsCompatibleMountPoint mount = new BackwardsCompatibleMountPoint(path, domMountPointService.createMountPoint(path));
41         mounts.put(path, mount);
42         return mount;
43     }
44
45     public void notifyMountCreated(final YangInstanceIdentifier identifier) {
46         for (final ListenerRegistration<MountProvisionListener> listener : listeners.getListeners()) {
47             listener.getInstance().onMountPointCreated(identifier);
48         }
49     }
50
51     public void notifyMountRemoved(final YangInstanceIdentifier identifier) {
52         for (final ListenerRegistration<MountProvisionListener> listener : listeners.getListeners()) {
53             listener.getInstance().onMountPointRemoved(identifier);
54         }
55     }
56
57     @Override
58     public MountProvisionInstance createOrGetMountPoint(
59             final YangInstanceIdentifier path) {
60         final MountProvisionInstance mount = getMountPoint(path);
61         if (mount == null) {
62             return createMountPoint(path);
63         }
64         return mount;
65     }
66
67     @Override
68     public MountProvisionInstance getMountPoint(final YangInstanceIdentifier path) {
69         // If the mount point was created here, return directly
70         if(mounts.containsKey(path)) {
71             return mounts.get(path);
72         }
73
74         // If mount was created in underlying DOMMountService, wrap as MountProvisionInstance
75         final Optional<DOMMountPoint> mount = domMountPointService.getMountPoint(path);
76         if(mount.isPresent()) {
77             return new BackwardsCompatibleMountPoint(path, mount.get());
78         } else {
79             return null;
80         }
81     }
82
83     @Override
84     public ListenerRegistration<MountProvisionListener> registerProvisionListener(
85             final MountProvisionListener listener) {
86         return domMountPointService.registerProvisionListener(listener);
87     }
88
89     @Override
90     public void onMountPointCreated(final YangInstanceIdentifier path) {
91         notifyMountCreated(path);
92     }
93
94     @Override
95     public void onMountPointRemoved(final YangInstanceIdentifier path) {
96             notifyMountRemoved(path);
97     }
98 }