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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.Transaction;
17 import org.opendaylight.mdsal.binding.api.TransactionChain;
18 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
19 import org.opendaylight.mdsal.dom.api.DOMActionService;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMNotification;
24 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
25 import org.opendaylight.mdsal.dom.api.DOMRpcService;
26 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
27 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
28 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.yangtools.concepts.ObjectRegistration;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class NetconfDeviceSalProvider implements AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
38 private final RemoteDeviceId id;
39 private final MountInstance mountInstance;
40 private final DataBroker dataBroker;
42 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
44 private TransactionChain txChain;
46 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
48 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
49 final Throwable cause) {
50 LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
52 resetTransactionChainForAdapaters();
53 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
57 public void onTransactionChainSuccessful(final TransactionChain chain) {
58 LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
62 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
63 this(deviceId, mountService, null);
66 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
67 final DataBroker dataBroker) {
69 mountInstance = new MountInstance(mountService, id);
70 this.dataBroker = dataBroker;
71 if (dataBroker != null) {
72 txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
73 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
77 public MountInstance getMountInstance() {
78 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
83 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
84 final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
85 checkState(local != null,
86 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
90 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
91 justification = "https://github.com/spotbugs/spotbugs/issues/811")
92 private void resetTransactionChainForAdapaters() {
93 txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
94 topologyDatastoreAdapter.setTxChain(txChain);
95 LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
100 mountInstance.close();
101 if (topologyDatastoreAdapter != null) {
102 topologyDatastoreAdapter.close();
104 topologyDatastoreAdapter = null;
105 if (txChain != null) {
110 public static class MountInstance implements AutoCloseable {
112 private final DOMMountPointService mountService;
113 private final RemoteDeviceId id;
115 private NetconfDeviceNotificationService notificationService;
116 private ObjectRegistration<DOMMountPoint> topologyRegistration;
118 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
119 this.mountService = requireNonNull(mountService);
120 this.id = requireNonNull(id);
123 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
124 final DOMDataBroker broker, final NetconfDataTreeService netconfService, final DOMRpcService rpc,
125 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
126 requireNonNull(mountService, "Closed");
127 checkState(topologyRegistration == null, "Already initialized");
129 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
130 mountService.createMountPoint(id.getTopologyPath());
131 mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
133 if (broker != null) {
134 mountBuilder.addService(DOMDataBroker.class, broker);
136 mountBuilder.addService(DOMRpcService.class, rpc);
137 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
138 if (deviceAction != null) {
139 mountBuilder.addService(DOMActionService.class, deviceAction);
141 if (netconfService != null) {
142 mountBuilder.addService(NetconfDataTreeService.class, netconfService);
144 this.notificationService = newNotificationService;
146 topologyRegistration = mountBuilder.register();
147 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
150 @SuppressWarnings("checkstyle:IllegalCatch")
151 public synchronized void onTopologyDeviceDisconnected() {
152 if (topologyRegistration == null) {
153 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
158 topologyRegistration.close();
159 } catch (final Exception e) {
160 // Only log and ignore
161 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
163 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
164 topologyRegistration = null;
169 public synchronized void close() {
170 onTopologyDeviceDisconnected();
173 public synchronized void publish(final DOMNotification domNotification) {
174 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
175 .publishNotification(domNotification);