Prepare netconf to support YANG 1.1 actions
[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.DOMActionService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
24 import org.opendaylight.yangtools.concepts.ObjectRegistration;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NetconfDeviceSalProvider implements AutoCloseable {
30
31     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
32
33     private final RemoteDeviceId id;
34     private final MountInstance mountInstance;
35     private final DataBroker dataBroker;
36
37     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
38
39     private BindingTransactionChain txChain;
40
41     private final TransactionChainListener transactionChainListener =  new TransactionChainListener() {
42         @Override
43         public void onTransactionChainFailed(final TransactionChain<?, ?> chain,
44                                              final AsyncTransaction<?, ?> transaction, final Throwable cause) {
45             LOG.error("{}: TransactionChain({}) {} FAILED!", id, chain, transaction.getIdentifier(), cause);
46             chain.close();
47             resetTransactionChainForAdapaters();
48             throw new IllegalStateException(id + "  TransactionChain(" + chain + ") not committed correctly", cause);
49         }
50
51         @Override
52         public void onTransactionChainSuccessful(final TransactionChain<?, ?> chain) {
53             LOG.trace("{}: TransactionChain({}) {} SUCCESSFUL", id, chain);
54         }
55     };
56
57     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
58         this(deviceId, mountService, null);
59     }
60
61     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
62             final DataBroker dataBroker) {
63         this.id = deviceId;
64         mountInstance = new MountInstance(mountService, id);
65         this.dataBroker = dataBroker;
66         if (dataBroker != null) {
67             txChain = Preconditions.checkNotNull(dataBroker).createTransactionChain(transactionChainListener);
68             topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, txChain);
69         }
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
110         private NetconfDeviceNotificationService notificationService;
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 void onTopologyDeviceConnected(final SchemaContext initialCtx,
119                 final DOMDataBroker broker, final DOMRpcService rpc,
120                 final NetconfDeviceNotificationService newNotificationService) {
121             onTopologyDeviceConnected(initialCtx, broker, rpc, newNotificationService, null);
122         }
123
124         public synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
125                 final DOMDataBroker broker, final DOMRpcService rpc,
126                 final NetconfDeviceNotificationService newNotificationService, DOMActionService deviceAction) {
127             Preconditions.checkNotNull(mountService, "Closed");
128             Preconditions.checkState(topologyRegistration == null, "Already initialized");
129
130             final DOMMountPointService.DOMMountPointBuilder mountBuilder =
131                     mountService.createMountPoint(id.getTopologyPath());
132             mountBuilder.addInitialSchemaContext(initialCtx);
133
134             mountBuilder.addService(DOMDataBroker.class, broker);
135             mountBuilder.addService(DOMRpcService.class, rpc);
136             mountBuilder.addService(DOMNotificationService.class, newNotificationService);
137             if (deviceAction != null) {
138                 mountBuilder.addService(DOMActionService.class, deviceAction);
139             }
140             this.notificationService = newNotificationService;
141
142             topologyRegistration = mountBuilder.register();
143             LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
144         }
145
146         @SuppressWarnings("checkstyle:IllegalCatch")
147         public synchronized void onTopologyDeviceDisconnected() {
148             if (topologyRegistration == null) {
149                 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
150                 return;
151             }
152
153             try {
154                 topologyRegistration.close();
155             } catch (final Exception e) {
156                 // Only log and ignore
157                 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
158             } finally {
159                 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
160                 topologyRegistration = null;
161             }
162         }
163
164         @Override
165         public synchronized void close() {
166             onTopologyDeviceDisconnected();
167         }
168
169         public synchronized void publish(final DOMNotification domNotification) {
170             Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}",
171                     domNotification);
172             notificationService.publishNotification(domNotification);
173         }
174     }
175
176 }