Move RemoteDeviceId
[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.eclipse.jdt.annotation.NonNull;
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.RemoteDeviceId;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
28 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Actions;
29 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices.Rpcs;
30 import org.opendaylight.netconf.sal.connect.api.SchemalessRpcService;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
34 import org.opendaylight.yangtools.concepts.ObjectRegistration;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
38 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 // Non-final for mocking
43 public class NetconfDeviceMount implements AutoCloseable {
44     private static final Logger LOG = LoggerFactory.getLogger(NetconfDeviceMount.class);
45     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "node-id").intern();
46     // FIXME: push this out to callers
47     private static final YangInstanceIdentifier DEFAULT_TOPOLOGY_NODE = YangInstanceIdentifier.builder()
48         .node(NetworkTopology.QNAME).node(Topology.QNAME)
49         .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), RemoteDeviceId.DEFAULT_TOPOLOGY_NAME)
50         .node(Node.QNAME)
51         .build();
52
53     private final DOMMountPointService mountService;
54     private final YangInstanceIdentifier mountPath;
55     private final RemoteDeviceId id;
56
57     private NetconfDeviceNotificationService notificationService;
58     private ObjectRegistration<DOMMountPoint> topologyRegistration;
59
60     @Deprecated(forRemoval = true)
61     public NetconfDeviceMount(final DOMMountPointService mountService, final RemoteDeviceId id) {
62         this(id, mountService, defaultTopologyMountPath(id));
63     }
64
65     public NetconfDeviceMount(final RemoteDeviceId id, final DOMMountPointService mountService,
66             final YangInstanceIdentifier mountPath) {
67         this.id = requireNonNull(id);
68         this.mountService = requireNonNull(mountService);
69         this.mountPath = requireNonNull(mountPath);
70     }
71
72     @Deprecated(forRemoval = true)
73     public static @NonNull YangInstanceIdentifier defaultTopologyMountPath(final RemoteDeviceId id) {
74         return DEFAULT_TOPOLOGY_NODE.node(NodeIdentifierWithPredicates.of(Node.QNAME, NODE_ID_QNAME, id.name()));
75     }
76
77     public void onDeviceConnected(final EffectiveModelContext initialCtx,
78             final RemoteDeviceServices services, final DOMDataBroker broker,
79             final NetconfDataTreeService dataTreeService) {
80         onDeviceConnected(initialCtx, services, new NetconfDeviceNotificationService(), broker,
81             dataTreeService);
82     }
83
84     public synchronized void onDeviceConnected(final EffectiveModelContext initialCtx,
85             final RemoteDeviceServices services, final NetconfDeviceNotificationService newNotificationService,
86             final DOMDataBroker broker, final NetconfDataTreeService dataTreeService) {
87         requireNonNull(mountService, "Closed");
88         checkState(topologyRegistration == null, "Already initialized");
89
90         final var mountBuilder = mountService.createMountPoint(mountPath);
91         mountBuilder.addService(DOMSchemaService.class, FixedDOMSchemaService.of(() -> initialCtx));
92
93         final var rpcs = services.rpcs();
94         mountBuilder.addService(NetconfRpcService.class, rpcs);
95         if (rpcs instanceof Rpcs.Normalized normalized) {
96             mountBuilder.addService(DOMRpcService.class, normalized);
97         } else if (rpcs instanceof Rpcs.Schemaless schemaless) {
98             mountBuilder.addService(SchemalessRpcService.class, schemaless);
99         }
100         if (services.actions() instanceof Actions.Normalized normalized) {
101             mountBuilder.addService(DOMActionService.class, normalized);
102         }
103
104         if (broker != null) {
105             mountBuilder.addService(DOMDataBroker.class, broker);
106         }
107         if (dataTreeService != null) {
108             mountBuilder.addService(NetconfDataTreeService.class, dataTreeService);
109         }
110         mountBuilder.addService(DOMNotificationService.class, newNotificationService);
111         notificationService = newNotificationService;
112
113         topologyRegistration = mountBuilder.register();
114         LOG.debug("{}: Mountpoint exposed into MD-SAL {}", id, topologyRegistration);
115     }
116
117     public synchronized void onDeviceDisconnected() {
118         if (topologyRegistration == null) {
119             LOG.trace("{}: Not removing mountpoint from MD-SAL, mountpoint was not registered yet", id);
120             return;
121         }
122
123         try {
124             topologyRegistration.close();
125         } finally {
126             LOG.debug("{}: Mountpoint removed from MD-SAL {}", id, topologyRegistration);
127             topologyRegistration = null;
128         }
129     }
130
131     public synchronized void publish(final DOMNotification domNotification) {
132         checkNotNull(notificationService, "Device not set up yet, cannot handle notification %s", domNotification)
133             .publishNotification(domNotification);
134     }
135
136     @Override
137     public synchronized void close() {
138         onDeviceDisconnected();
139     }
140 }