Bug 4940 - correctly implement default-request-timeout-millis
[netconf.git] / opendaylight / 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.Collections;
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.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
28
29     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
30
31     private final RemoteDeviceId id;
32     private final NetconfDeviceSalProvider salProvider;
33
34     private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
35
36     public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker) {
37         this.id = id;
38         this.salProvider = new NetconfDeviceSalProvider(id);
39         registerToSal(domBroker, bindingBroker);
40     }
41
42     public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker) {
43         domRegistryDependency.registerProvider(salProvider);
44         bindingBroker.registerProvider(salProvider);
45     }
46
47     @Override
48     public synchronized void onNotification(final DOMNotification domNotification) {
49         salProvider.getMountInstance().publish(domNotification);
50     }
51
52     @Override
53     public synchronized void onDeviceConnected(final SchemaContext schemaContext,
54                                                final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
55
56         final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
57
58         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
59
60         salProvider.getMountInstance().onDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
61         salProvider.getDatastoreAdapter().updateDeviceState(true, netconfSessionPreferences.getModuleBasedCaps());
62         salProvider.getMountInstance().onTopologyDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
63         salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
64     }
65
66     @Override
67     public synchronized void onDeviceDisconnected() {
68         salProvider.getDatastoreAdapter().updateDeviceState(false, Collections.<QName>emptySet());
69         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
70         salProvider.getMountInstance().onDeviceDisconnected();
71         salProvider.getMountInstance().onTopologyDeviceDisconnected();
72     }
73
74     @Override
75     public synchronized void onDeviceFailed(final Throwable throwable) {
76         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
77         salProvider.getMountInstance().onDeviceDisconnected();
78         salProvider.getMountInstance().onTopologyDeviceDisconnected();
79     }
80
81     @Override
82     public synchronized void close() {
83         for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
84             closeGracefully(reg);
85         }
86         closeGracefully(salProvider);
87     }
88
89     private void closeGracefully(final AutoCloseable resource) {
90         if (resource != null) {
91             try {
92                 resource.close();
93             } catch (final Exception e) {
94                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
95             }
96         }
97     }
98
99 }