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;
12 import java.util.Collection;
13 import java.util.Collections;
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
19 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
20 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
24 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
28 import org.opendaylight.controller.sal.core.api.Broker;
29 import org.opendaylight.controller.sal.core.api.Provider;
30 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
31 import org.opendaylight.yangtools.concepts.ObjectRegistration;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
38 private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
40 private final RemoteDeviceId id;
41 private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
42 private MountInstance mountInstance;
44 private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
46 private DataBroker dataBroker;
47 private BindingTransactionChain txChain;
49 private final TransactionChainListener transactionChainListener = new TransactionChainListener() {
51 public void onTransactionChainFailed(TransactionChain<?, ?> chain, AsyncTransaction<?, ?> transaction, Throwable cause) {
52 logger.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
54 resetTransactionChainForAdapaters();
55 throw new IllegalStateException(id + " TransactionChain(" + chain + ") not committed correctly", cause);
59 public void onTransactionChainSuccessful(TransactionChain<?, ?> chain) {
60 logger.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
64 public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
68 public MountInstance getMountInstance() {
69 Preconditions.checkState(mountInstance != null,
70 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
74 public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
75 Preconditions.checkState(datastoreAdapter != null,
76 "%s: Sal provider %s was not initialized by sal. Cannot get datastore adapter", id);
77 return datastoreAdapter;
80 public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
81 Preconditions.checkState(topologyDatastoreAdapter != null,
82 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
83 return topologyDatastoreAdapter;
87 public void onSessionInitiated(final Broker.ProviderSession session) {
88 logger.debug("{}: (BI)Session with sal established {}", id, session);
90 final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
91 if (mountService != null) {
92 mountInstance = new MountInstance(mountService, id);
97 public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
98 return Collections.emptySet();
102 public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
103 logger.debug("{}: Session with sal established {}", id, session);
105 this.dataBroker = session.getSALService(DataBroker.class);
106 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
108 datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, txChain);
109 topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
112 private void resetTransactionChainForAdapaters() {
113 txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
115 datastoreAdapter.setTxChain(txChain);
116 topologyDatastoreAdapter.setTxChain(txChain);
118 logger.trace("{}: Resetting TransactionChain {}", id, txChain);
122 public void close() throws Exception {
123 mountInstance.close();
124 datastoreAdapter.close();
125 datastoreAdapter = null;
126 topologyDatastoreAdapter.close();
127 topologyDatastoreAdapter = null;
131 public static final class MountInstance implements AutoCloseable {
133 private DOMMountPointService mountService;
134 private final RemoteDeviceId id;
135 private ObjectRegistration<DOMMountPoint> registration;
136 private NetconfDeviceNotificationService notificationService;
138 private ObjectRegistration<DOMMountPoint> topologyRegistration;
140 public MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
141 this.mountService = Preconditions.checkNotNull(mountService);
142 this.id = Preconditions.checkNotNull(id);
146 synchronized void onDeviceConnected(final SchemaContext initialCtx,
147 final DOMDataBroker broker, final DOMRpcService rpc,
148 final NetconfDeviceNotificationService notificationService) {
150 Preconditions.checkNotNull(mountService, "Closed");
151 Preconditions.checkState(registration == null, "Already initialized");
153 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
154 mountBuilder.addInitialSchemaContext(initialCtx);
156 mountBuilder.addService(DOMDataBroker.class, broker);
157 mountBuilder.addService(DOMRpcService.class, rpc);
158 mountBuilder.addService(DOMNotificationService.class, notificationService);
159 this.notificationService = notificationService;
161 registration = mountBuilder.register();
162 logger.debug("{}: Mountpoint exposed into MD-SAL {}", id, registration);
166 synchronized void onDeviceDisconnected() {
167 if(registration == null) {
168 logger.trace("{}: Not removing mountpoint from MD-SAL, mountpoint was not registered yet", id);
173 registration.close();
174 } catch (final Exception e) {
175 // Only log and ignore
176 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getPath(), e);
178 logger.debug("{}: Mountpoint removed from MD-SAL {}", id, registration);
183 public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
184 final DOMDataBroker broker, final DOMRpcService rpc,
185 final NetconfDeviceNotificationService notificationService) {
187 Preconditions.checkNotNull(mountService, "Closed");
188 Preconditions.checkState(topologyRegistration == null, "Already initialized");
190 final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
191 mountBuilder.addInitialSchemaContext(initialCtx);
193 mountBuilder.addService(DOMDataBroker.class, broker);
194 mountBuilder.addService(DOMRpcService.class, rpc);
195 mountBuilder.addService(DOMNotificationService.class, notificationService);
197 topologyRegistration = mountBuilder.register();
198 logger.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
202 public synchronized void onTopologyDeviceDisconnected() {
203 if(topologyRegistration == null) {
204 logger.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
209 topologyRegistration.close();
210 } catch (final Exception e) {
211 // Only log and ignore
212 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
214 logger.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
215 topologyRegistration = null;
220 public synchronized void close() throws Exception {
221 onDeviceDisconnected();
222 onTopologyDeviceDisconnected();
226 public synchronized void publish(final DOMNotification domNotification) {
227 Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
228 notificationService.publishNotification(domNotification);