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