2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
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;
25 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
27 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
29 private final RemoteDeviceId id;
30 private final NetconfDeviceSalProvider salProvider;
32 private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
34 public NetconfDeviceSalFacade(final RemoteDeviceId id, final Broker domBroker, final BindingAwareBroker bindingBroker) {
36 this.salProvider = new NetconfDeviceSalProvider(id);
37 registerToSal(domBroker, bindingBroker);
40 public void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker) {
41 domRegistryDependency.registerProvider(salProvider);
42 bindingBroker.registerProvider(salProvider);
46 public synchronized void onNotification(final DOMNotification domNotification) {
47 salProvider.getMountInstance().publish(domNotification);
51 public synchronized void onDeviceConnected(final SchemaContext schemaContext,
52 final NetconfSessionPreferences netconfSessionPreferences, final DOMRpcService deviceRpc) {
54 final DOMDataBroker domBroker = new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
56 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
58 salProvider.getMountInstance().onTopologyDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
59 salProvider.getTopologyDatastoreAdapter().updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
63 public synchronized void onDeviceDisconnected() {
64 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
65 salProvider.getMountInstance().onTopologyDeviceDisconnected();
69 public synchronized void onDeviceFailed(final Throwable throwable) {
70 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
71 salProvider.getMountInstance().onTopologyDeviceDisconnected();
75 public synchronized void close() {
76 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
79 closeGracefully(salProvider);
82 private void closeGracefully(final AutoCloseable resource) {
83 if (resource != null) {
86 } catch (final Exception e) {
87 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);