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.dom.api.NetconfDataTreeService;
27 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
28 import org.opendaylight.yangtools.concepts.ObjectRegistration;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class NetconfDeviceSalProvider implements AutoCloseable {
34 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
36 private final RemoteDeviceId id;
37 private final MountInstance mountInstance;
38 private final DataBroker dataBroker;
40 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
42 private TransactionChain txChain;
44 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
46 public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
47 final Throwable cause) {
48 LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
50 resetTransactionChainForAdapaters();
51 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
55 public void onTransactionChainSuccessful(final TransactionChain chain) {
56 LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
60 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
61 this(deviceId, mountService, null);
64 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
65 final DataBroker dataBroker) {
67 mountInstance = new MountInstance(mountService, id);
68 this.dataBroker = dataBroker;
69 if (dataBroker != null) {
70 txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
71 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
75 public MountInstance getMountInstance() {
76 checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
81 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
82 final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
83 checkState(local != null,
84 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
88 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
89 justification = "https://github.com/spotbugs/spotbugs/issues/811")
90 private void resetTransactionChainForAdapaters() {
91 txChain = requireNonNull(dataBroker).createTransactionChain(transactionChainListener);
92 topologyDatastoreAdapter.setTxChain(txChain);
93 LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
98 mountInstance.close();
99 if (topologyDatastoreAdapter != null) {
100 topologyDatastoreAdapter.close();
102 topologyDatastoreAdapter = null;
103 if (txChain != null) {
108 public static class MountInstance implements AutoCloseable {
110 private final DOMMountPointService mountService;
111 private final RemoteDeviceId id;
113 private NetconfDeviceNotificationService notificationService;
114 private ObjectRegistration<DOMMountPoint> topologyRegistration;
116 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
117 this.mountService = requireNonNull(mountService);
118 this.id = requireNonNull(id);
121 public void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
122 final DOMDataBroker broker, final DOMRpcService rpc,
123 final NetconfDeviceNotificationService newNotificationService) {
124 onTopologyDeviceConnected(initialCtx, broker, null, rpc, newNotificationService, null);
127 public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
128 final DOMDataBroker broker, final NetconfDataTreeService netconfService, final DOMRpcService rpc,
129 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
130 requireNonNull(mountService, "Closed");
131 checkState(topologyRegistration == null, "Already initialized");
133 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
134 mountService.createMountPoint(id.getTopologyPath());
135 mountBuilder.addInitialSchemaContext(initialCtx);
137 if (broker != null) {
138 mountBuilder.addService(DOMDataBroker.class, broker);
140 mountBuilder.addService(DOMRpcService.class, rpc);
141 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
142 if (deviceAction != null) {
143 mountBuilder.addService(DOMActionService.class, deviceAction);
145 if (netconfService != null) {
146 mountBuilder.addService(NetconfDataTreeService.class, netconfService);
148 this.notificationService = newNotificationService;
150 topologyRegistration = mountBuilder.register();
151 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
154 @SuppressWarnings("checkstyle:IllegalCatch")
155 public synchronized void onTopologyDeviceDisconnected() {
156 if (topologyRegistration == null) {
157 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
162 topologyRegistration.close();
163 } catch (final Exception e) {
164 // Only log and ignore
165 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
167 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
168 topologyRegistration = null;
173 public synchronized void close() {
174 onTopologyDeviceDisconnected();
177 public synchronized void publish(final DOMNotification domNotification) {
178 checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
179 .publishNotification(domNotification);