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