Bug 6696: Add transaction chain support to netconf connector
[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.dom.api.DOMDataBroker;
14 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
17 import org.opendaylight.controller.sal.core.api.Broker;
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
33     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
34
35     public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker) {
36         this.id = id;
37         this.salProvider = new NetconfDeviceSalProvider(id);
38         registerToSal(domBroker, bindingBroker);
39     }
40
41     @VisibleForTesting
42     NetconfDeviceSalFacade(final RemoteDeviceId id, NetconfDeviceSalProvider salProvider,
43                            final Broker domBroker, final BindingAwareBroker bindingBroker) {
44         this.id = id;
45         this.salProvider = salProvider;
46         registerToSal(domBroker, bindingBroker);
47     }
48
49     public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker) {
50         domRegistryDependency.registerProvider(salProvider);
51         bindingBroker.registerProvider(salProvider);
52     }
53
54     @Override
55     public synchronized void onNotification(final DOMNotification domNotification) {
56         salProvider.getMountInstance().publish(domNotification);
57     }
58
59     @Override
60     public synchronized void onDeviceConnected(final SchemaContext schemaContext,
61                                                final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
62
63         final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
64
65         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
66
67         salProvider.getMountInstance().onTopologyDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
68         salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
69     }
70
71     @Override
72     public synchronized void onDeviceDisconnected() {
73         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
74         salProvider.getMountInstance().onTopologyDeviceDisconnected();
75     }
76
77     @Override
78     public synchronized void onDeviceFailed(final Throwable throwable) {
79         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
80         salProvider.getMountInstance().onTopologyDeviceDisconnected();
81     }
82
83     @Override
84     public synchronized void close() {
85         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
86             closeGracefully(reg);
87         }
88         closeGracefully(salProvider);
89     }
90
91     private void closeGracefully(final AutoCloseable resource) {
92         if (resource != null) {
93             try {
94                 resource.close();
95             } catch (final Exception e) {
96                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
97             }
98         }
99     }
100
101 }