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.DOMDataBroker;
17 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
22 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.yangtools.concepts.ObjectRegistration;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class NetconfDeviceSalProvider implements AutoCloseable {
30 private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
32 private final RemoteDeviceId id;
33 private final MountInstance mountInstance;
34 private final DataBroker dataBroker;
36 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
38 private BindingTransactionChain txChain;
40 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
42 public void onTransactionChainFailed(final TransactionChain<?, ?> chain, final AsyncTransaction<?, ?> transaction, final Throwable cause) {
43 logger.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
45 resetTransactionChainForAdapaters();
46 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
50 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
51 logger.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
55 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
56 final DataBroker dataBroker) {
58 mountInstance = new MountInstance(mountService, id);
59 this.dataBroker = dataBroker;
60 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
62 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
65 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
67 mountInstance = new MountInstance(mountService, id);
68 this.dataBroker = null;
71 public MountInstance getMountInstance() {
72 Preconditions.checkState(mountInstance != null,
73 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
77 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
78 Preconditions.checkState(topologyDatastoreAdapter != null,
79 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
80 return topologyDatastoreAdapter;
83 private void resetTransactionChainForAdapaters() {
84 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
86 topologyDatastoreAdapter.setTxChain(txChain);
88 logger.trace("{}: Resetting TransactionChain {}", id, txChain);
92 public void close() throws Exception {
93 mountInstance.close();
94 if (topologyDatastoreAdapter != null) {
95 topologyDatastoreAdapter.close();
97 topologyDatastoreAdapter = null;
98 if (txChain != null) {
103 public static final class MountInstance implements AutoCloseable {
105 private final DOMMountPointService mountService;
106 private final RemoteDeviceId id;
107 private NetconfDeviceNotificationService notificationService;
109 private ObjectRegistration<DOMMountPoint> topologyRegistration;
111 public MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
112 this.mountService = Preconditions.checkNotNull(mountService);
113 this.id = Preconditions.checkNotNull(id);
116 public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
117 final DOMDataBroker broker, final DOMRpcService rpc,
118 final NetconfDeviceNotificationService notificationService) {
119 Preconditions.checkNotNull(mountService, "Closed");
120 Preconditions.checkState(topologyRegistration == null, "Already initialized");
122 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
123 mountBuilder.addInitialSchemaContext(initialCtx);
125 mountBuilder.addService(DOMDataBroker.class, broker);
126 mountBuilder.addService(DOMRpcService.class, rpc);
127 mountBuilder.addService(DOMNotificationService.class, notificationService);
128 this.notificationService = notificationService;
130 topologyRegistration = mountBuilder.register();
131 logger.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
135 public synchronized void onTopologyDeviceDisconnected() {
136 if(topologyRegistration == null) {
137 logger.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
142 topologyRegistration.close();
143 } catch (final Exception e) {
144 // Only log and ignore
145 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
147 logger.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
148 topologyRegistration = null;
153 public synchronized void close() throws Exception {
154 onTopologyDeviceDisconnected();
157 public synchronized void publish(final DOMNotification domNotification) {
158 Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
159 notificationService.publishNotification(domNotification);