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