Introduce restconf.server.{api,spi,mdsal}
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / mdsal / MdsalRestconfStreamRegistry.java
1 /*
2  * Copyright © 2019 FRINX s.r.o. 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;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Map;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
18 import org.opendaylight.restconf.server.spi.AbstractRestconfStreamRegistry;
19 import org.opendaylight.restconf.server.spi.RestconfStream;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.RestconfState;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.Streams;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.streams.Stream;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Reference;
30
31 /**
32  * This singleton class is responsible for creation, removal and searching for {@link RestconfStream}s.
33  */
34 @Singleton
35 @Component(factory = MdsalRestconfStreamRegistry.FACTORY_NAME, service = RestconfStream.Registry.class)
36 public final class MdsalRestconfStreamRegistry extends AbstractRestconfStreamRegistry {
37     public static final String FACTORY_NAME = "org.opendaylight.restconf.nb.rfc8040.streams.ListenersBroker";
38
39     private static final YangInstanceIdentifier RESTCONF_STATE_STREAMS = YangInstanceIdentifier.of(
40         NodeIdentifier.create(RestconfState.QNAME),
41         NodeIdentifier.create(Streams.QNAME),
42         NodeIdentifier.create(Stream.QNAME));
43     private static final String USE_WEBSOCKETS_PROP = ".useWebsockets";
44
45     private final DOMDataBroker dataBroker;
46
47     public MdsalRestconfStreamRegistry(final DOMDataBroker dataBroker, final boolean useWebsockets) {
48         super(useWebsockets);
49         this.dataBroker = requireNonNull(dataBroker);
50     }
51
52     @Inject
53     public MdsalRestconfStreamRegistry(final DOMDataBroker dataBroker) {
54         this(dataBroker, false);
55     }
56
57     @Activate
58     public MdsalRestconfStreamRegistry(@Reference final DOMDataBroker dataBroker, final Map<String, ?> props) {
59         this(dataBroker, (boolean) requireNonNull(props.get(USE_WEBSOCKETS_PROP)));
60     }
61
62     public static Map<String, ?> props(final boolean useSSE) {
63         return Map.of(USE_WEBSOCKETS_PROP, !useSSE);
64     }
65
66     @Override
67     protected ListenableFuture<?> putStream(final MapEntryNode stream) {
68         // Now issue a put operation
69         final var tx = dataBroker.newWriteOnlyTransaction();
70         tx.put(LogicalDatastoreType.OPERATIONAL, RESTCONF_STATE_STREAMS.node(stream.name()), stream);
71         return tx.commit();
72     }
73
74     @Override
75     protected ListenableFuture<?> deleteStream(final NodeIdentifierWithPredicates streamName) {
76         // Now issue a delete operation while the name is still protected by being associated in the map.
77         final var tx = dataBroker.newWriteOnlyTransaction();
78         tx.delete(LogicalDatastoreType.OPERATIONAL, RESTCONF_STATE_STREAMS.node(streamName));
79         return tx.commit();
80     }
81 }