Move transaction chain handling
[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 static com.google.common.base.Preconditions.checkNotNull;
11 import static com.google.common.base.Preconditions.checkState;
12 import static java.util.Objects.requireNonNull;
13
14 import org.opendaylight.mdsal.binding.api.DataBroker;
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.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
24 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
25 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
26 import org.opendaylight.yangtools.concepts.ObjectRegistration;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class NetconfDeviceSalProvider implements AutoCloseable {
32     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
33
34     private final RemoteDeviceId id;
35     private final MountInstance mountInstance;
36
37     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
38
39     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService) {
40         this(deviceId, mountService, null);
41     }
42
43     // FIXME: NETCONF-918: remove this method
44     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId, final DOMMountPointService mountService,
45             final DataBroker dataBroker) {
46         id = deviceId;
47         mountInstance = new MountInstance(mountService, id);
48         if (dataBroker != null) {
49             topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, id);
50         }
51     }
52
53     public MountInstance getMountInstance() {
54         checkState(mountInstance != null, "%s: Mount instance was not initialized by sal. Cannot get mount instance",
55                 id);
56         return mountInstance;
57     }
58
59     public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
60         final NetconfDeviceTopologyAdapter local = topologyDatastoreAdapter;
61         checkState(local != null,
62                 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id, this);
63         return local;
64     }
65
66     @Override
67     public void close() {
68         mountInstance.close();
69         if (topologyDatastoreAdapter != null) {
70             topologyDatastoreAdapter.close();
71             topologyDatastoreAdapter = null;
72         }
73     }
74
75     public static class MountInstance implements AutoCloseable {
76
77         private final DOMMountPointService mountService;
78         private final RemoteDeviceId id;
79
80         private NetconfDeviceNotificationService notificationService;
81         private ObjectRegistration<DOMMountPoint> topologyRegistration;
82
83         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
84             this.mountService = requireNonNull(mountService);
85             this.id = requireNonNull(id);
86         }
87
88         public synchronized void onTopologyDeviceConnected(final EffectiveModelContext initialCtx,
89                 final DOMDataBroker broker, final NetconfDataTreeService netconfService, final DOMRpcService rpc,
90                 final NetconfDeviceNotificationService newNotificationService, final DOMActionService deviceAction) {
91             requireNonNull(mountService, "Closed");
92             checkState(topologyRegistration == null, "Already initialized");
93
94             final DOMMountPointService.DOMMountPointBuilder mountBuilder =
95                     mountService.createMountPoint(id.getTopologyPath());
96             mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
97
98             if (broker != null) {
99                 mountBuilder.addService(DOMDataBroker.class, broker);
100             }
101             mountBuilder.addService(DOMRpcService.class, rpc);
102             mountBuilder.addService(DOMNotificationService.class, newNotificationService);
103             if (deviceAction != null) {
104                 mountBuilder.addService(DOMActionService.class, deviceAction);
105             }
106             if (netconfService != null) {
107                 mountBuilder.addService(NetconfDataTreeService.class, netconfService);
108             }
109             notificationService = newNotificationService;
110
111             topologyRegistration = mountBuilder.register();
112             LOG.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
113         }
114
115         @SuppressWarnings("checkstyle:IllegalCatch")
116         public synchronized void onTopologyDeviceDisconnected() {
117             if (topologyRegistration == null) {
118                 LOG.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
119                 return;
120             }
121
122             try {
123                 topologyRegistration.close();
124             } catch (final Exception e) {
125                 // Only log and ignore
126                 LOG.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
127             } finally {
128                 LOG.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, topologyRegistration);
129                 topologyRegistration = null;
130             }
131         }
132
133         @Override
134         public synchronized void close() {
135             onTopologyDeviceDisconnected();
136         }
137
138         public synchronized void publish(final DOMNotification domNotification) {
139             checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
140                 .publishNotification(domNotification);
141         }
142     }
143
144 }