eddce47171d676c0a30b344e5f7338a52dbde567
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / spi / 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.client.mdsal.spi;
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.client.mdsal.api.NetconfRpcService;
24 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
25 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
26 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Actions;
27 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Rpcs;
28 import org.opendaylight.netconf.client.mdsal.api.SchemalessRpcService;
29 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
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, dataTreeService);
58     }
59
60     public synchronized void onDeviceConnected(final EffectiveModelContext initialCtx,
61             final RemoteDeviceServices services, final NetconfDeviceNotificationService newNotificationService,
62             final DOMDataBroker broker, final NetconfDataTreeService dataTreeService) {
63         requireNonNull(mountService, "Closed");
64         checkState(topologyRegistration == null, "Already initialized");
65
66         final var mountBuilder = mountService.createMountPoint(mountPath);
67         mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
68
69         final var rpcs = services.rpcs();
70         mountBuilder.addService(NetconfRpcService.class, rpcs);
71         if (rpcs instanceof Rpcs.Normalized normalized) {
72             mountBuilder.addService(DOMRpcService.class, normalized);
73         } else if (rpcs instanceof Rpcs.Schemaless schemaless) {
74             mountBuilder.addService(SchemalessRpcService.class, schemaless);
75         }
76         if (services.actions() instanceof Actions.Normalized normalized) {
77             mountBuilder.addService(DOMActionService.class, normalized);
78         }
79
80         if (broker != null) {
81             mountBuilder.addService(DOMDataBroker.class, broker);
82         }
83         if (dataTreeService != null) {
84             mountBuilder.addService(NetconfDataTreeService.class, dataTreeService);
85         }
86         mountBuilder.addService(DOMNotificationService.class, newNotificationService);
87         notificationService = newNotificationService;
88
89         topologyRegistration = mountBuilder.register();
90         LOG.debug("{}: Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
91     }
92
93     public synchronized void onDeviceDisconnected() {
94         if (topologyRegistration == null) {
95             LOG.trace("{}: Not removing mountpoint from MD-SAL, mountpoint was not registered yet", id);
96             return;
97         }
98
99         try {
100             topologyRegistration.close();
101         } finally {
102             LOG.debug("{}: Mountpoint removed from MD-SAL {}", id, topologyRegistration);
103             topologyRegistration = null;
104         }
105     }
106
107     public synchronized void publish(final DOMNotification domNotification) {
108         checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
109             .publishNotification(domNotification);
110     }
111
112     @Override
113     public synchronized void close() {
114         onDeviceDisconnected();
115     }
116 }