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