Implementation of ModuleShardStrategy
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / sal / NetconfDeviceSalProvider.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.sal.connect.netconf.sal;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.concurrent.ExecutorService;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
16 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
17 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
18 import org.opendaylight.controller.sal.core.api.Broker;
19 import org.opendaylight.controller.sal.core.api.Provider;
20 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
21 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
22 import org.opendaylight.yangtools.yang.binding.RpcService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 final class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
27
28     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
29
30     private final RemoteDeviceId id;
31     private final ExecutorService executor;
32     private volatile MountProvisionInstance mountInstance;
33     private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
34
35     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final ExecutorService executor) {
36         this.id = deviceId;
37         this.executor = executor;
38     }
39
40     public MountProvisionInstance getMountInstance() {
41         Preconditions.checkState(mountInstance != null,
42                 "%s: Sal provider was not initialized by sal. Cannot get mount instance", id);
43         return mountInstance;
44     }
45
46     public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
47         Preconditions.checkState(datastoreAdapter != null,
48                 "%s: Sal provider %s was not initialized by sal. Cannot get datastore adapter", id);
49         return datastoreAdapter;
50     }
51
52     @Override
53     public void onSessionInitiated(final Broker.ProviderSession session) {
54         final MountProvisionService mountService = session.getService(MountProvisionService.class);
55         if (mountService != null) {
56             mountInstance = mountService.createOrGetMountPoint(id.getPath());
57         }
58
59         logger.debug("{}: (BI)Session with sal established {}", id, session);
60     }
61
62     @Override
63     public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
64         return Collections.emptySet();
65     }
66
67     @Override
68     public Collection<? extends RpcService> getImplementations() {
69         return Collections.emptySet();
70     }
71
72     @Override
73     public Collection<? extends BindingAwareProvider.ProviderFunctionality> getFunctionality() {
74         return Collections.emptySet();
75     }
76
77     @Override
78     public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
79         final DataProviderService dataBroker = session.getSALService(DataProviderService.class);
80         datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker, executor);
81
82         logger.debug("{}: Session with sal established {}", id, session);
83     }
84
85     @Override
86     public void onSessionInitialized(final BindingAwareBroker.ConsumerContext session) {
87     }
88
89     public void close() throws Exception {
90         mountInstance = null;
91         datastoreAdapter.close();
92         datastoreAdapter = null;
93     }
94
95 }