Bump upstreams for Silicon
[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.collect.ImmutableList;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.time.Instant;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.time.format.DateTimeFormatter;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.Optional;
22 import java.util.concurrent.ExecutionException;
23 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
27 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
28 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
29 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
30 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
31 import org.opendaylight.mdsal.dom.api.DOMRpcService;
32 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
33 import org.opendaylight.mdsal.dom.api.DOMService;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yangtools.concepts.ListenerRegistration;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
47 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
48 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
49
50 /**
51  * Facade of mounted netconf device.
52  */
53 class NetconfEventSourceMount {
54     private static final YangInstanceIdentifier STREAMS_PATH = YangInstanceIdentifier.builder().node(Netconf.QNAME)
55             .node(Streams.QNAME).build();
56     private static final QName CREATE_SUBSCRIPTION = QName.create(CreateSubscriptionInput.QNAME, "create-subscription");
57
58     private final DOMRpcService rpcService;
59     private final DOMNotificationService notificationService;
60     private final DOMDataBroker dataBroker;
61     private final Node node;
62     private final String nodeId;
63     private final BindingNormalizedNodeSerializer serializer;
64     private final DOMSchemaService schemaService;
65
66     NetconfEventSourceMount(final BindingNormalizedNodeSerializer serializer, final Node node,
67             final DOMMountPoint mountPoint) {
68         this.serializer = requireNonNull(serializer);
69         this.node = node;
70         this.nodeId = node.getNodeId().getValue();
71         this.rpcService = getService(mountPoint, DOMRpcService.class);
72         this.notificationService = getService(mountPoint, DOMNotificationService.class);
73         this.dataBroker = getService(mountPoint, DOMDataBroker.class);
74         this.schemaService = getService(mountPoint, DOMSchemaService.class);
75     }
76
77     private static <T extends DOMService> T getService(final DOMMountPoint mountPoint, final Class<T> service) {
78         final Optional<T> optional = mountPoint.getService(service);
79         Preconditions.checkState(optional.isPresent(), "Service not present on mount point: %s", service.getName());
80         return optional.get();
81     }
82
83     Node getNode() {
84         return node;
85     }
86
87     String getNodeId() {
88         return nodeId;
89     }
90
91     /**
92      * Invokes create-subscription rpc on mounted device stream. If lastEventTime is provided and stream supports
93      * replay,
94      * rpc will be invoked with start time parameter.
95      *
96      * @param stream        stream
97      * @param lastEventTime last event time
98      * @return rpc result
99      */
100     ListenableFuture<? extends DOMRpcResult> invokeCreateSubscription(final Stream stream,
101             final Optional<Instant> lastEventTime) {
102         final CreateSubscriptionInputBuilder inputBuilder = new CreateSubscriptionInputBuilder()
103                 .setStream(stream.getName());
104         if (lastEventTime.isPresent() && stream.isReplaySupport()) {
105             final ZonedDateTime dateTime = lastEventTime.get().atZone(ZoneId.systemDefault());
106             final String formattedDate = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dateTime);
107             inputBuilder.setStartTime(new DateAndTime(formattedDate));
108         }
109         final CreateSubscriptionInput input = inputBuilder.build();
110         final ContainerNode nnInput = serializer.toNormalizedNodeRpcData(input);
111         return rpcService.invokeRpc(CREATE_SUBSCRIPTION, nnInput);
112     }
113
114     /**
115      * Invokes create-subscription rpc on mounted device stream.
116      *
117      * @param stream stream
118      * @return rpc result
119      */
120     ListenableFuture<? extends DOMRpcResult> invokeCreateSubscription(final Stream stream) {
121         return invokeCreateSubscription(stream, Optional.empty());
122     }
123
124     /**
125      * Returns list of streams available on device.
126      *
127      * @return list of streams
128      * @throws ExecutionException if data read fails
129      * @throws InterruptedException if data read fails
130      */
131     Collection<Stream> getAvailableStreams() throws InterruptedException, ExecutionException {
132         final Optional<NormalizedNode<?, ?>> streams;
133         try (DOMDataTreeReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
134             streams = tx.read(LogicalDatastoreType.OPERATIONAL, STREAMS_PATH).get();
135         }
136         if (streams.isPresent()) {
137             Streams streams1 = (Streams) serializer.fromNormalizedNode(STREAMS_PATH, streams.get()).getValue();
138             return streams1.nonnullStream().values();
139         }
140         return Collections.emptyList();
141     }
142
143     EffectiveModelContext getSchemaContext() {
144         return schemaService.getGlobalContext();
145     }
146
147     /**
148      * Registers notification listener to receive a set of notifications.
149      *
150      * @param listener         listener
151      * @param notificationPath notification path
152      * @return ListenerRegistration
153      * @see DOMNotificationService#registerNotificationListener(DOMNotificationListener, SchemaPath...)
154      */
155     ListenerRegistration<DOMNotificationListener> registerNotificationListener(final DOMNotificationListener listener,
156                                                                                final SchemaPath notificationPath) {
157         return notificationService.registerNotificationListener(listener,
158             Absolute.of(ImmutableList.copyOf(notificationPath.getPathFromRoot())));
159     }
160
161 }