Deprecate messagebus-netconf
[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 @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");
56
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;
64
65     NetconfEventSourceMount(final BindingNormalizedNodeSerializer serializer, final Node node,
66             final DOMMountPoint mountPoint) {
67         this.serializer = requireNonNull(serializer);
68         this.node = node;
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);
74     }
75
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();
80     }
81
82     Node getNode() {
83         return node;
84     }
85
86     String getNodeId() {
87         return nodeId;
88     }
89
90     /**
91      * Invokes create-subscription rpc on mounted device stream. If lastEventTime is provided and stream supports
92      * replay,
93      * rpc will be invoked with start time parameter.
94      *
95      * @param stream        stream
96      * @param lastEventTime last event time
97      * @return rpc result
98      */
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));
107         }
108         final CreateSubscriptionInput input = inputBuilder.build();
109         final ContainerNode nnInput = serializer.toNormalizedNodeRpcData(input);
110         return rpcService.invokeRpc(CREATE_SUBSCRIPTION, nnInput);
111     }
112
113     /**
114      * Invokes create-subscription rpc on mounted device stream.
115      *
116      * @param stream stream
117      * @return rpc result
118      */
119     ListenableFuture<? extends DOMRpcResult> invokeCreateSubscription(final Stream stream) {
120         return invokeCreateSubscription(stream, Optional.empty());
121     }
122
123     /**
124      * Returns list of streams available on device.
125      *
126      * @return list of streams
127      * @throws ExecutionException if data read fails
128      * @throws InterruptedException if data read fails
129      */
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();
134         }
135         if (streams.isPresent()) {
136             Streams streams1 = (Streams) serializer.fromNormalizedNode(STREAMS_PATH, streams.get()).getValue();
137             return streams1.nonnullStream().values();
138         }
139         return Collections.emptyList();
140     }
141
142     EffectiveModelContext getSchemaContext() {
143         return schemaService.getGlobalContext();
144     }
145
146     /**
147      * Registers notification listener to receive a set of notifications.
148      *
149      * @param listener         listener
150      * @param notificationPath notification path
151      * @return ListenerRegistration
152      * @see DOMNotificationService#registerNotificationListener(DOMNotificationListener, SchemaPath...)
153      */
154     ListenerRegistration<DOMNotificationListener> registerNotificationListener(final DOMNotificationListener listener,
155                                                                                final SchemaPath notificationPath) {
156         return notificationService.registerNotificationListener(listener, notificationPath.asAbsolute());
157     }
158
159 }