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