private static NetconfMessage getNetconfMessage(final String additionalHeader, final Document doc) throws NetconfDocumentedException { NetconfMessage msg = new NetconfMessage(doc); if(NetconfHelloMessage.isHelloMessage(msg)) { if (additionalHeader != null) { return new NetconfHelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader)); } else { return new NetconfHelloMessage(doc); } } return msg; }
@Override public void close() { channel.close(); up = false; sessionListener.onSessionTerminated(thisInstance(), new NetconfTerminationReason("Session closed")); }
public static boolean isOKMessage(NetconfMessage message) throws NetconfDocumentedException { return isOKMessage(message.getDocument()); }
public static void sendErrorMessage(final NetconfSession session, final DocumentedException sendErrorException, final NetconfMessage incommingMessage) { final Document errorDocument = createDocument(sendErrorException); if (LOG.isTraceEnabled()) { LOG.trace("Sending error {}", XmlUtil.toString(errorDocument)); } tryToCopyAttributes(incommingMessage.getDocument(), errorDocument, sendErrorException); ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument)); f.addListener(new SendErrorVerifyingListener(sendErrorException)); }
public static void sendErrorMessage(final NetconfSession session, final DocumentedException sendErrorException) { LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException); final Document errorDocument = createDocument(sendErrorException); ChannelFuture f = session.sendMessage(new NetconfMessage(errorDocument)); f.addListener(new SendErrorVerifyingListener(sendErrorException)); }
public static boolean isOKMessage(XmlElement xmlElement) throws NetconfDocumentedException { if(xmlElement.getChildElements().size() != 1) { return false; } try { return xmlElement.getOnlyChildElement().getName().equals(XmlNetconfConstants.OK); } catch (DocumentedException e) { throw new NetconfDocumentedException(e); } }
public static void sendErrorMessage(final Channel channel, final DocumentedException sendErrorException) { LOG.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException); final Document errorDocument = createDocument(sendErrorException); ChannelFuture f = channel.writeAndFlush(new NetconfMessage(errorDocument)); f.addListener(new SendErrorVerifyingListener(sendErrorException)); }
private boolean shouldUseChunkFraming(final Document doc) { return containsBase11Capability(doc) && containsBase11Capability(sessionPreferences.getHelloMessage().getDocument()); }
@Override protected void handleMessage(final NetconfMessage netconfMessage) { LOG.debug("handling incoming message"); sessionListener.onMessage(thisInstance(), netconfMessage); }
@Override protected void sessionUp() { LOG.debug("Session {} up", toString()); sessionListener.onSessionUp(thisInstance()); this.up = true; }
@Override protected void endOfInput() { LOG.debug("Session {} end of input detected while session was in state {}", toString(), isUp() ? "up" : "initialized"); if (isUp()) { this.sessionListener.onSessionDown(thisInstance(), new IOException("End of input detected. Close the session.")); } }
@Override public java.lang.AutoCloseable createInstance() { final NetconfServerDispatcher dispatch = getDispatcherDependency(); final ChannelFuture tcpServer = dispatch.createServer(getInetAddress()); tcpServer.addListener(new GenericFutureListener<ChannelFuture>() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isDone() && future.isSuccess()) { LOG.info("Netconf TCP endpoint started successfully at {}", getInetAddress()); } else { LOG.warn("Unable to start TCP netconf server at {}", getInetAddress(), future.cause()); throw new RuntimeException("Unable to start TCP netconf server", future.cause()); } } }); return new NetconfServerCloseable(tcpServer); }
private void start() { final NetconfHelloMessage helloMessage = this.sessionPreferences.getHelloMessage(); LOG.debug("Session negotiation started with hello message {} on channel {}", helloMessage, channel);
public static boolean isErrorMessage(NetconfMessage message) throws NetconfDocumentedException { return isErrorMessage(message.getDocument()); }
public static boolean isErrorMessage(XmlElement xmlElement) throws NetconfDocumentedException { if(xmlElement.getChildElements().size() != 1) { return false; } try { return xmlElement.getOnlyChildElement().getName().equals(DocumentedException.RPC_ERROR); } catch (DocumentedException e) { throw new NetconfDocumentedException(e); } }
out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in)))); } else { LOG.debug("No more content in incoming buffer.");
@Override @VisibleForTesting public void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws IOException, TransformerException { LOG.trace("Sent to encode : {}", msg); if (clientId.isPresent()) { Comment comment = msg.getDocument().createComment("clientId:" + clientId.get()); msg.getDocument().appendChild(comment); } try (OutputStream os = new ByteBufOutputStream(out)) { // Wrap OutputStreamWriter with BufferedWriter as suggested in javadoc for OutputStreamWriter // Using custom BufferedWriter that does not provide newLine method as performance improvement // see javadoc for BufferedWriter StreamResult result = new StreamResult(new BufferedWriter(new OutputStreamWriter(os))); DOMSource source = new DOMSource(msg.getDocument()); ThreadLocalTransformers.getPrettyTransformer().transform(source, result); } } }
private Optional<DataSchemaNode> getSchemaNodeFromNamespace(final String namespace, final XmlElement element) throws DocumentedException{ Optional<DataSchemaNode> dataSchemaNode = Optional.absent(); try { //returns module with newest revision since findModuleByNamespace returns a set of modules and we only need the newest one final Module module = schemaContext.getCurrentContext().findModuleByNamespaceAndRevision(new URI(namespace), null); if (module == null) { // no module is present with this namespace throw new NetconfDocumentedException("Unable to find module by namespace: " + namespace, ErrorType.application, ErrorTag.unknown_namespace, ErrorSeverity.error); } DataSchemaNode schemaNode = module.getDataChildByName(QName.create(module.getQNameModule(), element.getName())); if (schemaNode != null) { dataSchemaNode = Optional.of(schemaNode); } else { throw new DocumentedException("Unable to find node with namespace: " + namespace + "in module: " + module.toString(), ErrorType.application, ErrorTag.unknown_namespace, ErrorSeverity.error); } } catch (URISyntaxException e) { LOG.debug("Unable to create URI for namespace : {}", namespace); } return dataSchemaNode; }
@Override protected void encode(final ChannelHandlerContext ctx, final NetconfMessage msg, final ByteBuf out) throws EXIOptionsException, IOException, TransformerException, TransmogrifierException { LOG.trace("Sent to encode : {}", msg); try (final OutputStream os = new ByteBufOutputStream(out)) { transmogrifier.setOutputStream(os); final ContentHandler handler = transmogrifier.getSAXTransmogrifier(); final Transformer transformer = ThreadLocalTransformers.getDefaultTransformer(); transformer.transform(new DOMSource(msg.getDocument()), new SAXResult(handler)); } finally { // Make sure we do not retain any reference to state by removing // the output stream reference and resetting internal state. transmogrifier.setOutputStream(null); transmogrifier.getSAXTransmogrifier(); } } }
@Override public final void startExiCommunication(final NetconfMessage startExiMessage) { final EXIParameters exiParams; try { exiParams = EXIParameters.fromXmlElement(XmlElement.fromDomDocument(startExiMessage.getDocument())); } catch (final EXIOptionsException e) { LOG.warn("Unable to parse EXI parameters from {} on session {}", startExiMessage, this, e); throw new IllegalArgumentException("Cannot parse options", e); } final NetconfEXICodec exiCodec = new NetconfEXICodec(exiParams.getOptions()); final NetconfMessageToEXIEncoder exiEncoder; try { exiEncoder = NetconfMessageToEXIEncoder.create(exiCodec); } catch (EXIOptionsException | TransmogrifierException e) { LOG.warn("Failed to instantiate EXI encoder for {} on session {}", exiCodec, this, e); throw new IllegalStateException("Cannot instantiate encoder for options", e); } 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); }