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.controller.md.sal.binding.api.BindingTransactionChain;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
16 import org.opendaylight.controller.md.sal.dom.api.DOMActionService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.yangtools.concepts.ObjectRegistration;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NetconfDeviceSalProvider implements AutoCloseable {
31 private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
33 private final RemoteDeviceId id;
34 private final MountInstance mountInstance;
35 private final DataBroker dataBroker;
37 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
39 private BindingTransactionChain txChain;
41 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
43 public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
44 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
45 LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
47 resetTransactionChainForAdapaters();
48 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
52 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
53 LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
57 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
58 this(deviceId, mountService, null);
61 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
62 final DataBroker dataBroker) {
64 mountInstance = new MountInstance(mountService, id);
65 this.dataBroker = dataBroker;
66 if (dataBroker != null) {
67 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
68 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
72 public MountInstance getMountInstance() {
73 Preconditions.checkState(mountInstance != null,
74 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
78 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
79 Preconditions.checkState(topologyDatastoreAdapter != null,
80 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
81 return topologyDatastoreAdapter;
84 private void resetTransactionChainForAdapaters() {
85 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
87 topologyDatastoreAdapter.setTxChain(txChain);
89 LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
95 mountInstance.close();
96 if (topologyDatastoreAdapter != null) {
97 topologyDatastoreAdapter.close();
99 topologyDatastoreAdapter = null;
100 if (txChain != null) {
105 public static class MountInstance implements AutoCloseable {
107 private final DOMMountPointService mountService;
108 private final RemoteDeviceId id;
110 private NetconfDeviceNotificationService notificationService;
111 private ObjectRegistration<DOMMountPoint> topologyRegistration;
113 MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
114 this.mountService = Preconditions.checkNotNull(mountService);
115 this.id = Preconditions.checkNotNull(id);
118 public void onTopologyDeviceConnected(final SchemaContext initialCtx,
119 final DOMDataBroker broker, final DOMRpcService rpc,
120 final NetconfDeviceNotificationService newNotificationService) {
121 onTopologyDeviceConnected(initialCtx, broker, rpc, newNotificationService, null);
124 public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
125 final DOMDataBroker broker, final DOMRpcService rpc,
126 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
127 Preconditions.checkNotNull(mountService, "Closed");
128 Preconditions.checkState(topologyRegistration == null, "Already initialized");
130 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
131 mountService.createMountPoint(id.getTopologyPath());
132 mountBuilder.addInitialSchemaContext(initialCtx);
134 mountBuilder.addService(DOMDataBroker.class, broker);
135 mountBuilder.addService(DOMRpcService.class, rpc);
136 mountBuilder.addService(DOMNotificationService.class, newNotificationService);
137 if (deviceAction != null) {
138 mountBuilder.addService(DOMActionService.class, deviceAction);
140 this.notificationService = newNotificationService;
142 topologyRegistration = mountBuilder.register();
143 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
146 @SuppressWarnings("checkstyle:IllegalCatch")
147 public synchronized void onTopologyDeviceDisconnected() {
148 if (topologyRegistration == null) {
149 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
154 topologyRegistration.close();
155 } catch (final Exception e) {
156 // Only log and ignore
157 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
159 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
160 topologyRegistration = null;
165 public synchronized void close() {
166 onTopologyDeviceDisconnected();
169 public synchronized void publish(final DOMNotification domNotification) {
170 Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}",
172 notificationService.publishNotification(domNotification);