4966348d186d6cc82722ef1274542cdc15c997a8
[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(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 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(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(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     /**
103      * Deprecated.
104
105      * @deprecated this method should never have been exposed publicly - registration should be done via the
106      *         public {@link #createMountPoint} interface. As such, this method expects the {@code mountPoint} param
107      *         to be of type {@link SimpleDOMMountPoint}.
108      */
109     @SuppressWarnings("unchecked")
110     @Deprecated
111     public ObjectRegistration<DOMMountPoint> registerMountPoint(final DOMMountPoint mountPoint) {
112         Preconditions.checkArgument(mountPoint instanceof SimpleDOMMountPoint, "Expected SimpleDOMMountPoint");
113
114         final org.opendaylight.mdsal.dom.api.DOMMountPointService.DOMMountPointBuilder delegateBuilder =
115             delegate.createMountPoint(mountPoint.getIdentifier());
116
117         if (mountPoint.getSchemaContext() != null) {
118             delegateBuilder.addInitialSchemaContext(mountPoint.getSchemaContext());
119         }
120
121         ((SimpleDOMMountPoint)mountPoint).getAllServices().forEach(
122             entry -> delegateBuilder.addService((Class<DOMService>)entry.getKey(), entry.getValue()));
123
124         final ObjectRegistration<org.opendaylight.mdsal.dom.api.DOMMountPoint> delegateReg = delegateBuilder.register();
125         return new ObjectRegistration<DOMMountPoint>() {
126             @Override
127             public void close() {
128                 delegateReg.close();
129             }
130
131             @Override
132             public DOMMountPoint getInstance() {
133                 return mountPoint;
134             }
135         };
136     }
137
138     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
139
140         ClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
141         private SimpleDOMMountPoint mountPoint;
142         private final YangInstanceIdentifier path;
143         private SchemaContext schemaContext;
144
145         public DOMMountPointBuilderImpl(final YangInstanceIdentifier path) {
146             this.path = path;
147         }
148
149         @Override
150         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
151             services.putInstance(type, impl);
152             return this;
153         }
154
155         @Override
156         public DOMMountPointBuilder addInitialSchemaContext(final SchemaContext ctx) {
157             schemaContext = ctx;
158             return this;
159         }
160
161         @Override
162         public ObjectRegistration<DOMMountPoint> register() {
163             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
164             mountPoint = SimpleDOMMountPoint.create(path, services, schemaContext);
165             return registerMountPoint(mountPoint);
166         }
167     }
168 }

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