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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.