2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.messagebus.eventsources.netconf;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.time.Instant;
15 import java.time.ZoneId;
16 import java.time.ZonedDateTime;
17 import java.time.format.DateTimeFormatter;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
27 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
28 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
29 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
30 import org.opendaylight.mdsal.dom.api.DOMRpcService;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.mdsal.dom.api.DOMService;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
49 * Facade of mounted netconf device.
51 @Deprecated(forRemoval = true)
52 class NetconfEventSourceMount {
53 private static final YangInstanceIdentifier STREAMS_PATH = YangInstanceIdentifier.builder().node(Netconf.QNAME)
54 .node(Streams.QNAME).build();
55 private static final QName CREATE_SUBSCRIPTION = QName.create(CreateSubscriptionInput.QNAME, "create-subscription");
57 private final DOMRpcService rpcService;
58 private final DOMNotificationService notificationService;
59 private final DOMDataBroker dataBroker;
60 private final Node node;
61 private final String nodeId;
62 private final BindingNormalizedNodeSerializer serializer;
63 private final DOMSchemaService schemaService;
65 NetconfEventSourceMount(final BindingNormalizedNodeSerializer serializer, final Node node,
66 final DOMMountPoint mountPoint) {
67 this.serializer = requireNonNull(serializer);
69 this.nodeId = node.getNodeId().getValue();
70 this.rpcService = getService(mountPoint, DOMRpcService.class);
71 this.notificationService = getService(mountPoint, DOMNotificationService.class);
72 this.dataBroker = getService(mountPoint, DOMDataBroker.class);
73 this.schemaService = getService(mountPoint, DOMSchemaService.class);
76 private static <T extends DOMService> T getService(final DOMMountPoint mountPoint, final Class<T> service) {
77 final Optional<T> optional = mountPoint.getService(service);
78 Preconditions.checkState(optional.isPresent(), "Service not present on mount point: %s", service.getName());
79 return optional.get();
91 * Invokes create-subscription rpc on mounted device stream. If lastEventTime is provided and stream supports
93 * rpc will be invoked with start time parameter.
95 * @param stream stream
96 * @param lastEventTime last event time
99 ListenableFuture<? extends DOMRpcResult> invokeCreateSubscription(final Stream stream,
100 final Optional<Instant> lastEventTime) {
101 final CreateSubscriptionInputBuilder inputBuilder = new CreateSubscriptionInputBuilder()
102 .setStream(stream.getName());
103 if (lastEventTime.isPresent() && stream.getReplaySupport()) {
104 final ZonedDateTime dateTime = lastEventTime.get().atZone(ZoneId.systemDefault());
105 final String formattedDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dateTime);
106 inputBuilder.setStartTime(new DateAndTime(formattedDate));
108 final CreateSubscriptionInput input = inputBuilder.build();
109 final ContainerNode nnInput = serializer.toNormalizedNodeRpcData(input);
110 return rpcService.invokeRpc(CREATE_SUBSCRIPTION, nnInput);
114 * Invokes create-subscription rpc on mounted device stream.
116 * @param stream stream
119 ListenableFuture<? extends DOMRpcResult> invokeCreateSubscription(final Stream stream) {
120 return invokeCreateSubscription(stream, Optional.empty());
124 * Returns list of streams available on device.
126 * @return list of streams
127 * @throws ExecutionException if data read fails
128 * @throws InterruptedException if data read fails
130 Collection<Stream> getAvailableStreams() throws InterruptedException, ExecutionException {
131 final Optional<NormalizedNode<?, ?>> streams;
132 try (DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
133 streams = tx.read(LogicalDatastoreType.OPERATIONAL, STREAMS_PATH).get();
135 if (streams.isPresent()) {
136 Streams streams1 = (Streams) serializer.fromNormalizedNode(STREAMS_PATH, streams.get()).getValue();
137 return streams1.nonnullStream().values();
139 return Collections.emptyList();
142 EffectiveModelContext getSchemaContext() {
143 return schemaService.getGlobalContext();
147 * Registers notification listener to receive a set of notifications.
149 * @param listener listener
150 * @param notificationPath notification path
151 * @return ListenerRegistration
152 * @see DOMNotificationService#registerNotificationListener(DOMNotificationListener, SchemaPath...)
154 ListenerRegistration<DOMNotificationListener> registerNotificationListener(final DOMNotificationListener listener,
155 final SchemaPath notificationPath) {
156 return notificationService.registerNotificationListener(listener, notificationPath.asAbsolute());