2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.MutableClassToInstanceMap;
13 import java.util.HashMap;
15 import java.util.Optional;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.kohsuke.MetaInfServices;
19 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
20 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.mdsal.dom.api.DOMService;
23 import org.opendaylight.mdsal.dom.spi.SimpleDOMMountPoint;
24 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
25 import org.opendaylight.yangtools.concepts.ObjectRegistration;
26 import org.opendaylight.yangtools.concepts.Registration;
27 import org.opendaylight.yangtools.util.ObjectRegistry;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
37 public final class DOMMountPointServiceImpl implements DOMMountPointService {
38 private static final Logger LOG = LoggerFactory.getLogger(DOMMountPointServiceImpl.class);
40 private final Map<YangInstanceIdentifier, DOMMountPoint> mountPoints = new HashMap<>();
41 private final ObjectRegistry<DOMMountPointListener> listeners =
42 ObjectRegistry.createConcurrent("mount point listeners");
46 public DOMMountPointServiceImpl() {
47 // Exposed only for DI
51 public Optional<DOMMountPoint> getMountPoint(final YangInstanceIdentifier path) {
52 return Optional.ofNullable(mountPoints.get(path));
56 public synchronized DOMMountPointBuilder createMountPoint(final YangInstanceIdentifier path) {
57 checkNotExists(path, mountPoints.get(requireNonNull(path)));
58 return new DOMMountPointBuilderImpl(path);
62 public Registration registerProvisionListener(final DOMMountPointListener listener) {
63 return listeners.register(listener);
66 @SuppressWarnings("checkstyle:IllegalCatch")
67 private ObjectRegistration<DOMMountPoint> registerMountPoint(final SimpleDOMMountPoint mountPoint) {
68 final YangInstanceIdentifier mountPointId = mountPoint.getIdentifier();
69 synchronized (mountPoints) {
70 checkNotExists(mountPointId, mountPoints.putIfAbsent(mountPointId, mountPoint));
72 listeners.streamObjects().forEach(listener -> {
74 listener.onMountPointCreated(mountPointId);
75 } catch (final Exception ex) {
76 LOG.error("Listener {} failed on mount point {} created event", listener, mountPoint, ex);
80 return new AbstractObjectRegistration<>(mountPoint) {
82 protected void removeRegistration() {
83 unregisterMountPoint(getInstance().getIdentifier());
88 @SuppressWarnings("checkstyle:IllegalCatch")
89 private void unregisterMountPoint(final YangInstanceIdentifier mountPointId) {
90 synchronized (mountPoints) {
91 if (mountPoints.remove(mountPointId) == null) {
92 LOG.warn("Removing non-existent mount point {} at", mountPointId, new Throwable());
97 listeners.streamObjects().forEach(listener -> {
99 listener.onMountPointRemoved(mountPointId);
100 } catch (final Exception ex) {
101 LOG.error("Listener {} failed on mount point {} removed event", listener, mountPointId, ex);
106 private static void checkNotExists(final YangInstanceIdentifier id, final DOMMountPoint mountPoint) {
107 if (mountPoint != null) {
108 throw new IllegalStateException("Mount point " + id + " already exists as " + mountPoint);
112 private final class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
113 private final MutableClassToInstanceMap<DOMService<?, ?>> services = MutableClassToInstanceMap.create();
114 private final YangInstanceIdentifier path;
116 private SimpleDOMMountPoint mountPoint;
118 DOMMountPointBuilderImpl(final YangInstanceIdentifier path) {
119 this.path = requireNonNull(path);
123 public <T extends DOMService<T, E>, E extends DOMService.Extension<T, E>> DOMMountPointBuilder addService(
124 final Class<T> type, final T impl) {
125 services.putInstance(type, requireNonNull(impl));
130 public ObjectRegistration<DOMMountPoint> register() {
131 if (mountPoint != null) {
132 throw new IllegalStateException("Mount point is already built");
134 mountPoint = SimpleDOMMountPoint.create(path, services);
135 return registerMountPoint(mountPoint);