27a4a78b73899361e7d6086069a0848251491e5d
[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 package org.opendaylight.controller.md.sal.dom.broker.impl.mount;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ClassToInstanceMap;
14 import com.google.common.collect.MutableClassToInstanceMap;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMService;
18 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
19 import org.opendaylight.controller.sal.core.compat.DOMMountPointAdapter;
20 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.concepts.ObjectRegistration;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 public class DOMMountPointServiceImpl implements DOMMountPointService {
27     private final org.opendaylight.mdsal.dom.api.DOMMountPointService delegate;
28
29     @VisibleForTesting
30     public DOMMountPointServiceImpl() {
31         this(new org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl());
32     }
33
34     public DOMMountPointServiceImpl(final org.opendaylight.mdsal.dom.api.DOMMountPointService delegate) {
35         this.delegate = delegate;
36     }
37
38     @Override
39     public Optional<DOMMountPoint> getMountPoint(final YangInstanceIdentifier path) {
40         return Optional.fromJavaUtil(delegate.getMountPoint(path).map(DOMMountPointAdapter::new));
41     }
42
43     @Override
44     public DOMMountPointBuilder createMountPoint(final YangInstanceIdentifier path) {
45         return new DOMMountPointBuilderImpl(path);
46     }
47
48     @Override
49     public ListenerRegistration<DOMMountPointListener> registerProvisionListener(final DOMMountPointListener listener) {
50         return delegate.registerProvisionListener(listener);
51     }
52
53     @SuppressWarnings("unchecked")
54     ObjectRegistration<DOMMountPoint> registerMountPoint(final SimpleDOMMountPoint mountPoint) {
55         final org.opendaylight.mdsal.dom.api.DOMMountPointService.DOMMountPointBuilder delegateBuilder =
56                 delegate.createMountPoint(mountPoint.getIdentifier());
57
58         if (mountPoint.getSchemaContext() != null) {
59             delegateBuilder.addInitialSchemaContext(mountPoint.getSchemaContext());
60         }
61
62         mountPoint.getAllServices().forEach(
63             entry -> delegateBuilder.addService((Class<DOMService>)entry.getKey(), entry.getValue()));
64
65         final ObjectRegistration<org.opendaylight.mdsal.dom.api.DOMMountPoint> delegateReg = delegateBuilder.register();
66         return new ObjectRegistration<DOMMountPoint>() {
67             @Override
68             public void close() {
69                 delegateReg.close();
70             }
71
72             @Override
73             public DOMMountPoint getInstance() {
74                 return mountPoint;
75             }
76         };
77     }
78
79     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
80
81         ClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
82         private SimpleDOMMountPoint mountPoint;
83         private final YangInstanceIdentifier path;
84         private SchemaContext schemaContext;
85
86         public DOMMountPointBuilderImpl(final YangInstanceIdentifier path) {
87             this.path = path;
88         }
89
90         @Override
91         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
92             services.putInstance(type, impl);
93             return this;
94         }
95
96         @Override
97         public DOMMountPointBuilder addInitialSchemaContext(final SchemaContext ctx) {
98             schemaContext = ctx;
99             return this;
100         }
101
102         @Override
103         public ObjectRegistration<DOMMountPoint> register() {
104             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
105             mountPoint = SimpleDOMMountPoint.create(path, services, schemaContext);
106             return registerMountPoint(mountPoint);
107         }
108     }
109 }

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