Further warnings mitigation
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / mdsal / streams / dtcl / DataTreeChangeSource.java
1 /*
2  * Copyright (c) 2014, 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.restconf.server.mdsal.streams.dtcl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.collect.ImmutableMap;
15 import java.time.Instant;
16 import java.util.List;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.mdsal.dom.api.DOMDataBroker.DataTreeChangeExtension;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
22 import org.opendaylight.restconf.server.spi.DatabindProvider;
23 import org.opendaylight.restconf.server.spi.RestconfStream;
24 import org.opendaylight.restconf.server.spi.RestconfStream.EncodingName;
25 import org.opendaylight.restconf.server.spi.RestconfStream.Sink;
26 import org.opendaylight.restconf.server.spi.RestconfStream.Source;
27 import org.opendaylight.yangtools.concepts.Registration;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
30
31 /**
32  * A {@link RestconfStream} reporting changes on a particular data tree.
33  */
34 @VisibleForTesting
35 public final class DataTreeChangeSource extends Source<List<DataTreeCandidate>> {
36     private static final ImmutableMap<EncodingName, DataTreeCandidateFormatterFactory> ENCODINGS = ImmutableMap.of(
37         EncodingName.RFC8040_JSON, JSONDataTreeCandidateFormatter.FACTORY,
38         EncodingName.RFC8040_XML, XMLDataTreeCandidateFormatter.FACTORY);
39
40     private final @NonNull DataTreeChangeExtension changeService;
41     private final @NonNull DatabindProvider databindProvider;
42     private final @NonNull LogicalDatastoreType datastore;
43     private final @NonNull YangInstanceIdentifier path;
44
45     public DataTreeChangeSource(final DatabindProvider databindProvider, final DataTreeChangeExtension changeService,
46             final LogicalDatastoreType datastore, final YangInstanceIdentifier path) {
47         super(ENCODINGS);
48         this.databindProvider = requireNonNull(databindProvider);
49         this.changeService = requireNonNull(changeService);
50         this.datastore = requireNonNull(datastore);
51         this.path = requireNonNull(path);
52     }
53
54     @Override
55     protected Registration start(final Sink<List<DataTreeCandidate>> sink) {
56         return changeService.registerTreeChangeListener(DOMDataTreeIdentifier.of(datastore, path),
57             new DOMDataTreeChangeListener() {
58                 @Override
59                 public void onDataTreeChanged(final List<DataTreeCandidate> changes) {
60                     // FIXME: format one change at a time?
61                     sink.publish(databindProvider.currentDatabind().modelContext(), changes, Instant.now());
62                 }
63
64                 @Override
65                 public void onInitialData() {
66                     // No-op
67                 }
68             });
69     }
70
71     @Override
72     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
73         return super.addToStringAttributes(helper.add("path", path));
74     }
75 }