NetconfClientSession session = new NetconfClientSession(sessionListener, channel, sessId, caps);
final NetconfMessageToEXIEncoder exiEncoder = NetconfMessageToEXIEncoder.create(codec);
- final NetconfEXIToMessageDecoder exiDecoder = new NetconfEXIToMessageDecoder(codec);
+ final NetconfEXIToMessageDecoder exiDecoder = NetconfEXIToMessageDecoder.create(codec);
session.addExiHandlers(exiDecoder, exiEncoder);
session.stopExiCommunication();
throw new IllegalStateException("Cannot instantiate encoder for options", e);
}
- final NetconfEXIToMessageDecoder exiDecoder = new NetconfEXIToMessageDecoder(exiCodec);
- addExiHandlers(exiDecoder, exiEncoder);
+ final NetconfEXIToMessageDecoder exiDecoder;
+ try {
+ exiDecoder = NetconfEXIToMessageDecoder.create(exiCodec);
+ } catch (EXIOptionsException e) {
+ LOG.warn("Failed to instantiate EXI decodeer for {} on session {}", exiCodec, this, e);
+ throw new IllegalStateException("Cannot instantiate encoder for options", e);
+ }
+ addExiHandlers(exiDecoder, exiEncoder);
LOG.debug("Session {} EXI handlers added to pipeline", this);
}
private static final Logger LOG = LoggerFactory.getLogger(NetconfEXIToMessageDecoder.class);
private static final SAXTransformerFactory FACTORY = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
+ /**
+ * This class is not marked as shared, so it can be attached to only a single channel,
+ * which means that {@link #decode(ChannelHandlerContext, ByteBuf, List)}
+ * cannot be invoked concurrently. Hence we can reuse the reader.
+ */
+ private final EXIReader reader;
- private final NetconfEXICodec codec;
+ private NetconfEXIToMessageDecoder(final EXIReader reader) {
+ this.reader = Preconditions.checkNotNull(reader);
+ }
- public NetconfEXIToMessageDecoder(final NetconfEXICodec codec) {
- this.codec = Preconditions.checkNotNull(codec);
+ public static NetconfEXIToMessageDecoder create(final NetconfEXICodec codec) throws EXIOptionsException {
+ return new NetconfEXIToMessageDecoder(codec.getReader());
}
@Override
LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
}
- final EXIReader r = codec.getReader();
final TransformerHandler handler = FACTORY.newTransformerHandler();
- r.setContentHandler(handler);
+ reader.setContentHandler(handler);
final DOMResult domResult = new DOMResult();
handler.setResult(domResult);
try (final InputStream is = new ByteBufInputStream(in)) {
- r.parse(new InputSource(is));
+ // Performs internal reset before doing anything
+ reader.parse(new InputSource(is));
}
out.add(new NetconfMessage((Document) domResult.getNode()));
public void setUp() throws Exception {
final NetconfEXICodec codec = new NetconfEXICodec(new EXIOptions());
netconfMessageToEXIEncoder = NetconfMessageToEXIEncoder.create(codec);
- netconfEXIToMessageDecoder = new NetconfEXIToMessageDecoder(codec);
+ netconfEXIToMessageDecoder = NetconfEXIToMessageDecoder.create(codec);
msg = new NetconfMessage(XmlUtil.readXmlToDocument(msgAsString));
this.msgAsExi = msgToExi(msgAsString, codec);