c32576ecdc1174873963622a9903ac956068347c
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / mount / DOMMountPointServiceImpl.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
9 package org.opendaylight.controller.md.sal.dom.broker.impl.mount;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ClassToInstanceMap;
14 import com.google.common.collect.MutableClassToInstanceMap;
15 import java.util.HashMap;
16 import java.util.Map;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMService;
20 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
21 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.concepts.ObjectRegistration;
24 import org.opendaylight.yangtools.util.ListenerRegistry;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27
28 public class DOMMountPointServiceImpl implements DOMMountPointService {
29
30     private final Map<YangInstanceIdentifier, DOMMountPoint> mountPoints = new HashMap<>();
31
32     private final ListenerRegistry<MountProvisionListener> listeners = ListenerRegistry.create();
33
34     @Override
35     public Optional<DOMMountPoint> getMountPoint(final YangInstanceIdentifier path) {
36         return Optional.fromNullable(mountPoints.get(path));
37     }
38
39     @Override
40     public DOMMountPointBuilder createMountPoint(final YangInstanceIdentifier path) {
41         Preconditions.checkState(!mountPoints.containsKey(path), "Mount point already exists");
42         return new DOMMountPointBuilderImpl(path);
43     }
44
45     public void notifyMountCreated(final YangInstanceIdentifier identifier) {
46         for (final ListenerRegistration<MountProvisionListener> listener : listeners
47                 .getListeners()) {
48             listener.getInstance().onMountPointCreated(identifier);
49         }
50     }
51
52     public void notifyMountRemoved(final YangInstanceIdentifier identifier) {
53         for (final ListenerRegistration<MountProvisionListener> listener : listeners
54                 .getListeners()) {
55             listener.getInstance().onMountPointRemoved(identifier);
56         }
57     }
58
59     @Override
60     public ListenerRegistration<MountProvisionListener> registerProvisionListener(
61             final MountProvisionListener listener) {
62         return listeners.register(listener);
63     }
64
65     public ObjectRegistration<DOMMountPoint> registerMountPoint(final DOMMountPoint mountPoint) {
66         synchronized (mountPoints) {
67             Preconditions.checkState(!mountPoints.containsKey(mountPoint.getIdentifier()), "Mount point already exists");
68             mountPoints.put(mountPoint.getIdentifier(), mountPoint);
69         }
70         notifyMountCreated(mountPoint.getIdentifier());
71
72         return new MountRegistration(mountPoint);
73     }
74
75     public void unregisterMountPoint(final YangInstanceIdentifier mountPointId) {
76         synchronized (mountPoints) {
77             Preconditions.checkState(mountPoints.containsKey(mountPointId), "Mount point does not exist");
78             mountPoints.remove(mountPointId);
79         }
80         notifyMountRemoved(mountPointId);
81     }
82
83     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
84
85         ClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
86         private SimpleDOMMountPoint mountPoint;
87         private final YangInstanceIdentifier path;
88         private SchemaContext schemaContext;
89
90         public DOMMountPointBuilderImpl(final YangInstanceIdentifier path) {
91             this.path = path;
92         }
93
94         @Override
95         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
96             services.putInstance(type, impl);
97             return this;
98         }
99
100         @Override
101         public DOMMountPointBuilder addInitialSchemaContext(final SchemaContext ctx) {
102             schemaContext = ctx;
103             return this;
104         }
105
106         @Override
107         public ObjectRegistration<DOMMountPoint> register() {
108             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
109             mountPoint = SimpleDOMMountPoint.create(path, services,schemaContext);
110             return registerMountPoint(mountPoint);
111         }
112     }
113
114     private final class MountRegistration implements ObjectRegistration<DOMMountPoint> {
115         private final DOMMountPoint mountPoint;
116
117         public MountRegistration(final DOMMountPoint mountPoint) {
118             this.mountPoint = mountPoint;
119         }
120
121         @Override
122         public DOMMountPoint getInstance() {
123             return mountPoint;
124         }
125
126         @Override
127         public void close() throws Exception {
128             unregisterMountPoint(mountPoint.getIdentifier());
129         }
130     }
131 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.