BUG-8665: fix memory leak around RangeSets
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / ShardDataTreeChangePublisherActor.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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.datastore;
9
10 import akka.actor.Props;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.function.Consumer;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
15 import org.opendaylight.yangtools.concepts.ListenerRegistration;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
18
19 /**
20  * Actor used to generate and publish DataTreeChange notifications.
21  *
22  * @author Thomas Pantelis
23  */
24 public class ShardDataTreeChangePublisherActor
25         extends ShardDataTreeNotificationPublisherActor<ShardDataTreeChangeListenerPublisher> {
26
27     private ShardDataTreeChangePublisherActor(final String name, final String logContext) {
28         super(new DefaultShardDataTreeChangeListenerPublisher(logContext), name, logContext);
29     }
30
31     @Override
32     protected void handleReceive(Object message) {
33         if (message instanceof RegisterListener) {
34             RegisterListener reg = (RegisterListener)message;
35             LOG.debug("{}: Received {}", logContext(), reg);
36             if (reg.initialState.isPresent()) {
37                 DefaultShardDataTreeChangeListenerPublisher.notifySingleListener(reg.path, reg.listener,
38                         reg.initialState.get(), logContext());
39             }
40
41             publisher().registerTreeChangeListener(reg.path, reg.listener, Optional.absent(), reg.onRegistration);
42         } else {
43             super.handleReceive(message);
44         }
45     }
46
47     static Props props(final String name, final String logContext) {
48         return Props.create(ShardDataTreeChangePublisherActor.class, name, logContext);
49     }
50
51     static class RegisterListener {
52         private final YangInstanceIdentifier path;
53         private final DOMDataTreeChangeListener listener;
54         private final Optional<DataTreeCandidate> initialState;
55         private final Consumer<ListenerRegistration<DOMDataTreeChangeListener>> onRegistration;
56
57         RegisterListener(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
58                 final Optional<DataTreeCandidate> initialState,
59                 final Consumer<ListenerRegistration<DOMDataTreeChangeListener>> onRegistration) {
60             this.path = Preconditions.checkNotNull(path);
61             this.listener = Preconditions.checkNotNull(listener);
62             this.initialState = Preconditions.checkNotNull(initialState);
63             this.onRegistration = Preconditions.checkNotNull(onRegistration);
64         }
65
66         @Override
67         public String toString() {
68             return "RegisterListener [path=" + path + ", listener=" + listener + ", initialState present="
69                     + initialState.isPresent() + "]";
70         }
71     }
72 }