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