Merge "BUG-1381: make users of JavassistUtils.getLock synchronized properly"
[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 java.util.HashMap;
12 import java.util.Map;
13
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMService;
17 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
18 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.concepts.ObjectRegistration;
21 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.ClassToInstanceMap;
28 import com.google.common.collect.MutableClassToInstanceMap;
29
30 public class DOMMountPointServiceImpl implements DOMMountPointService {
31
32     private final Map<InstanceIdentifier, SimpleDOMMountPoint> mountPoints = new HashMap<>();
33
34     private final ListenerRegistry<MountProvisionListener> listeners = ListenerRegistry.create();
35
36     @Override
37     public Optional<DOMMountPoint> getMountPoint(final InstanceIdentifier path) {
38         return Optional.<DOMMountPoint>fromNullable(mountPoints.get(path));
39     }
40
41     @Override
42     public DOMMountPointBuilder createMountPoint(final InstanceIdentifier path) {
43         Preconditions.checkState(!mountPoints.containsKey(path), "Mount point already exists");
44         return new DOMMountPointBuilderImpl(path);
45     }
46
47     public void notifyMountCreated(final InstanceIdentifier identifier) {
48         for (final ListenerRegistration<MountProvisionListener> listener : listeners
49                 .getListeners()) {
50             listener.getInstance().onMountPointCreated(identifier);
51         }
52     }
53
54     @Override
55     public ListenerRegistration<MountProvisionListener> registerProvisionListener(
56             final MountProvisionListener listener) {
57         return listeners.register(listener);
58     }
59
60     public ObjectRegistration<DOMMountPoint> registerMountPoint(final SimpleDOMMountPoint mountPoint) {
61         synchronized (mountPoints) {
62             Preconditions.checkState(!mountPoints.containsKey(mountPoint.getIdentifier()), "Mount point already exists");
63             mountPoints.put(mountPoint.getIdentifier(), mountPoint);
64         }
65         notifyMountCreated(mountPoint.getIdentifier());
66
67         // FIXME this shouldnt be null
68         return null;
69     }
70
71     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
72
73         ClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
74         private SimpleDOMMountPoint mountPoint;
75         private final InstanceIdentifier path;
76         private SchemaContext schemaContext;
77
78         public DOMMountPointBuilderImpl(final InstanceIdentifier path) {
79             this.path = path;
80         }
81
82         @Override
83         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
84             services.putInstance(type, impl);
85             return this;
86         }
87
88         @Override
89         public DOMMountPointBuilder addInitialSchemaContext(final SchemaContext ctx) {
90             schemaContext = ctx;
91             return this;
92         }
93
94         @Override
95         public ObjectRegistration<DOMMountPoint> register() {
96             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
97             mountPoint = SimpleDOMMountPoint.create(path, services,schemaContext);
98             return registerMountPoint(mountPoint);
99         }
100     }
101 }

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