dc5201a419d73081bd79dbfca53f92bc33808ed7
[mdsal.git] / replicate / mdsal-replicate-netty / src / main / java / org / opendaylight / mdsal / replicate / netty / SourceRequestHandler.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.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufInputStream;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelHandlerContext;
17 import io.netty.channel.SimpleChannelInboundHandler;
18 import java.io.IOException;
19 import java.util.List;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
22 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
26 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Final inbound handler on source side. Handles requests coming from sink and reacts to them.
32  */
33 final class SourceRequestHandler extends SimpleChannelInboundHandler<ByteBuf> {
34     private static final Logger LOG = LoggerFactory.getLogger(SourceRequestHandler.class);
35
36     private final DOMDataTreeChangeService dtcs;
37
38     private ListenerRegistration<?> reg;
39
40     SourceRequestHandler(final DOMDataTreeChangeService dtcs) {
41         this.dtcs = requireNonNull(dtcs);
42     }
43
44     @Override
45     public void channelInactive(final ChannelHandlerContext ctx) {
46         LOG.info("Channel {} going inactive", ctx.channel());
47         if (reg != null) {
48             reg.close();
49             reg = null;
50         }
51         ctx.fireChannelInactive();
52     }
53
54     @Override
55     protected void channelRead0(final ChannelHandlerContext ctx, final ByteBuf msg) throws IOException {
56         verify(msg.isReadable(), "Empty message received");
57
58         final short msgType = msg.readUnsignedByte();
59         final Channel channel = ctx.channel();
60         LOG.trace("Channel {} received message type {}", channel, msgType);
61         switch (msgType) {
62             case Constants.MSG_SUBSCRIBE_REQ:
63                 subscribe(channel, msg);
64                 break;
65             case Constants.MSG_PONG:
66                 break;
67             default:
68                 throw new IllegalStateException("Unexpected message type " + msgType);
69         }
70     }
71
72     @Override
73     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
74         LOG.warn("Closing channel {} due to an error", ctx.channel(), cause);
75         ctx.close();
76     }
77
78     private void subscribe(final Channel channel, final ByteBuf msg) throws IOException {
79         verify(reg == null, "Unexpected subscription when already subscribed");
80
81         final DOMDataTreeIdentifier dataTree;
82         try (ByteBufInputStream input = new ByteBufInputStream(msg)) {
83             final NormalizedNodeDataInput normalizedInput = NormalizedNodeDataInput.newDataInput(input);
84
85             dataTree = new DOMDataTreeIdentifier(LogicalDatastoreType.readFrom(normalizedInput),
86                 normalizedInput.readYangInstanceIdentifier());
87         }
88
89         LOG.info("Channel {} subscribing to {}", channel, dataTree);
90         reg = dtcs.registerDataTreeChangeListener(dataTree, new ClusteredDOMDataTreeChangeListener() {
91             @Override
92             public void onInitialData() {
93                 LOG.debug("Channel {} tree {} has empty data", channel, dataTree);
94                 channel.writeAndFlush(AbstractSourceMessage.empty());
95             }
96
97             @Override
98             public void onDataTreeChanged(final List<DataTreeCandidate> changes) {
99                 LOG.debug("Channel {} tree {} has {} changes", channel, dataTree, changes.size());
100                 channel.writeAndFlush(AbstractSourceMessage.of(changes));
101             }
102         });
103     }
104 }