Migrate nullness annotations
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / FrontendReadOnlyTransaction.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.datastore;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Optional;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionRequest;
16 import org.opendaylight.controller.cluster.access.commands.ExistsTransactionSuccess;
17 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionRequest;
18 import org.opendaylight.controller.cluster.access.commands.ModifyTransactionSuccess;
19 import org.opendaylight.controller.cluster.access.commands.PersistenceProtocol;
20 import org.opendaylight.controller.cluster.access.commands.ReadTransactionRequest;
21 import org.opendaylight.controller.cluster.access.commands.ReadTransactionSuccess;
22 import org.opendaylight.controller.cluster.access.commands.TransactionRequest;
23 import org.opendaylight.controller.cluster.access.commands.TransactionSuccess;
24 import org.opendaylight.controller.cluster.access.concepts.RequestEnvelope;
25 import org.opendaylight.controller.cluster.access.concepts.RequestException;
26 import org.opendaylight.controller.cluster.access.concepts.UnsupportedRequestException;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Read-only frontend transaction state as observed by the shard leader.
33  *
34  * @author Robert Varga
35  */
36 @NotThreadSafe
37 final class FrontendReadOnlyTransaction extends FrontendTransaction {
38     private static final Logger LOG = LoggerFactory.getLogger(FrontendReadOnlyTransaction.class);
39
40     private final ReadOnlyShardDataTreeTransaction openTransaction;
41
42     private FrontendReadOnlyTransaction(final AbstractFrontendHistory history,
43             final ReadOnlyShardDataTreeTransaction transaction) {
44         super(history, transaction.getIdentifier());
45         this.openTransaction = requireNonNull(transaction);
46     }
47
48     static FrontendReadOnlyTransaction create(final AbstractFrontendHistory history,
49             final ReadOnlyShardDataTreeTransaction transaction) {
50         return new FrontendReadOnlyTransaction(history, transaction);
51     }
52
53     // Sequence has already been checked
54     @Override
55     TransactionSuccess<?> doHandleRequest(final TransactionRequest<?> request, final RequestEnvelope envelope,
56             final long now) throws RequestException {
57         if (request instanceof ExistsTransactionRequest) {
58             return handleExistsTransaction((ExistsTransactionRequest) request);
59         } else if (request instanceof ReadTransactionRequest) {
60             return handleReadTransaction((ReadTransactionRequest) request);
61         } else if (request instanceof ModifyTransactionRequest) {
62             handleModifyTransaction((ModifyTransactionRequest) request, envelope, now);
63             return null;
64         } else {
65             LOG.warn("Rejecting unsupported request {}", request);
66             throw new UnsupportedRequestException(request);
67         }
68     }
69
70     @Override
71     void retire() {
72         // No-op
73     }
74
75     private void handleModifyTransaction(final ModifyTransactionRequest request, final RequestEnvelope envelope,
76             final long now) {
77         // The only valid request here is with abort protocol
78         final Optional<PersistenceProtocol> optProto = request.getPersistenceProtocol();
79         checkArgument(optProto.isPresent(), "Commit protocol is missing in %s", request);
80         checkArgument(optProto.get() == PersistenceProtocol.ABORT, "Unsupported commit protocol in %s", request);
81         openTransaction.abort(() -> recordAndSendSuccess(envelope, now,
82             new ModifyTransactionSuccess(request.getTarget(), request.getSequence())));
83     }
84
85     private ExistsTransactionSuccess handleExistsTransaction(final ExistsTransactionRequest request) {
86         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
87         return recordSuccess(request.getSequence(), new ExistsTransactionSuccess(openTransaction.getIdentifier(),
88             request.getSequence(), data.isPresent()));
89     }
90
91     private ReadTransactionSuccess handleReadTransaction(final ReadTransactionRequest request) {
92         final Optional<NormalizedNode<?, ?>> data = openTransaction.getSnapshot().readNode(request.getPath());
93         return recordSuccess(request.getSequence(), new ReadTransactionSuccess(openTransaction.getIdentifier(),
94             request.getSequence(), data));
95     }
96 }