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.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
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;
26 public final class NetconfDeviceSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
28 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalFacade.class);
30 private final RemoteDeviceId id;
31 private final NetconfDeviceSalProvider salProvider;
32 private final List<AutoCloseable> salRegistrations = Lists.newArrayList();
34 public NetconfDeviceSalFacade(final RemoteDeviceId id, final DOMMountPointService mountPointService,
35 final DataBroker dataBroker) {
37 this.salProvider = new NetconfDeviceSalProvider(id, mountPointService, dataBroker);
41 NetconfDeviceSalFacade(final RemoteDeviceId id, final NetconfDeviceSalProvider salProvider) {
43 this.salProvider = salProvider;
47 public synchronized void onNotification(final DOMNotification domNotification) {
48 salProvider.getMountInstance().publish(domNotification);
52 public synchronized void onDeviceConnected(final SchemaContext schemaContext,
53 final NetconfSessionPreferences netconfSessionPreferences,
54 final DOMRpcService deviceRpc) {
56 final DOMDataBroker domBroker =
57 new NetconfDeviceDataBroker(id, schemaContext, deviceRpc, netconfSessionPreferences);
59 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
61 salProvider.getMountInstance()
62 .onTopologyDeviceConnected(schemaContext, domBroker, deviceRpc, notificationService);
63 salProvider.getTopologyDatastoreAdapter()
64 .updateDeviceData(true, netconfSessionPreferences.getNetconfDeviceCapabilities());
68 public synchronized void onDeviceDisconnected() {
69 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
70 salProvider.getMountInstance().onTopologyDeviceDisconnected();
74 public synchronized void onDeviceFailed(final Throwable throwable) {
75 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
76 salProvider.getMountInstance().onTopologyDeviceDisconnected();
80 public synchronized void close() {
81 for (final AutoCloseable reg : Lists.reverse(salRegistrations)) {
84 closeGracefully(salProvider);
87 @SuppressWarnings("checkstyle:IllegalCatch")
88 private void closeGracefully(final AutoCloseable resource) {
89 if (resource != null) {
92 } catch (final Exception e) {
93 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);