bf870bf36a07ccd421f86e9eeba1571ad73f0e5e
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / 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.controller.sal.connect.netconf.sal;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Collections;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
16 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
22 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
23 import org.opendaylight.controller.sal.core.api.Broker;
24 import org.opendaylight.controller.sal.core.api.Provider;
25 import org.opendaylight.yangtools.concepts.ObjectRegistration;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 final class NetconfDeviceSalProvider implements AutoCloseable, Provider, BindingAwareProvider {
31
32     private static final Logger logger = LoggerFactory.getLogger(NetconfDeviceSalProvider.class);
33
34     private final RemoteDeviceId id;
35     private volatile NetconfDeviceDatastoreAdapter datastoreAdapter;
36     private MountInstance mountInstance;
37
38     private volatile NetconfDeviceTopologyAdapter topologyDatastoreAdapter;
39
40     public NetconfDeviceSalProvider(final RemoteDeviceId deviceId) {
41         this.id = deviceId;
42     }
43
44     public MountInstance getMountInstance() {
45         Preconditions.checkState(mountInstance != null,
46                 "%s: Mount instance was not initialized by sal. Cannot get mount instance", id);
47         return mountInstance;
48     }
49
50     public NetconfDeviceDatastoreAdapter getDatastoreAdapter() {
51         Preconditions.checkState(datastoreAdapter != null,
52                 "%s: Sal provider %s was not initialized by sal. Cannot get datastore adapter", id);
53         return datastoreAdapter;
54     }
55
56     public NetconfDeviceTopologyAdapter getTopologyDatastoreAdapter() {
57         Preconditions.checkState(topologyDatastoreAdapter != null,
58                 "%s: Sal provider %s was not initialized by sal. Cannot get topology datastore adapter", id);
59         return topologyDatastoreAdapter;
60     }
61
62     @Override
63     public void onSessionInitiated(final Broker.ProviderSession session) {
64         logger.debug("{}: (BI)Session with sal established {}", id, session);
65
66         final DOMMountPointService mountService = session.getService(DOMMountPointService.class);
67         if (mountService != null) {
68             mountInstance = new MountInstance(mountService, id);
69         }
70     }
71
72     @Override
73     public Collection<Provider.ProviderFunctionality> getProviderFunctionality() {
74         return Collections.emptySet();
75     }
76
77     @Override
78     public void onSessionInitiated(final BindingAwareBroker.ProviderContext session) {
79         logger.debug("{}: Session with sal established {}", id, session);
80
81         final DataBroker dataBroker = session.getSALService(DataBroker.class);
82         datastoreAdapter = new NetconfDeviceDatastoreAdapter(id, dataBroker);
83
84         topologyDatastoreAdapter = new NetconfDeviceTopologyAdapter(id, dataBroker);
85     }
86
87     public void close() throws Exception {
88         mountInstance.close();
89         datastoreAdapter.close();
90         datastoreAdapter = null;
91         topologyDatastoreAdapter.close();
92         topologyDatastoreAdapter = null;
93     }
94
95     static final class MountInstance implements AutoCloseable {
96
97         private DOMMountPointService mountService;
98         private final RemoteDeviceId id;
99         private ObjectRegistration<DOMMountPoint> registration;
100         private NetconfDeviceNotificationService notificationService;
101
102         private ObjectRegistration<DOMMountPoint> topologyRegistration;
103
104         MountInstance(final DOMMountPointService mountService, final RemoteDeviceId id) {
105             this.mountService = Preconditions.checkNotNull(mountService);
106             this.id = Preconditions.checkNotNull(id);
107         }
108
109         @Deprecated
110         synchronized void onDeviceConnected(final SchemaContext initialCtx,
111                 final DOMDataBroker broker, final DOMRpcService rpc,
112                 final NetconfDeviceNotificationService notificationService) {
113
114             Preconditions.checkNotNull(mountService, "Closed");
115             Preconditions.checkState(registration == null, "Already initialized");
116
117             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getPath());
118             mountBuilder.addInitialSchemaContext(initialCtx);
119
120             mountBuilder.addService(DOMDataBroker.class, broker);
121             mountBuilder.addService(DOMRpcService.class, rpc);
122             mountBuilder.addService(DOMNotificationService.class, notificationService);
123             this.notificationService = notificationService;
124
125             registration = mountBuilder.register();
126             logger.debug("{}: Mountpoint exposed into MD-SAL {}", id, registration);
127         }
128
129         @Deprecated
130         synchronized void onDeviceDisconnected() {
131             if(registration == null) {
132                 logger.trace("{}: Not removing mountpoint from MD-SAL, mountpoint was not registered yet", id);
133                 return;
134             }
135
136             try {
137                 registration.close();
138             } catch (final Exception e) {
139                 // Only log and ignore
140                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getPath(), e);
141             } finally {
142                 logger.debug("{}: Mountpoint removed from MD-SAL {}", id, registration);
143                 registration = null;
144             }
145         }
146
147         synchronized void onTopologyDeviceConnected(final SchemaContext initialCtx,
148                                                     final DOMDataBroker broker, final DOMRpcService rpc,
149                                                     final NetconfDeviceNotificationService notificationService) {
150
151             Preconditions.checkNotNull(mountService, "Closed");
152             Preconditions.checkState(topologyRegistration == null, "Already initialized");
153
154             final DOMMountPointService.DOMMountPointBuilder mountBuilder = mountService.createMountPoint(id.getTopologyPath());
155             mountBuilder.addInitialSchemaContext(initialCtx);
156
157             mountBuilder.addService(DOMDataBroker.class, broker);
158             mountBuilder.addService(DOMRpcService.class, rpc);
159             mountBuilder.addService(DOMNotificationService.class, notificationService);
160
161             topologyRegistration = mountBuilder.register();
162             logger.debug("{}: TOPOLOGY Mountpoint exposed into MD-SAL {}", id, registration);
163
164         }
165
166         synchronized void onTopologyDeviceDisconnected() {
167             if(topologyRegistration == null) {
168                 logger.trace("{}: Not removing TOPOLOGY mountpoint from MD-SAL, mountpoint was not registered yet", id);
169                 return;
170             }
171
172             try {
173                 topologyRegistration.close();
174             } catch (final Exception e) {
175                 // Only log and ignore
176                 logger.warn("Unable to unregister mount instance for {}. Ignoring exception", id.getTopologyPath(), e);
177             } finally {
178                 logger.debug("{}: TOPOLOGY Mountpoint removed from MD-SAL {}", id, registration);
179                 topologyRegistration = null;
180             }
181         }
182
183         @Override
184         synchronized public void close() throws Exception {
185             onDeviceDisconnected();
186             onTopologyDeviceDisconnected();
187             mountService = null;
188         }
189
190         public synchronized void publish(final DOMNotification domNotification) {
191             Preconditions.checkNotNull(notificationService, "Device not set up yet, cannot handle notification {}", domNotification);
192             notificationService.publishNotification(domNotification);
193         }
194     }
195
196 }