Merge "Bug 1100 - Invoking an RPC with no input should not throw 500 when expected"
[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 java.util.Collection;
11 import java.util.Collections;
12
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 import com.google.common.base.Preconditions;
27
28 final class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
29
30     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
31
32     private final RemoteDeviceId id;
33     private final ExecutorService executor;
34     private volatile MountProvisionInstance mountInstance;
35     private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
36
37     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final ExecutorService executor) {
38         this.id = deviceId;
39         this.executor = executor;
40     }
41
42     public MountProvisionInstance getMountInstance() {
43         Preconditions.checkState(mountInstance != null,
44                 "%s: Sal provider was not initialized by sal. Cannot publish notification", id);
45         return mountInstance;
46     }
47
48     public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
49         Preconditions.checkState(datastoreAdapter != null,
50                 "%s: Sal provider %s was not initialized by sal. Cannot publish notification", id);
51         return datastoreAdapter;
52     }
53
54     @Override
55     public void onSessionInitiated(final Broker.ProviderSession session) {
56         final MountProvisionService mountService = session.getService(MountProvisionService.class);
57         if (mountService != null) {
58             mountInstance = mountService.createOrGetMountPoint(id.getPath());
59         }
60
61         logger.debug("{}: (BI)Session with sal established {}", id, session);
62     }
63
64     @Override
65     public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
66         return Collections.emptySet();
67     }
68
69     @Override
70     public Collection<? extends RpcService> getImplementations() {
71         return Collections.emptySet();
72     }
73
74     @Override
75     public Collection<? extends BindingAwareProvider.ProviderFunctionality> getFunctionality() {
76         return Collections.emptySet();
77     }
78
79     @Override
80     public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
81         final DataProviderService dataBroker = session.getSALService(DataProviderService.class);
82         datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker, executor);
83
84         logger.debug("{}: Session with sal established {}", id, session);
85     }
86
87     @Override
88     public void onSessionInitialized(final BindingAwareBroker.ConsumerContext session) {
89     }
90
91     public void close() throws Exception {
92         mountInstance = null;
93         datastoreAdapter.close();
94         datastoreAdapter = null;
95     }
96
97 }