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