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.netconf.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.yangtools.concepts.ObjectRegistration;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class NetconfDeviceSalProvider implements AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
35 private final RemoteDeviceId id;
36 private final MountInstance mountInstance;
37 private final DataBroker dataBroker;
39 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
41 private TransactionChain txChain;
43 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
45 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
46 final Throwable cause) {
47 LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
49 resetTransactionChainForAdapaters();
50 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
54 public void onTransactionChainSuccessful(final TransactionChain chain) {
55 LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
59 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
60 this(deviceId, mountService, null);
63 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
64 final DataBroker dataBroker) {
66 mountInstance = new MountInstance(mountService, id);
67 this.dataBroker = dataBroker;
68 if (dataBroker != null) {
69 txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
70 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
74 public MountInstance getMountInstance() {
75 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
80 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
81 final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
82 checkState(local != null,
83 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
87 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
88 justification = "https://github.com/spotbugs/spotbugs/issues/811")
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 void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
121 final DOMDataBroker broker, final DOMRpcService rpc,
122 final NetconfDeviceNotificationService newNotificationService) {
123 onTopologyDeviceConnected(initialCtx, broker, rpc, newNotificationService, null);
126 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
127 final DOMDataBroker broker, final DOMRpcService rpc,
128 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
129 requireNonNull(mountService, "Closed");
130 checkState(topologyRegistration == null, "Already initialized");
132 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
133 mountService.createMountPoint(id.getTopologyPath());
134 mountBuilder.addInitialSchemaContext(initialCtx);
136 mountBuilder.addService(DOMDataBroker.class, broker);
137 mountBuilder.addService(DOMRpcService.class, rpc);
138 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
139 if (deviceAction != null) {
140 mountBuilder.addService(DOMActionService.class, deviceAction);
142 this.notificationService = newNotificationService;
144 topologyRegistration = mountBuilder.register();
145 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
148 @SuppressWarnings("checkstyle:IllegalCatch")
149 public synchronized void onTopologyDeviceDisconnected() {
150 if (topologyRegistration == null) {
151 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
156 topologyRegistration.close();
157 } catch (final Exception e) {
158 // Only log and ignore
159 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
161 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
162 topologyRegistration = null;
167 public synchronized void close() {
168 onTopologyDeviceDisconnected();
171 public synchronized void publish(final DOMNotification domNotification) {
172 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
173 .publishNotification(domNotification);