Adjust to yangtools-2.0.0 changes
[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
68                     .checkState(!mountPoints.containsKey(mountPoint.getIdentifier()), "Mount point already exists");
69             mountPoints.put(mountPoint.getIdentifier(), mountPoint);
70         }
71         notifyMountCreated(mountPoint.getIdentifier());
72
73         return new MountRegistration(mountPoint);
74     }
75
76     public void unregisterMountPoint(final YangInstanceIdentifier mountPointId) {
77         synchronized (mountPoints) {
78             Preconditions.checkState(mountPoints.containsKey(mountPointId), "Mount point does not exist");
79             mountPoints.remove(mountPointId);
80         }
81         notifyMountRemoved(mountPointId);
82     }
83
84     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
85
86         ClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
87         private SimpleDOMMountPoint mountPoint;
88         private final YangInstanceIdentifier path;
89         private SchemaContext schemaContext;
90
91         public DOMMountPointBuilderImpl(final YangInstanceIdentifier path) {
92             this.path = path;
93         }
94
95         @Override
96         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
97             services.putInstance(type, impl);
98             return this;
99         }
100
101         @Override
102         public DOMMountPointBuilder addInitialSchemaContext(final SchemaContext ctx) {
103             schemaContext = ctx;
104             return this;
105         }
106
107         @Override
108         public ObjectRegistration<DOMMountPoint> register() {
109             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
110             mountPoint = SimpleDOMMountPoint.create(path, services,schemaContext);
111             return registerMountPoint(mountPoint);
112         }
113     }
114
115     private final class MountRegistration implements ObjectRegistration<DOMMountPoint> {
116         private final DOMMountPoint mountPoint;
117
118         MountRegistration(final DOMMountPoint mountPoint) {
119             this.mountPoint = mountPoint;
120         }
121
122         @Override
123         public DOMMountPoint getInstance() {
124             return mountPoint;
125         }
126
127         @Override
128         public void close() {
129             unregisterMountPoint(mountPoint.getIdentifier());
130         }
131     }
132 }

©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.