Convert sal-distributed-datastore to OSGi DS
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / sharding / OSGiDistributedShardedDOMDataTree.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.controller.cluster.sharding;
9
10 import com.google.common.collect.ClassToInstanceMap;
11 import java.util.Collection;
12 import java.util.concurrent.CompletionStage;
13 import org.opendaylight.controller.cluster.ActorSystemProvider;
14 import org.opendaylight.controller.cluster.access.concepts.MemberName;
15 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
17 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
18 import org.opendaylight.mdsal.dom.api.DOMDataTreeLoopException;
19 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
21 import org.opendaylight.mdsal.dom.api.DOMDataTreeServiceExtension;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeShard;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingConflictException;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeShardingService;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.osgi.service.component.annotations.Reference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Component(immediate = true, property = "type=default")
34 public final class OSGiDistributedShardedDOMDataTree
35         implements DOMDataTreeService, DOMDataTreeShardingService, DistributedShardFactory {
36     private static final Logger LOG = LoggerFactory.getLogger(OSGiDistributedShardedDOMDataTree.class);
37
38     @Reference
39     ActorSystemProvider actorSystemProvider = null;
40     @Reference(target = "(type=distributed-config)")
41     DistributedDataStoreInterface configDatastore = null;
42     @Reference(target = "(type=distributed-operational)")
43     DistributedDataStoreInterface operDatastore = null;
44
45     private DistributedShardedDOMDataTree delegate;
46
47     @Override
48     public DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
49         return delegate.createProducer(subtrees);
50     }
51
52     @Override
53     public ClassToInstanceMap<DOMDataTreeServiceExtension> getExtensions() {
54         return delegate.getExtensions();
55     }
56
57     @Override
58     public CompletionStage<DistributedShardRegistration> createDistributedShard(final DOMDataTreeIdentifier prefix,
59             final Collection<MemberName> replicaMembers) throws DOMDataTreeShardingConflictException {
60         return delegate.createDistributedShard(prefix, replicaMembers);
61     }
62
63     @Override
64     public <T extends DOMDataTreeShard> ListenerRegistration<T> registerDataTreeShard(
65             final DOMDataTreeIdentifier prefix, final T shard, final DOMDataTreeProducer producer)
66             throws DOMDataTreeShardingConflictException {
67         return delegate.registerDataTreeShard(prefix, shard, producer);
68     }
69
70     @Override
71     public <T extends DOMDataTreeListener> ListenerRegistration<T> registerListener(final T listener,
72             final Collection<DOMDataTreeIdentifier> subtrees, final boolean allowRxMerges,
73             final Collection<DOMDataTreeProducer> producers) throws DOMDataTreeLoopException {
74         return delegate.registerListener(listener, subtrees, allowRxMerges, producers);
75     }
76
77     @Activate
78     void activate() {
79         LOG.info("Distributed DOM Data Tree Service starting");
80         delegate = new DistributedShardedDOMDataTree(actorSystemProvider, operDatastore, configDatastore);
81         delegate.init();
82         LOG.info("Distributed DOM Data Tree Service started");
83     }
84
85     @Deactivate
86     void deactivate() {
87         LOG.info("Distributed DOM Data Tree Service stopping");
88         // TODO: this needs a shutdown hook, I think
89         delegate = null;
90         LOG.info("Distributed DOM Data Tree Service stopped");
91     }
92 }