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