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