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