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