4fd4724589c03f9b192a95dce3cc5dd032035881
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / 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 package org.opendaylight.mdsal.dom.broker;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.MutableClassToInstanceMap;
14 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Optional;
18 import javax.inject.Singleton;
19 import org.kohsuke.MetaInfServices;
20 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMService;
24 import org.opendaylight.mdsal.dom.spi.SimpleDOMMountPoint;
25 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.concepts.ObjectRegistration;
28 import org.opendaylight.yangtools.util.ListenerRegistry;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Deactivate;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 @Component(immediate = true)
37 @MetaInfServices
38 @Singleton
39 public final class DOMMountPointServiceImpl implements DOMMountPointService {
40     private static final Logger LOG = LoggerFactory.getLogger(DOMMountPointServiceImpl.class);
41
42     private final Map<YangInstanceIdentifier, DOMMountPoint> mountPoints = new HashMap<>();
43
44     private final ListenerRegistry<DOMMountPointListener> listeners = ListenerRegistry.create();
45
46     @Override
47     public Optional<DOMMountPoint> getMountPoint(final YangInstanceIdentifier path) {
48         return Optional.ofNullable(mountPoints.get(path));
49     }
50
51     @Override
52     public DOMMountPointBuilder createMountPoint(final YangInstanceIdentifier path) {
53         checkState(!mountPoints.containsKey(path), "Mount point already exists");
54         return new DOMMountPointBuilderImpl(path);
55     }
56
57     @Override
58     public ListenerRegistration<DOMMountPointListener> registerProvisionListener(final DOMMountPointListener listener) {
59         return listeners.register(listener);
60     }
61
62     @Activate
63     @SuppressWarnings("static-method")
64     void activate() {
65         LOG.info("DOMMountPointService activated");
66     }
67
68     @Deactivate
69     @SuppressWarnings("static-method")
70     void deactivate() {
71         LOG.info("DOMMountPointService deactivated");
72     }
73
74     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
75             justification = "https://github.com/spotbugs/spotbugs/issues/811")
76     @SuppressWarnings("checkstyle:IllegalCatch")
77     private ObjectRegistration<DOMMountPoint> registerMountPoint(final SimpleDOMMountPoint mountPoint) {
78         final YangInstanceIdentifier mountPointId = mountPoint.getIdentifier();
79         synchronized (mountPoints) {
80             final DOMMountPoint prev = mountPoints.putIfAbsent(mountPointId, mountPoint);
81             checkState(prev == null, "Mount point %s already exists as %s", mountPointId, prev);
82         }
83         listeners.streamListeners().forEach(listener -> {
84             try {
85                 listener.onMountPointCreated(mountPointId);
86             } catch (final Exception ex) {
87                 LOG.error("Listener {} failed on mount point {} created event", listener, mountPoint, ex);
88             }
89         });
90
91         return new AbstractObjectRegistration<>(mountPoint) {
92             @Override
93             protected void removeRegistration() {
94                 unregisterMountPoint(getInstance().getIdentifier());
95             }
96         };
97     }
98
99     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
100             justification = "https://github.com/spotbugs/spotbugs/issues/811")
101     @SuppressWarnings("checkstyle:IllegalCatch")
102     private void unregisterMountPoint(final YangInstanceIdentifier mountPointId) {
103         synchronized (mountPoints) {
104             if (mountPoints.remove(mountPointId) == null) {
105                 LOG.warn("Removing non-existent mount point {} at", mountPointId, new Throwable());
106                 return;
107             }
108         }
109
110         listeners.streamListeners().forEach(listener -> {
111             try {
112                 listener.onMountPointRemoved(mountPointId);
113             } catch (final Exception ex) {
114                 LOG.error("Listener {} failed on mount point {} removed event", listener, mountPointId, ex);
115             }
116         });
117     }
118
119     private final class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
120
121         private final MutableClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
122         private final YangInstanceIdentifier path;
123
124         private SimpleDOMMountPoint mountPoint;
125
126         DOMMountPointBuilderImpl(final YangInstanceIdentifier path) {
127             this.path = requireNonNull(path);
128         }
129
130         @Override
131         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
132             services.putInstance(requireNonNull(type), requireNonNull(impl));
133             return this;
134         }
135
136         @Override
137         public ObjectRegistration<DOMMountPoint> register() {
138             checkState(mountPoint == null, "Mount point is already built.");
139             mountPoint = SimpleDOMMountPoint.create(path, services);
140             return registerMountPoint(mountPoint);
141         }
142     }
143 }