Move byte-based serialization method
[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.mdsal.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 final class ShardDataTreeChangePublisherActor
25         extends ShardDataTreeNotificationPublisherActor<DefaultShardDataTreeChangeListenerPublisher> {
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(final 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             } else {
40                 reg.listener.onInitialData();
41             }
42
43             publisher().registerTreeChangeListener(reg.path, reg.listener, reg.onRegistration);
44         } else {
45             super.handleReceive(message);
46         }
47     }
48
49     static Props props(final String name, final String logContext) {
50         return Props.create(ShardDataTreeChangePublisherActor.class, name, logContext);
51     }
52
53     static class RegisterListener {
54         private final YangInstanceIdentifier path;
55         private final DOMDataTreeChangeListener listener;
56         private final Optional<DataTreeCandidate> initialState;
57         private final Consumer<ListenerRegistration<DOMDataTreeChangeListener>> onRegistration;
58
59         RegisterListener(final YangInstanceIdentifier path, final DOMDataTreeChangeListener listener,
60                 final Optional<DataTreeCandidate> initialState,
61                 final Consumer<ListenerRegistration<DOMDataTreeChangeListener>> onRegistration) {
62             this.path = Preconditions.checkNotNull(path);
63             this.listener = Preconditions.checkNotNull(listener);
64             this.initialState = Preconditions.checkNotNull(initialState);
65             this.onRegistration = Preconditions.checkNotNull(onRegistration);
66         }
67
68         @Override
69         public String toString() {
70             return "RegisterListener [path=" + path + ", listener=" + listener + ", initialState present="
71                     + initialState.isPresent() + "]";
72         }
73     }
74 }