2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.replicate.netty;
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;
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.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.stream.ReusableStreamReceiver;
33 import org.opendaylight.yangtools.yang.data.codec.binfmt.DataTreeCandidateInputOutput;
34 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
35 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 import org.opendaylight.yangtools.yang.data.impl.schema.ReusableImmutableNormalizedNodeStreamWriter;
37 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 final class SinkRequestHandler extends SimpleChannelInboundHandler<ByteBuf> {
43 private static final Logger LOG = LoggerFactory.getLogger(SinkRequestHandler.class);
44 private static final ContainerNode EMPTY_ROOT = ImmutableNodes.containerNode(SchemaContext.NAME);
46 private final ReusableStreamReceiver receiver = ReusableImmutableNormalizedNodeStreamWriter.create();
47 private final List<ByteBuf> chunks = new ArrayList<>();
48 private final DOMDataTreeIdentifier tree;
49 private final DOMTransactionChain chain;
51 SinkRequestHandler(final DOMDataTreeIdentifier tree, final DOMTransactionChain chain) {
52 this.tree = requireNonNull(tree);
53 this.chain = requireNonNull(chain);
57 protected void channelRead0(final ChannelHandlerContext ctx, final ByteBuf msg) throws IOException {
58 verify(msg.isReadable(), "Empty message received");
60 final short msgType = msg.readUnsignedByte();
61 final Channel channel = ctx.channel();
62 LOG.trace("Channel {} received message type {}", channel, msgType);
64 case Constants.MSG_EMPTY_DATA -> handleEmptyData();
65 case Constants.MSG_DTC_CHUNK -> chunks.add(msg.retain());
66 case Constants.MSG_DTC_APPLY -> handleDtcApply();
67 case Constants.MSG_PING -> {
68 LOG.trace("Received PING from Source, sending PONG");
69 ctx.channel().writeAndFlush(Constants.PONG);
71 default -> throw new IllegalStateException("Unexpected message type " + msgType);
75 private void handleEmptyData() {
76 final DOMDataTreeWriteTransaction tx = chain.newWriteOnlyTransaction();
78 if (tree.getRootIdentifier().isEmpty()) {
79 tx.put(tree.getDatastoreType(), YangInstanceIdentifier.of(), EMPTY_ROOT);
81 tx.delete(tree.getDatastoreType(), tree.getRootIdentifier());
86 private void handleDtcApply() throws IOException {
87 checkState(!chunks.isEmpty(), "No chunks to apply");
89 final ByteBuf bufs = Unpooled.wrappedBuffer(chunks.toArray(new ByteBuf[0]));
92 final DataTreeCandidate candidate;
93 try (ByteBufInputStream stream = new ByteBufInputStream(bufs)) {
94 candidate = DataTreeCandidateInputOutput.readDataTreeCandidate(NormalizedNodeDataInput.newDataInput(stream),
98 final DOMDataTreeWriteTransaction tx = chain.newWriteOnlyTransaction();
99 DataTreeCandidateUtils.applyToTransaction(tx, tree.getDatastoreType(), candidate);
103 private static void commit(final DOMDataTreeWriteTransaction tx) {
104 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
106 public void onSuccess(final CommitInfo result) {
107 LOG.trace("Transaction committed with {}", result);
111 public void onFailure(final Throwable cause) {
112 // Handled by transaction chain listener
114 }, MoreExecutors.directExecutor());