7ead9c26e251e643ac94e8cf18685133ce3e9b69
[mdsal.git] / replicate / mdsal-replicate-netty / src / main / java / org / opendaylight / mdsal / replicate / netty / SinkRequestHandler.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.mdsal.replicate.netty;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.ByteBufInputStream;
18 import io.netty.buffer.Unpooled;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelHandlerContext;
21 import io.netty.channel.SimpleChannelInboundHandler;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.opendaylight.mdsal.common.api.CommitInfo;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
28 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
29 import org.opendaylight.mdsal.replicate.common.DataTreeCandidateUtils;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.codec.binfmt.DataTreeCandidateInputOutput;
33 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 final class SinkRequestHandler extends SimpleChannelInboundHandler<ByteBuf> {
39     private static final Logger LOG = LoggerFactory.getLogger(SinkRequestHandler.class);
40
41     private final ReusableStreamReceiver receiver = ReusableImmutableNormalizedNodeStreamWriter.create();
42     private final List<ByteBuf> chunks = new ArrayList<>();
43     private final DOMDataTreeIdentifier tree;
44     private final DOMTransactionChain chain;
45
46     SinkRequestHandler(final DOMDataTreeIdentifier tree, final DOMTransactionChain chain) {
47         this.tree = requireNonNull(tree);
48         this.chain = requireNonNull(chain);
49     }
50
51     @Override
52     protected void channelRead0(final ChannelHandlerContext ctx, final ByteBuf msg) throws IOException {
53         verify(msg.isReadable(), "Empty message received");
54
55         final short msgType = msg.readUnsignedByte();
56         final Channel channel = ctx.channel();
57         LOG.trace("Channel {} received message type {}", channel, msgType);
58         switch (msgType) {
59             case Constants.MSG_EMPTY_DATA:
60                 handleEmptyData();
61                 break;
62             case Constants.MSG_DTC_CHUNK:
63                 chunks.add(msg);
64                 break;
65             case Constants.MSG_DTC_APPLY:
66                 handleDtcApply();
67                 break;
68             default:
69                 throw new IllegalStateException("Unexpected message type " + msgType);
70         }
71     }
72
73     private void handleEmptyData() {
74         final DOMDataTreeWriteTransaction tx = chain.newWriteOnlyTransaction();
75         tx.delete(tree.getDatastoreType(), tree.getRootIdentifier());
76         commit(tx);
77     }
78
79     private void handleDtcApply() throws IOException {
80         checkState(!chunks.isEmpty(), "No chunks to apply");
81
82         final ByteBuf bufs = Unpooled.wrappedBuffer(chunks.toArray(new ByteBuf[0]));
83         chunks.clear();
84
85         final DataTreeCandidate candidate;
86         try (ByteBufInputStream stream = new ByteBufInputStream(bufs)) {
87             candidate = DataTreeCandidateInputOutput.readDataTreeCandidate(NormalizedNodeDataInput.newDataInput(stream),
88                 receiver);
89         }
90
91         final DOMDataTreeWriteTransaction tx = chain.newWriteOnlyTransaction();
92         DataTreeCandidateUtils.applyToTransaction(tx, tree.getDatastoreType(), candidate);
93         commit(tx);
94     }
95
96     private static void commit(final DOMDataTreeWriteTransaction tx) {
97         tx.commit().addCallback(new FutureCallback<CommitInfo>() {
98             @Override
99             public void onSuccess(final CommitInfo result) {
100                 LOG.trace("Transaction committed with {}", result);
101             }
102
103             @Override
104             public void onFailure(final Throwable cause) {
105                 // Handled by transaction chain listener
106             }
107         }, MoreExecutors.directExecutor());
108     }
109 }