33f49ebb64534102a757ea76864687994fb2acfc
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceMount.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.messagebus.eventsources.netconf;
9
10 import static java.util.Objects.requireNonNull;
11
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;
47
48 /**
49  * Facade of mounted netconf device.
50  */
51 class NetconfEventSourceMount {
52     private static final YangInstanceIdentifier STREAMS_PATH = YangInstanceIdentifier.builder().node(Netconf.QNAME)
53             .node(Streams.QNAME).build();
54     private static final QName CREATE_SUBSCRIPTION = QName.create(CreateSubscriptionInput.QNAME, "create-subscription");
55
56     private final DOMRpcService rpcService;
57     private final DOMNotificationService notificationService;
58     private final DOMDataBroker dataBroker;
59     private final Node node;
60     private final String nodeId;
61     private final BindingNormalizedNodeSerializer serializer;
62     private final DOMSchemaService schemaService;
63
64     NetconfEventSourceMount(final BindingNormalizedNodeSerializer serializer, final Node node,
65             final DOMMountPoint mountPoint) {
66         this.serializer = requireNonNull(serializer);
67         this.node = node;
68         this.nodeId = node.getNodeId().getValue();
69         this.rpcService = getService(mountPoint, DOMRpcService.class);
70         this.notificationService = getService(mountPoint, DOMNotificationService.class);
71         this.dataBroker = getService(mountPoint, DOMDataBroker.class);
72         this.schemaService = getService(mountPoint, DOMSchemaService.class);
73     }
74
75     private static <T extends DOMService> T getService(final DOMMountPoint mountPoint, final Class<T> service) {
76         final Optional<T> optional = mountPoint.getService(service);
77         Preconditions.checkState(optional.isPresent(), "Service not present on mount point: %s", service.getName());
78         return optional.get();
79     }
80
81     Node getNode() {
82         return node;
83     }
84
85     String getNodeId() {
86         return nodeId;
87     }
88
89     /**
90      * Invokes create-subscription rpc on mounted device stream. If lastEventTime is provided and stream supports
91      * replay,
92      * rpc will be invoked with start time parameter.
93      *
94      * @param stream        stream
95      * @param lastEventTime last event time
96      * @return rpc result
97      */
98     ListenableFuture<? extends DOMRpcResult> invokeCreateSubscription(final Stream stream,
99             final Optional<Instant> lastEventTime) {
100         final CreateSubscriptionInputBuilder inputBuilder = new CreateSubscriptionInputBuilder()
101                 .setStream(stream.getName());
102         if (lastEventTime.isPresent() && stream.isReplaySupport()) {
103             final ZonedDateTime dateTime = lastEventTime.get().atZone(ZoneId.systemDefault());
104             final String formattedDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dateTime);
105             inputBuilder.setStartTime(new DateAndTime(formattedDate));
106         }
107         final CreateSubscriptionInput input = inputBuilder.build();
108         final ContainerNode nnInput = serializer.toNormalizedNodeRpcData(input);
109         return rpcService.invokeRpc(CREATE_SUBSCRIPTION, nnInput);
110     }
111
112     /**
113      * Invokes create-subscription rpc on mounted device stream.
114      *
115      * @param stream stream
116      * @return rpc result
117      */
118     ListenableFuture<? extends DOMRpcResult> invokeCreateSubscription(final Stream stream) {
119         return invokeCreateSubscription(stream, Optional.empty());
120     }
121
122     /**
123      * Returns list of streams available on device.
124      *
125      * @return list of streams
126      * @throws ExecutionException if data read fails
127      * @throws InterruptedException if data read fails
128      */
129     Collection<Stream> getAvailableStreams() throws InterruptedException, ExecutionException {
130         final Optional<NormalizedNode<?, ?>> streams;
131         try (DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
132             streams = tx.read(LogicalDatastoreType.OPERATIONAL, STREAMS_PATH).get();
133         }
134         if (streams.isPresent()) {
135             Streams streams1 = (Streams) serializer.fromNormalizedNode(STREAMS_PATH, streams.get()).getValue();
136             return streams1.nonnullStream().values();
137         }
138         return Collections.emptyList();
139     }
140
141     EffectiveModelContext getSchemaContext() {
142         return schemaService.getGlobalContext();
143     }
144
145     /**
146      * Registers notification listener to receive a set of notifications.
147      *
148      * @param listener         listener
149      * @param notificationPath notification path
150      * @return ListenerRegistration
151      * @see DOMNotificationService#registerNotificationListener(DOMNotificationListener, SchemaPath...)
152      */
153     ListenerRegistration<DOMNotificationListener> registerNotificationListener(final DOMNotificationListener listener,
154                                                                                final SchemaPath notificationPath) {
155         return notificationService.registerNotificationListener(listener, notificationPath.asAbsolute());
156     }
157
158 }