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 LOG = 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,
43 final AsyncTransaction<?, ?> transaction, final Throwable cause) {
44 LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
46 resetTransactionChainForAdapaters();
47 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
51 public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
52 LOG.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
56 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
57 final DataBroker dataBroker) {
59 mountInstance = new MountInstance(mountService, id);
60 this.dataBroker = dataBroker;
61 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
63 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
66 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
68 mountInstance = new MountInstance(mountService, id);
69 this.dataBroker = null;
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);
93 public void close() throws Exception {
94 mountInstance.close();
95 if (topologyDatastoreAdapter != null) {
96 topologyDatastoreAdapter.close();
98 topologyDatastoreAdapter = null;
99 if (txChain != null) {
104 public static final class MountInstance implements AutoCloseable {
106 private final DOMMountPointService mountService;
107 private final RemoteDeviceId id;
108 private NetconfDeviceNotificationService notificationService;
110 private ObjectRegistration<DOMMountPoint> topologyRegistration;
112 public MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
113 this.mountService = Preconditions.checkNotNull(mountService);
114 this.id = Preconditions.checkNotNull(id);
117 public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
118 final DOMDataBroker broker, final DOMRpcService rpc,
119 final NetconfDeviceNotificationService notificationService) {
120 Preconditions.checkNotNull(mountService, "Closed");
121 Preconditions.checkState(topologyRegistration == null, "Already initialized");
123 final DOMMountPointService.DOMMountPointBuilder mountBuilder =
124 mountService.createMountPoint(id.getTopologyPath());
125 mountBuilder.addInitialSchemaContext(initialCtx);
127 mountBuilder.addService(DOMDataBroker.class, broker);
128 mountBuilder.addService(DOMRpcService.class, rpc);
129 mountBuilder.addService(DOMNotificationService.class, notificationService);
130 this.notificationService = notificationService;
132 topologyRegistration = mountBuilder.register();
133 LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
137 @SuppressWarnings("checkstyle:IllegalCatch")
138 public synchronized void onTopologyDeviceDisconnected() {
139 if (topologyRegistration == null) {
140 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
145 topologyRegistration.close();
146 } catch (final Exception e) {
147 // Only log and ignore
148 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
150 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
151 topologyRegistration = null;
156 public synchronized void close() throws Exception {
157 onTopologyDeviceDisconnected();
160 public synchronized void publish(final DOMNotification domNotification) {
161 Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}",
163 notificationService.publishNotification(domNotification);