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.annotations.VisibleForTesting;
11 import com.google.common.collect.Lists;
12 import java.util.List;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.dom.api.DOMActionService;
15 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
16 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.mdsal.dom.api.DOMRpcService;
19 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
20 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
21 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
29 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
31 private final RemoteDeviceId id;
32 private final NetconfDeviceSalProvider salProvider;
33 private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
35 public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
36 final DataBroker dataBroker) {
38 this.salProvider = new NetconfDeviceSalProvider(id, mountPointService, dataBroker);
42 NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider) {
44 this.salProvider = salProvider;
48 public synchronized void onNotification(final DOMNotification domNotification) {
49 salProvider.getMountInstance().publish(domNotification);
53 public synchronized void onDeviceConnected(final SchemaContext schemaContext,
54 final NetconfSessionPreferences netconfSessionPreferences,
55 final DOMRpcService deviceRpc, final DOMActionService deviceAction) {
57 final DOMDataBroker domBroker =
58 new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
60 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
62 salProvider.getMountInstance()
63 .onTopologyDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService, deviceAction);
64 salProvider.getTopologyDatastoreAdapter()
65 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
69 public synchronized void onDeviceDisconnected() {
70 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
71 salProvider.getMountInstance().onTopologyDeviceDisconnected();
75 public synchronized void onDeviceFailed(final Throwable throwable) {
76 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
77 salProvider.getMountInstance().onTopologyDeviceDisconnected();
81 public synchronized void close() {
82 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
85 closeGracefully(salProvider);
88 @SuppressWarnings("checkstyle:IllegalCatch")
89 private void closeGracefully(final AutoCloseable resource) {
90 if (resource != null) {
93 } catch (final Exception e) {
94 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);