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.base.Preconditions;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.Transaction;
13 import org.opendaylight.mdsal.binding.api.TransactionChain;
14 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
15 import org.opendaylight.mdsal.dom.api.DOMActionService;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
21 import org.opendaylight.mdsal.dom.api.DOMRpcService;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.concepts.ObjectRegistration;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class NetconfDeviceSalProvider implements AutoCloseable {
30 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
32 private final RemoteDeviceId id;
33 private final MountInstance mountInstance;
34 private final DataBroker dataBroker;
36 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
38 private TransactionChain txChain;
40 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
42 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
43 final Throwable cause) {
44 LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
46 resetTransactionChainForAdapaters();
47 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
51 public void onTransactionChainSuccessful(final TransactionChain chain) {
52 LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
56 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
57 this(deviceId, mountService, null);
60 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
61 final DataBroker dataBroker) {
63 mountInstance = new MountInstance(mountService, id);
64 this.dataBroker = dataBroker;
65 if (dataBroker != null) {
66 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
67 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
71 public MountInstance getMountInstance() {
72 Preconditions.checkState(mountInstance != null,
73 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
77 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
78 Preconditions.checkState(topologyDatastoreAdapter != null,
79 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
80 return topologyDatastoreAdapter;
83 private void resetTransactionChainForAdapaters() {
84 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
86 topologyDatastoreAdapter.setTxChain(txChain);
88 LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
94 mountInstance.close();
95 if (topologyDatastoreAdapter != null) {
96 topologyDatastoreAdapter.close();
98 topologyDatastoreAdapter = null;
99 if (txChain != null) {
104 public static class MountInstance implements AutoCloseable {
106 private final DOMMountPointService mountService;
107 private final RemoteDeviceId id;
109 private NetconfDeviceNotificationService notificationService;
110 private ObjectRegistration<DOMMountPoint> topologyRegistration;
112 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
113 this.mountService = Preconditions.checkNotNull(mountService);
114 this.id = Preconditions.checkNotNull(id);
117 public void onTopologyDeviceConnected(final SchemaContext initialCtx,
118 final DOMDataBroker broker, final DOMRpcService rpc,
119 final NetconfDeviceNotificationService newNotificationService) {
120 onTopologyDeviceConnected(initialCtx, broker, rpc, newNotificationService, null);
123 public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
124 final DOMDataBroker broker, final DOMRpcService rpc,
125 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
126 Preconditions.checkNotNull(mountService, "Closed");
127 Preconditions.checkState(topologyRegistration == null, "Already initialized");
129 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
130 mountService.createMountPoint(id.getTopologyPath());
131 mountBuilder.addInitialSchemaContext(initialCtx);
133 mountBuilder.addService(DOMDataBroker.class, broker);
134 mountBuilder.addService(DOMRpcService.class, rpc);
135 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
136 if (deviceAction != null) {
137 mountBuilder.addService(DOMActionService.class, deviceAction);
139 this.notificationService = newNotificationService;
141 topologyRegistration = mountBuilder.register();
142 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
145 @SuppressWarnings("checkstyle:IllegalCatch")
146 public synchronized void onTopologyDeviceDisconnected() {
147 if (topologyRegistration == null) {
148 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
153 topologyRegistration.close();
154 } catch (final Exception e) {
155 // Only log and ignore
156 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
158 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
159 topologyRegistration = null;
164 public synchronized void close() {
165 onTopologyDeviceDisconnected();
168 public synchronized void publish(final DOMNotification domNotification) {
169 Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}",
171 notificationService.publishNotification(domNotification);