BUG 2134 : Make persistence configurable at the datastore level
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / common / actor / AbstractUntypedPersistentActor.java
1 /*
2  * Copyright (c) 2014 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
9 package org.opendaylight.controller.cluster.common.actor;
10
11 import akka.event.Logging;
12 import akka.event.LoggingAdapter;
13 import akka.japi.Procedure;
14 import akka.persistence.SnapshotSelectionCriteria;
15 import akka.persistence.UntypedPersistentActor;
16 import org.opendaylight.controller.cluster.DataPersistenceProvider;
17
18 public abstract class AbstractUntypedPersistentActor extends UntypedPersistentActor {
19
20     protected final LoggingAdapter LOG =
21         Logging.getLogger(getContext().system(), this);
22
23     public AbstractUntypedPersistentActor() {
24         if(LOG.isDebugEnabled()) {
25             LOG.debug("Actor created {}", getSelf());
26         }
27         getContext().
28             system().
29             actorSelection("user/termination-monitor").
30             tell(new Monitor(getSelf()), getSelf());
31
32     }
33
34
35     @Override public void onReceiveCommand(Object message) throws Exception {
36         final String messageType = message.getClass().getSimpleName();
37         if(LOG.isDebugEnabled()) {
38             LOG.debug("Received message {}", messageType);
39         }
40         handleCommand(message);
41         if(LOG.isDebugEnabled()) {
42             LOG.debug("Done handling message {}", messageType);
43         }
44
45     }
46
47     @Override public void onReceiveRecover(Object message) throws Exception {
48         final String messageType = message.getClass().getSimpleName();
49         if(LOG.isDebugEnabled()) {
50             LOG.debug("Received message {}", messageType);
51         }
52         handleRecover(message);
53         if(LOG.isDebugEnabled()) {
54             LOG.debug("Done handling message {}", messageType);
55         }
56
57     }
58
59     protected abstract void handleRecover(Object message) throws Exception;
60
61     protected abstract void handleCommand(Object message) throws Exception;
62
63     protected void ignoreMessage(Object message) {
64         LOG.debug("Unhandled message {} ", message);
65     }
66
67     protected void unknownMessage(Object message) throws Exception {
68         if(LOG.isDebugEnabled()) {
69             LOG.debug("Received unhandled message {}", message);
70         }
71         unhandled(message);
72     }
73
74     protected class PersistentDataProvider implements DataPersistenceProvider {
75
76         public PersistentDataProvider(){
77
78         }
79
80         @Override
81         public boolean isRecoveryApplicable() {
82             return true;
83         }
84
85         @Override
86         public <T> void persist(T o, Procedure<T> procedure) {
87             AbstractUntypedPersistentActor.this.persist(o, procedure);
88         }
89
90         @Override
91         public void saveSnapshot(Object o) {
92             AbstractUntypedPersistentActor.this.saveSnapshot(o);
93         }
94
95         @Override
96         public void deleteSnapshots(SnapshotSelectionCriteria criteria) {
97             AbstractUntypedPersistentActor.this.deleteSnapshots(criteria);
98         }
99
100         @Override
101         public void deleteMessages(long sequenceNumber) {
102             AbstractUntypedPersistentActor.this.deleteMessages(sequenceNumber);
103         }
104     }
105
106     protected class NonPersistentDataProvider implements DataPersistenceProvider {
107
108         public NonPersistentDataProvider(){
109
110         }
111
112         @Override
113         public boolean isRecoveryApplicable() {
114             return false;
115         }
116
117         @Override
118         public <T> void persist(T o, Procedure<T> procedure) {
119             try {
120                 procedure.apply(o);
121             } catch (Exception e) {
122                 LOG.error(e, "An unexpected error occurred");
123             }
124         }
125
126         @Override
127         public void saveSnapshot(Object o) {
128         }
129
130         @Override
131         public void deleteSnapshots(SnapshotSelectionCriteria criteria) {
132
133         }
134
135         @Override
136         public void deleteMessages(long sequenceNumber) {
137
138         }
139     }
140 }