Deprecate sal.core.api.mount.MountProvisionListener
[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.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ClassToInstanceMap;
15 import com.google.common.collect.MutableClassToInstanceMap;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
18 import org.opendaylight.controller.md.sal.dom.api.DOMService;
19 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
20 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
22 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.concepts.ObjectRegistration;
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 org.opendaylight.mdsal.dom.api.DOMMountPointService delegate;
31
32     @VisibleForTesting
33     public DOMMountPointServiceImpl() {
34         this(new org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl());
35     }
36
37     public DOMMountPointServiceImpl(final org.opendaylight.mdsal.dom.api.DOMMountPointService delegate) {
38         this.delegate = delegate;
39     }
40
41     @Override
42     public Optional<DOMMountPoint> getMountPoint(final YangInstanceIdentifier path) {
43         final Optional<org.opendaylight.mdsal.dom.api.DOMMountPoint> delegateMountPoint = delegate.getMountPoint(path);
44         return delegateMountPoint.isPresent() ? Optional.of(convert(delegateMountPoint.get())) : Optional.absent();
45     }
46
47     private static DOMMountPoint convert(final org.opendaylight.mdsal.dom.api.DOMMountPoint from) {
48         return new DOMMountPoint() {
49             @Override
50             public YangInstanceIdentifier getIdentifier() {
51                 return from.getIdentifier();
52             }
53
54             @Override
55             public <T extends DOMService> Optional<T> getService(final Class<T> cls) {
56                 return from.getService(cls);
57             }
58
59             @Override
60             public SchemaContext getSchemaContext() {
61                 return from.getSchemaContext();
62             }
63
64             @Override
65             public int hashCode() {
66                 return from.getIdentifier().hashCode();
67             }
68
69             @Override
70             public boolean equals(final Object obj) {
71                 if (this == obj) {
72                     return true;
73                 }
74
75                 if (!(obj instanceof DOMMountPoint)) {
76                     return false;
77                 }
78
79                 DOMMountPoint other = (DOMMountPoint) obj;
80                 return from.getIdentifier().equals(other.getIdentifier());
81             }
82         };
83     }
84
85     @Override
86     public DOMMountPointBuilder createMountPoint(final YangInstanceIdentifier path) {
87         return new DOMMountPointBuilderImpl(path);
88     }
89
90     @Override
91     public ListenerRegistration<MountProvisionListener> registerProvisionListener(
92             final MountProvisionListener listener) {
93         final ListenerRegistration<DOMMountPointListener> delegateReg = delegate.registerProvisionListener(listener);
94         return new AbstractListenerRegistration<MountProvisionListener>(listener) {
95             @Override
96             protected void removeRegistration() {
97                 delegateReg.close();
98             }
99         };
100     }
101
102     @Override
103     public ListenerRegistration<DOMMountPointListener> registerProvisionListener(final DOMMountPointListener listener) {
104         return delegate.registerProvisionListener(listener);
105     }
106
107     /**
108      * Deprecated.
109
110      * @deprecated this method should never have been exposed publicly - registration should be done via the
111      *         public {@link #createMountPoint} interface. As such, this method expects the {@code mountPoint} param
112      *         to be of type {@link SimpleDOMMountPoint}.
113      */
114     @SuppressWarnings("unchecked")
115     @Deprecated
116     public ObjectRegistration<DOMMountPoint> registerMountPoint(final DOMMountPoint mountPoint) {
117         Preconditions.checkArgument(mountPoint instanceof SimpleDOMMountPoint, "Expected SimpleDOMMountPoint");
118
119         final org.opendaylight.mdsal.dom.api.DOMMountPointService.DOMMountPointBuilder delegateBuilder =
120             delegate.createMountPoint(mountPoint.getIdentifier());
121
122         if (mountPoint.getSchemaContext() != null) {
123             delegateBuilder.addInitialSchemaContext(mountPoint.getSchemaContext());
124         }
125
126         ((SimpleDOMMountPoint)mountPoint).getAllServices().forEach(
127             entry -> delegateBuilder.addService((Class<DOMService>)entry.getKey(), entry.getValue()));
128
129         final ObjectRegistration<org.opendaylight.mdsal.dom.api.DOMMountPoint> delegateReg = delegateBuilder.register();
130         return new ObjectRegistration<DOMMountPoint>() {
131             @Override
132             public void close() {
133                 delegateReg.close();
134             }
135
136             @Override
137             public DOMMountPoint getInstance() {
138                 return mountPoint;
139             }
140         };
141     }
142
143     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
144
145         ClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
146         private SimpleDOMMountPoint mountPoint;
147         private final YangInstanceIdentifier path;
148         private SchemaContext schemaContext;
149
150         public DOMMountPointBuilderImpl(final YangInstanceIdentifier path) {
151             this.path = path;
152         }
153
154         @Override
155         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
156             services.putInstance(type, impl);
157             return this;
158         }
159
160         @Override
161         public DOMMountPointBuilder addInitialSchemaContext(final SchemaContext ctx) {
162             schemaContext = ctx;
163             return this;
164         }
165
166         @Override
167         public ObjectRegistration<DOMMountPoint> register() {
168             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
169             mountPoint = SimpleDOMMountPoint.create(path, services, schemaContext);
170             return registerMountPoint(mountPoint);
171         }
172     }
173 }

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