49a499709e08c8cdbbd137b45976133c0085b4d1
[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 java.util.HashMap;
12 import java.util.Map;
13
14 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMService;
17 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
18 import org.opendaylight.yangtools.concepts.ObjectRegistration;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21
22 import com.google.common.base.Optional;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.ClassToInstanceMap;
25 import com.google.common.collect.MutableClassToInstanceMap;
26
27 public class DOMMountPointServiceImpl implements DOMMountPointService {
28
29     private final Map<InstanceIdentifier, SimpleDOMMountPoint> mountPoints = new HashMap<>();
30
31     @Override
32     public Optional<DOMMountPoint> getMountPoint(final InstanceIdentifier path) {
33         return Optional.<DOMMountPoint>fromNullable(mountPoints.get(path));
34     }
35
36     @Override
37     public DOMMountPointBuilder createMountPoint(final InstanceIdentifier path) {
38         Preconditions.checkState(!mountPoints.containsKey(path), "Mount point already exists");
39         return new DOMMountPointBuilderImpl(path);
40     }
41
42     public ObjectRegistration<DOMMountPoint> registerMountPoint(final SimpleDOMMountPoint mountPoint) {
43         synchronized (mountPoints) {
44             Preconditions.checkState(!mountPoints.containsKey(mountPoint.getIdentifier()), "Mount point already exists");
45             mountPoints.put(mountPoint.getIdentifier(), mountPoint);
46         }
47         return null;
48     }
49
50     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
51
52         ClassToInstanceMap<DOMService> services = MutableClassToInstanceMap.create();
53         private SimpleDOMMountPoint mountPoint;
54         private final InstanceIdentifier path;
55         private SchemaContext schemaContext;
56
57         public DOMMountPointBuilderImpl(final InstanceIdentifier path) {
58             this.path = path;
59         }
60
61         @Override
62         public <T extends DOMService> DOMMountPointBuilder addService(final Class<T> type, final T impl) {
63             services.putInstance(type, impl);
64             return this;
65         }
66
67         @Override
68         public DOMMountPointBuilder addInitialSchemaContext(final SchemaContext ctx) {
69             schemaContext = ctx;
70             return this;
71         }
72
73         @Override
74         public ObjectRegistration<DOMMountPoint> register() {
75             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
76             mountPoint = SimpleDOMMountPoint.create(path, services,schemaContext);
77             return registerMountPoint(mountPoint);
78         }
79     }
80 }

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