Rename MountInstance to NetconfDeviceMount
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / sal / NetconfDeviceMount.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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 // Non-final for mocking
36 public class NetconfDeviceMount implements AutoCloseable {
37     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceMount.class);
38
39     private final DOMMountPointService mountService;
40     private final RemoteDeviceId id;
41
42     private NetconfDeviceNotificationService notificationService;
43     private ObjectRegistration<DOMMountPoint> topologyRegistration;
44
45     public NetconfDeviceMount(final DOMMountPointService mountService, final RemoteDeviceId id) {
46         this.mountService = requireNonNull(mountService);
47         this.id = requireNonNull(id);
48     }
49
50     public void onDeviceConnected(final EffectiveModelContext initialCtx,
51             final RemoteDeviceServices services, final DOMDataBroker broker,
52             final NetconfDataTreeService dataTreeService) {
53         onDeviceConnected(initialCtx, services, new NetconfDeviceNotificationService(), broker,
54             dataTreeService);
55     }
56
57     public synchronized void onDeviceConnected(final EffectiveModelContext initialCtx,
58             final RemoteDeviceServices services, final NetconfDeviceNotificationService newNotificationService,
59             final DOMDataBroker broker, final NetconfDataTreeService dataTreeService) {
60         requireNonNull(mountService, "Closed");
61         checkState(topologyRegistration == null, "Already initialized");
62
63         final var mountBuilder = mountService.createMountPoint(id.getTopologyPath());
64         mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
65
66         final var rpcs = services.rpcs();
67         mountBuilder.addService(NetconfRpcService.class, rpcs);
68         if (rpcs instanceof Rpcs.Normalized normalized) {
69             mountBuilder.addService(DOMRpcService.class, normalized);
70         } else if (rpcs instanceof Rpcs.Schemaless schemaless) {
71             mountBuilder.addService(SchemalessRpcService.class, schemaless);
72         }
73         if (services.actions() instanceof Actions.Normalized normalized) {
74             mountBuilder.addService(DOMActionService.class, normalized);
75         }
76
77         if (broker != null) {
78             mountBuilder.addService(DOMDataBroker.class, broker);
79         }
80         if (dataTreeService != null) {
81             mountBuilder.addService(NetconfDataTreeService.class, dataTreeService);
82         }
83         mountBuilder.addService(DOMNotificationService.class, newNotificationService);
84         notificationService = newNotificationService;
85
86         topologyRegistration = mountBuilder.register();
87         LOG.debug("{}: Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
88     }
89
90     public synchronized void onDeviceDisconnected() {
91         if (topologyRegistration == null) {
92             LOG.trace("{}: Not removing mountpoint from MD-SAL, mountpoint was not registered yet", id);
93             return;
94         }
95
96         try {
97             topologyRegistration.close();
98         } finally {
99             LOG.debug("{}: Mountpoint removed from MD-SAL {}", id, topologyRegistration);
100             topologyRegistration = null;
101         }
102     }
103
104     public synchronized void publish(final DOMNotification domNotification) {
105         checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
106             .publishNotification(domNotification);
107     }
108
109     @Override
110     public synchronized void close() {
111         onDeviceDisconnected();
112     }
113 }