b24eb89c9927bcc3da6d7dcd1b8bcc4405de9e75
[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.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;
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 BindingTransactionChain txChain;
39
40     private final TransactionChainListener transactionChainListener =  new TransactionChainListener() {
41         @Override
42         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
43                                              final AsyncTransaction<?, ?> transaction, 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                                     final DataBroker dataBroker) {
58         this.id = deviceId;
59         mountInstance = new MountInstance(mountService, id);
60         this.dataBroker = dataBroker;
61         txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
62
63         topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
64     }
65
66     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
67         this.id = deviceId;
68         mountInstance = new MountInstance(mountService, id);
69         this.dataBroker = null;
70     }
71
72     public MountInstance getMountInstance() {
73         Preconditions.checkState(mountInstance != null,
74                 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
75         return mountInstance;
76     }
77
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;
82     }
83
84     private void resetTransactionChainForAdapaters() {
85         txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
86
87         topologyDatastoreAdapter.setTxChain(txChain);
88
89         LOG.trace("{}: Resetting TransactionChain {}", id, txChain);
90
91     }
92
93     @Override
94     public void close() {
95         mountInstance.close();
96         if (topologyDatastoreAdapter != null) {
97             topologyDatastoreAdapter.close();
98         }
99         topologyDatastoreAdapter = null;
100         if (txChain != null) {
101             txChain.close();
102         }
103     }
104
105     public static class MountInstance implements AutoCloseable {
106
107         private final DOMMountPointService mountService;
108         private final RemoteDeviceId id;
109         private NetconfDeviceNotificationService notificationService;
110
111         private ObjectRegistration<DOMMountPoint> topologyRegistration;
112
113         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
114             this.mountService = Preconditions.checkNotNull(mountService);
115             this.id = Preconditions.checkNotNull(id);
116         }
117
118         public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
119                 final DOMDataBroker broker, final DOMRpcService rpc,
120                 final NetconfDeviceNotificationService newNotificationService) {
121             Preconditions.checkNotNull(mountService, "Closed");
122             Preconditions.checkState(topologyRegistration == null, "Already initialized");
123
124             final DOMMountPointService.DOMMountPointBuilder mountBuilder =
125                     mountService.createMountPoint(id.getTopologyPath());
126             mountBuilder.addInitialSchemaContext(initialCtx);
127
128             mountBuilder.addService(DOMDataBroker.class, broker);
129             mountBuilder.addService(DOMRpcService.class, rpc);
130             mountBuilder.addService(DOMNotificationService.class, newNotificationService);
131             this.notificationService = newNotificationService;
132
133             topologyRegistration = mountBuilder.register();
134             LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
135
136         }
137
138         @SuppressWarnings("checkstyle:IllegalCatch")
139         public synchronized void onTopologyDeviceDisconnected() {
140             if (topologyRegistration == null) {
141                 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
142                 return;
143             }
144
145             try {
146                 topologyRegistration.close();
147             } catch (final Exception e) {
148                 // Only log and ignore
149                 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
150             } finally {
151                 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
152                 topologyRegistration = null;
153             }
154         }
155
156         @Override
157         public synchronized void close() {
158             onTopologyDeviceDisconnected();
159         }
160
161         public synchronized void publish(final DOMNotification domNotification) {
162             Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}",
163                     domNotification);
164             notificationService.publishNotification(domNotification);
165         }
166     }
167
168 }