Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceSalProvider.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.sal;
9
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.mdsal.binding.api.DataBroker;
12 import org.opendaylight.mdsal.binding.api.Transaction;
13 import org.opendaylight.mdsal.binding.api.TransactionChain;
14 import org.opendaylight.mdsal.binding.api.TransactionChainListener;
15 import org.opendaylight.mdsal.dom.api.DOMActionService;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
21 import org.opendaylight.mdsal.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;
27
28 public class NetconfDeviceSalProvider implements AutoCloseable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
31
32     private final RemoteDeviceId id;
33     private final MountInstance mountInstance;
34     private final DataBroker dataBroker;
35
36     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
37
38     private TransactionChain txChain;
39
40     private final TransactionChainListener transactionChainListener =  new TransactionChainListener() {
41         @Override
42         public void onTransactionChainFailed(final TransactionChain chain, final Transaction transaction,
43                 final Throwable cause) {
44             LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
45             chain.close();
46             resetTransactionChainForAdapaters();
47             throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
48         }
49
50         @Override
51         public void onTransactionChainSuccessful(final TransactionChain chain) {
52             LOG.trace("{}: TransactionChain({}) SUCCESSFUL", id, chain);
53         }
54     };
55
56     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
57         this(deviceId, mountService, null);
58     }
59
60     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
61             final DataBroker dataBroker) {
62         this.id = deviceId;
63         mountInstance = new MountInstance(mountService, id);
64         this.dataBroker = dataBroker;
65         if (dataBroker != null) {
66             txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
67             topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
68         }
69     }
70
71     public MountInstance getMountInstance() {
72         Preconditions.checkState(mountInstance != null,
73                 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
74         return mountInstance;
75     }
76
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;
81     }
82
83     private void resetTransactionChainForAdapaters() {
84         txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
85
86         topologyDatastoreAdapter.setTxChain(txChain);
87
88         LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
89
90     }
91
92     @Override
93     public void close() {
94         mountInstance.close();
95         if (topologyDatastoreAdapter != null) {
96             topologyDatastoreAdapter.close();
97         }
98         topologyDatastoreAdapter = null;
99         if (txChain != null) {
100             txChain.close();
101         }
102     }
103
104     public static class MountInstance implements AutoCloseable {
105
106         private final DOMMountPointService mountService;
107         private final RemoteDeviceId id;
108
109         private NetconfDeviceNotificationService notificationService;
110         private ObjectRegistration<DOMMountPoint> topologyRegistration;
111
112         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
113             this.mountService = Preconditions.checkNotNull(mountService);
114             this.id = Preconditions.checkNotNull(id);
115         }
116
117         public void onTopologyDeviceConnected(final SchemaContext initialCtx,
118                 final DOMDataBroker broker, final DOMRpcService rpc,
119                 final NetconfDeviceNotificationService newNotificationService) {
120             onTopologyDeviceConnected(initialCtx, broker, rpc, newNotificationService, null);
121         }
122
123         public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
124                 final DOMDataBroker broker, final DOMRpcService rpc,
125                 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
126             Preconditions.checkNotNull(mountService, "Closed");
127             Preconditions.checkState(topologyRegistration == null, "Already initialized");
128
129             final DOMMountPointService.DOMMountPointBuilder mountBuilder =
130                     mountService.createMountPoint(id.getTopologyPath());
131             mountBuilder.addInitialSchemaContext(initialCtx);
132
133             mountBuilder.addService(DOMDataBroker.class, broker);
134             mountBuilder.addService(DOMRpcService.class, rpc);
135             mountBuilder.addService(DOMNotificationService.class, newNotificationService);
136             if (deviceAction != null) {
137                 mountBuilder.addService(DOMActionService.class, deviceAction);
138             }
139             this.notificationService = newNotificationService;
140
141             topologyRegistration = mountBuilder.register();
142             LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
143         }
144
145         @SuppressWarnings("checkstyle:IllegalCatch")
146         public synchronized void onTopologyDeviceDisconnected() {
147             if (topologyRegistration == null) {
148                 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
149                 return;
150             }
151
152             try {
153                 topologyRegistration.close();
154             } catch (final Exception e) {
155                 // Only log and ignore
156                 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
157             } finally {
158                 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
159                 topologyRegistration = null;
160             }
161         }
162
163         @Override
164         public synchronized void close() {
165             onTopologyDeviceDisconnected();
166         }
167
168         public synchronized void publish(final DOMNotification domNotification) {
169             Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}",
170                     domNotification);
171             notificationService.publishNotification(domNotification);
172         }
173     }
174
175 }