Fix followerDistributedDataStore tear down
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / commands / PersistenceProtocol.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.controller.cluster.access.commands;
9
10 import com.google.common.annotations.Beta;
11 import java.io.DataInput;
12 import java.io.DataOutput;
13 import java.io.IOException;
14 import org.opendaylight.yangtools.concepts.WritableObject;
15
16 /**
17  * Enumeration of transaction persistence protocols. These govern which protocol is executed between the frontend
18  * and backend to drive persistence of a particular transaction.
19  *
20  * @author Robert Varga
21  */
22 @Beta
23 public enum PersistenceProtocol implements WritableObject {
24     /**
25      * Abort protocol. The transaction has been aborted on the frontend and its effects should not be visible
26      * in the global history. This is a simple request/reply protocol.
27      */
28     ABORT {
29         @Override
30         byte byteValue() {
31             return 1;
32         }
33     },
34     /**
35      * Simple commit protocol. The transaction should be committed to the global history. The receiving backend
36      * is the only entity which needs to persist its effects, hence a simple request/reply protocol is sufficient.
37      */
38     SIMPLE {
39         @Override
40         byte byteValue() {
41             return 2;
42         }
43     },
44     /**
45      * Three-phase commit protocol (3PC). The transaction should be committed to the global history, but it is a part
46      * of a transaction spanning multiple entities and coordination is needed to drive persistence.
47      */
48     THREE_PHASE {
49         @Override
50         byte byteValue() {
51             return 3;
52         }
53     },
54     /**
55      * Transaction is ready. This is not a really a persistence protocol, but an indication that frontend has
56      * completed modifications on the transaction and considers it ready, without deciding the actual commit protocol.
57      */
58     READY {
59         @Override
60         byte byteValue() {
61             return 4;
62         }
63     };
64
65     @Override
66     public final void writeTo(final DataOutput out) throws IOException {
67         out.writeByte(byteValue());
68     }
69
70     public static PersistenceProtocol readFrom(final DataInput in) throws IOException {
71         return valueOf(in.readByte());
72     }
73
74     abstract byte byteValue();
75
76     static int byteValue(final PersistenceProtocol finish) {
77         return finish == null ? 0 : finish.byteValue();
78     }
79
80     static PersistenceProtocol valueOf(final byte value) {
81         switch (value) {
82             case 0:
83                 return null;
84             case 1:
85                 return ABORT;
86             case 2:
87                 return SIMPLE;
88             case 3:
89                 return THREE_PHASE;
90             case 4:
91                 return READY;
92             default:
93                 throw new IllegalArgumentException("Unhandled byte value " + value);
94         }
95     }
96 }