use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.
the class DeviceTest method toasterRPCsTest.
@Test
public void toasterRPCsTest() throws ExecutionException, InterruptedException, URISyntaxException, SAXException, TimeoutException, IOException {
for (SimpleNetconfClientSessionListener listener : SESSION_LISTENERS) {
final NetconfMessage makeToastResponse = sendRequestToDevice(MAKE_TOAST_REQUEST_XML, listener);
Assertions.assertTrue(containsOkElement(makeToastResponse));
}
}
use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.
the class DeviceTest method getSchemaTest.
@Test
public void getSchemaTest() throws IOException, URISyntaxException, SAXException, InterruptedException, ExecutionException, TimeoutException {
for (SimpleNetconfClientSessionListener listener : SESSION_LISTENERS) {
final NetconfMessage schemaResponse = sendRequestToDevice(GET_SCHEMAS_REQUEST_XML, listener);
final NodeList schema = schemaResponse.getDocument().getDocumentElement().getElementsByTagName("schema");
Assertions.assertTrue(schema.getLength() > 0);
boolean toasterSchemaContained = false;
for (int i = 0; i < schema.getLength(); i++) {
if (schema.item(i).getNodeType() == Node.ELEMENT_NODE) {
final Element item = (Element) schema.item(i);
final String schemaName = item.getElementsByTagName("identifier").item(0).getTextContent();
final String schemaNameSpace = item.getElementsByTagName("namespace").item(0).getTextContent();
if ("toaster".equals(schemaName) && "http://netconfcentral.org/ns/toaster".equals(schemaNameSpace)) {
toasterSchemaContained = true;
}
}
}
Assertions.assertTrue(toasterSchemaContained);
}
}
use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.
the class NotificationOperation method sendMessage.
public void sendMessage(final Notification notificationMessage, final QName quName) {
final List<NetconfSession> sessionList = this.sessions.get(quName.getLocalName());
if (sessionList != null && !sessionList.isEmpty()) {
final ContainerNode containerNode = this.adapterContext.currentSerializer().toNormalizedNodeNotification(notificationMessage);
final Optional<? extends NotificationDefinition> notificationDefinition = ConverterUtils.loadNotification(this.effectiveModelContext, quName);
final XmlNodeConverter xmlNodeConverter = new XmlNodeConverter(this.effectiveModelContext);
if (notificationDefinition.isEmpty()) {
throw new UnsupportedOperationException("Cannot load definition for QName: " + quName);
}
final Writer writer;
try {
writer = xmlNodeConverter.serializeRpc(notificationDefinition.get(), containerNode);
try (InputStream is = new ByteArrayInputStream(writer.toString().getBytes(StandardCharsets.UTF_8))) {
final DocumentBuilder builder = UntrustedXML.newDocumentBuilder();
final Document notification = builder.parse(is);
final Element body = notification.createElementNS(RPCUtil.CREATE_SUBSCRIPTION_NAMESPACE, "notification");
final Element notificationElement = notification.getDocumentElement();
final Element eventTime = notification.createElement("eventTime");
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
eventTime.setTextContent(dateFormat.format(new Date()));
body.appendChild(eventTime);
body.appendChild(notificationElement);
final Document document = builder.newDocument();
final org.w3c.dom.Node importNode = document.importNode(body, true);
document.appendChild(importNode);
final NetconfMessage netconfMessage = new NetconfMessage(document);
LOG.debug("Sending notification message: {}", netconfMessage.toString());
sessionList.forEach(session -> session.sendMessage(netconfMessage));
} catch (IOException | SAXException e) {
LOG.error("Failed to send notification message", e);
}
} catch (final SerializationException e) {
LOG.error("Failed to serialize notification to xml", e);
}
}
}
use of org.opendaylight.netconf.api.NetconfMessage in project lighty-netconf-simulator by PANTHEONtech.
the class ActionDeviceTest method actionsTest.
@Test
public void actionsTest() throws IOException, URISyntaxException, SAXException, InterruptedException, ExecutionException, TimeoutException {
final SimpleNetconfClientSessionListener sessionListener = new SimpleNetconfClientSessionListener();
try (NetconfClientSession session = dispatcher.createClient(createSHHConfig(sessionListener)).get(TimeoutUtil.TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) {
final NetconfMessage startActionResponse = sentRequesttoDevice(sessionListener, START_ACTION_REQUEST_XML);
final String startResultTag = startActionResponse.getDocument().getDocumentElement().getElementsByTagName(START_TAG).item(0).getTextContent();
assertEquals(startResultTag, START_ACTION_EXPECTED_VALUE);
final NetconfMessage resetActionResponse = sentRequesttoDevice(sessionListener, RESET_ACTION_REQUEST_XML);
final String resetResultTag = resetActionResponse.getDocument().getDocumentElement().getElementsByTagName(RESET_TAG).item(0).getTextContent();
assertEquals(resetResultTag, RESET_ACTION_EXPECTED_VALUE);
}
}
use of org.opendaylight.netconf.api.NetconfMessage in project netconf by opendaylight.
the class NetconfServerSessionListenerTest method testOnMessageRuntimeFail.
@Test
public void testOnMessageRuntimeFail() throws Exception {
doThrow(new RuntimeException("runtime fail")).when(router).onNetconfMessage(any(), any());
final Document reply = XmlUtil.readXmlToDocument("<rpc message-id=\"101\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" + "<example/></rpc>");
final NetconfMessage msg = new NetconfMessage(reply);
final IllegalStateException ex = assertThrows(IllegalStateException.class, () -> listener.onMessage(session, msg));
verify(monitoringListener).onSessionEvent(argThat(sessionEventIs(SessionEvent.Type.IN_RPC_FAIL)));
}
Aggregations