Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / sal / sal-core-api / src / main / java / org / opendaylight / controller / sal / core / api / data / DataCommitHandler.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.controller.sal.core.api.data;\r
9 \r
10 import java.util.Set;\r
11 \r
12 import org.opendaylight.controller.sal.common.DataStoreIdentifier;\r
13 import org.opendaylight.controller.sal.core.api.Provider;\r
14 import org.opendaylight.controller.yang.common.RpcResult;\r
15 \r
16 \r
17 /**\r
18  * Two phase commit handler (cohort) of the two-phase commit protocol of data.\r
19  * \r
20  * <p>\r
21  * The provider should expose the implementation of DataCommitHandler if it's\r
22  * functionality depends on any subset of data stored in data repositories, in\r
23  * order to participate in {@link DataBrokerService#commit(DataStoreIdentifier)\r
24  * operation.\r
25  * \r
26  * <p>\r
27  * Operations of two-phase commit handlers should not change data in data store,\r
28  * this is responsibility of the coordinator (broker or some component of the\r
29  * broker).\r
30  * \r
31  * The commit handlers are responsible for changing the internal state of the\r
32  * provider to reflect the commited changes in data.\r
33  * \r
34  * <h3>Two-phase commit</h3>\r
35  * \r
36  * <h4>Commit Request Phase</h4>\r
37  * \r
38  * <ol>\r
39  * <li> <code>Consumer</code> edits data by invocation of\r
40  * <code>DataBrokerService.editCandidateData(DataStoreIdentifier, CompositeNodeModification)</code>\r
41  * <li> <code>Consumer</code> starts a commit by invoking\r
42  * <code>DataBrokerService.commit(DataStoreIdentifier)</code>\r
43  * <li> <code>Broker</code> retrieves a list of all registered\r
44  * <code>DataCommitHandlers</code>\r
45  * <li>For each <code>DataCommitHandler</code>\r
46  * <ol>\r
47  * <li><code>Broker</code> invokes a\r
48  * <code>DataCommitHandler.requestCommit(DataStoreIdentifier)</code> operation.\r
49  * <li><code>DataCommitHandler</code> returns a <code>RpcResult</code> with\r
50  * <code>CommitTransaction</code>\r
51  * <li>If the result was successful, broker adds <code>CommitTransaction</code>\r
52  * to the list of opened transactions. If not, brokers stops a commit request\r
53  * phase and starts a rollback phase.\r
54  * </ol>\r
55  * <li><code>Broker</code> starts a commit finish phase\r
56  * </ol>\r
57  * \r
58  * <h4>Commit Finish Phase</h4>\r
59  * \r
60  * <ol>\r
61  * <li>For each <code>CommitTransaction</code> from Commit Request phase\r
62  * <ol>\r
63  * <li><code>Broker</code> broker invokes a\r
64  * <code>CommitTransaction.finish()</code>\r
65  * <li>The provider finishes a commit (applies the change) and returns an\r
66  * <code>RpcResult</code>.\r
67  * </ol>\r
68  * <li>\r
69  * <ul>\r
70  * <li>If all returned results means successful, the brokers end two-phase\r
71  * commit by returning a success commit result to the Consumer.\r
72  * <li>If error occured, the broker starts a commit rollback phase.\r
73  * </ul>\r
74  * </ol>\r
75  * \r
76  * <h4>Commit Rollback Phase</h4>\r
77  * <li>For each <code>CommitTransaction</code> from Commit Request phase\r
78  * <ol>\r
79  * <li><code>Broker</code>\r
80  * \r
81  * broker invokes a {@link CommitTransaction#finish()}\r
82  * <li>The provider rollbacks a commit and returns an {@link RpcResult} of\r
83  * rollback. </ol>\r
84  * <li>Broker returns a error result to the consumer.\r
85  * \r
86  * \r
87  * <h3>Registration of functionality</h3>\r
88  * The registration could be done by :\r
89  * <ul>\r
90  * <li>returning an instance of implementation in the return value of\r
91  * {@link Provider#getProviderFunctionality()}\r
92  * <li>passing an instance of implementation and {@link DataStoreIdentifier} of\r
93  * rpc as arguments to the\r
94  * {@link DataProviderService#addCommitHandler(DataStoreIdentifier, DataCommitHandler)}\r
95  * </ul>\r
96  * \r
97  * \r
98  */\r
99 public interface DataCommitHandler extends Provider.ProviderFunctionality {\r
100 \r
101     /**\r
102      * A set of Data Stores supported by implementation.\r
103      * \r
104      * The set of {@link DataStoreIdentifier}s which identifies target data\r
105      * stores which are supported by this commit handler. This set is used, when\r
106      * {@link Provider} is registered to the SAL, to register and expose the\r
107      * commit handler functionality to affected data stores.\r
108      * \r
109      * @return Set of Data Store identifiers\r
110      */\r
111     Set<DataStoreIdentifier> getSupportedDataStores();\r
112 \r
113     /**\r
114      * The provider (commit handler) starts a commit transaction.\r
115      * \r
116      * <p>\r
117      * The commit handler (provider) prepares an commit scenario, rollback\r
118      * scenario and validates data.\r
119      * \r
120      * <p>\r
121      * If the provider is aware that at this point the commit would not be\r
122      * successful, the transaction is not created, but list of errors which\r
123      * prevented the start of transaction are returned.\r
124      * \r
125      * @param store\r
126      * @return Transaction object representing this commit, errors otherwise.\r
127      */\r
128     RpcResult<CommitTransaction> requestCommit(DataStoreIdentifier store);\r
129 \r
130     public interface CommitTransaction {\r
131         /**\r
132          * \r
133          * @return Data store affected by the transaction\r
134          */\r
135         DataStoreIdentifier getDataStore();\r
136 \r
137         /**\r
138          * Returns the handler associated with this transaction.\r
139          * \r
140          * @return Handler\r
141          */\r
142         DataCommitHandler getHandler();\r
143 \r
144         /**\r
145          * \r
146          * Finishes a commit.\r
147          * \r
148          * The provider (commit handler) should apply all changes to its state\r
149          * which are a result of data change-\r
150          * \r
151          * @return\r
152          */\r
153         RpcResult<Void> finish() throws IllegalStateException;\r
154 \r
155         /**\r
156          * Rollbacks a commit.\r
157          * \r
158          * @return\r
159          * @throws IllegalStateException\r
160          *             If the method is invoked after {@link #finish()}\r
161          */\r
162         RpcResult<Void> rollback() throws IllegalStateException;\r
163     }\r
164 }\r