Merge "Use mounted device schema context when resolving mounted device rpc path"
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalFacade.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.netconf.sal.connect.netconf.sal;
9
10 import com.google.common.collect.Lists;
11 import java.util.List;
12 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
13 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
14 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.controller.sal.core.api.Broker;
17 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
18 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
19 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
20 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
26
27     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
28
29     private final RemoteDeviceId id;
30     private final NetconfDeviceSalProvider salProvider;
31
32     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
33
34     public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker) {
35         this.id = id;
36         this.salProvider = new NetconfDeviceSalProvider(id);
37         registerToSal(domBroker, bindingBroker);
38     }
39
40     public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker) {
41         domRegistryDependency.registerProvider(salProvider);
42         bindingBroker.registerProvider(salProvider);
43     }
44
45     @Override
46     public synchronized void onNotification(final DOMNotification domNotification) {
47         salProvider.getMountInstance().publish(domNotification);
48     }
49
50     @Override
51     public synchronized void onDeviceConnected(final SchemaContext schemaContext,
52                                                final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
53
54         final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
55
56         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
57
58         salProvider.getMountInstance().onTopologyDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
59         salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
60     }
61
62     @Override
63     public synchronized void onDeviceDisconnected() {
64         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
65         salProvider.getMountInstance().onTopologyDeviceDisconnected();
66     }
67
68     @Override
69     public synchronized void onDeviceFailed(final Throwable throwable) {
70         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
71         salProvider.getMountInstance().onTopologyDeviceDisconnected();
72     }
73
74     @Override
75     public synchronized void close() {
76         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
77             closeGracefully(reg);
78         }
79         closeGracefully(salProvider);
80     }
81
82     private void closeGracefully(final AutoCloseable resource) {
83         if (resource != null) {
84             try {
85                 resource.close();
86             } catch (final Exception e) {
87                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
88             }
89         }
90     }
91
92 }