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 static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.binding.api.Transaction;
16 import org.opendaylight.mdsal.binding.api.TransactionChain;
17 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
18 import org.opendaylight.mdsal.dom.api.DOMActionService;
19 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
20 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.mdsal.dom.api.DOMNotification;
23 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
24 import org.opendaylight.mdsal.dom.api.DOMRpcService;
25 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
26 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
27 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.yangtools.concepts.ObjectRegistration;
30 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class NetconfDeviceSalProvider implements AutoCloseable {
35 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
37 private final RemoteDeviceId id;
38 private final MountInstance mountInstance;
39 private final DataBroker dataBroker;
41 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
43 private TransactionChain txChain;
45 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
47 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
48 final Throwable cause) {
49 LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
51 resetTransactionChainForAdapaters();
52 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
56 public void onTransactionChainSuccessful(final TransactionChain chain) {
57 LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
61 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
62 this(deviceId, mountService, null);
65 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
66 final DataBroker dataBroker) {
68 mountInstance = new MountInstance(mountService, id);
69 this.dataBroker = dataBroker;
70 if (dataBroker != null) {
71 txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
72 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
76 public MountInstance getMountInstance() {
77 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
82 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
83 final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
84 checkState(local != null,
85 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
89 private void resetTransactionChainForAdapaters() {
90 txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
91 topologyDatastoreAdapter.setTxChain(txChain);
92 LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
97 mountInstance.close();
98 if (topologyDatastoreAdapter != null) {
99 topologyDatastoreAdapter.close();
101 topologyDatastoreAdapter = null;
102 if (txChain != null) {
107 public static class MountInstance implements AutoCloseable {
109 private final DOMMountPointService mountService;
110 private final RemoteDeviceId id;
112 private NetconfDeviceNotificationService notificationService;
113 private ObjectRegistration<DOMMountPoint> topologyRegistration;
115 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
116 this.mountService = requireNonNull(mountService);
117 this.id = requireNonNull(id);
120 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
121 final DOMDataBroker broker, final NetconfDataTreeService netconfService, final DOMRpcService rpc,
122 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
123 requireNonNull(mountService, "Closed");
124 checkState(topologyRegistration == null, "Already initialized");
126 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
127 mountService.createMountPoint(id.getTopologyPath());
128 mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
130 if (broker != null) {
131 mountBuilder.addService(DOMDataBroker.class, broker);
133 mountBuilder.addService(DOMRpcService.class, rpc);
134 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
135 if (deviceAction != null) {
136 mountBuilder.addService(DOMActionService.class, deviceAction);
138 if (netconfService != null) {
139 mountBuilder.addService(NetconfDataTreeService.class, netconfService);
141 this.notificationService = newNotificationService;
143 topologyRegistration = mountBuilder.register();
144 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
147 @SuppressWarnings("checkstyle:IllegalCatch")
148 public synchronized void onTopologyDeviceDisconnected() {
149 if (topologyRegistration == null) {
150 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
155 topologyRegistration.close();
156 } catch (final Exception e) {
157 // Only log and ignore
158 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
160 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
161 topologyRegistration = null;
166 public synchronized void close() {
167 onTopologyDeviceDisconnected();
170 public synchronized void publish(final DOMNotification domNotification) {
171 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
172 .publishNotification(domNotification);