From: Lukas Sedlak Date: Tue, 8 Jul 2014 10:00:23 +0000 (+0200) Subject: Bug 1292: Fix for NPE after notification get. X-Git-Tag: release/helium~508^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=bc9ee1429cbc247b3ef152a1d3ea29971a41843f Bug 1292: Fix for NPE after notification get. Fixed incorrect pass of SchemaContext in toNotification method in NetconfMessageTransformer class. Added test for proper testing of resolving of notficiation message. Added test models and test notification xml payload for testing purposes. Change-Id: I4ada2fdf976003f4a2809d6cef53816ec39be364 Signed-off-by: Lukas Sedlak --- diff --git a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java index 21f94474f8..f9e6239bed 100644 --- a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java +++ b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java @@ -55,7 +55,7 @@ public class NetconfMessageTransformer implements MessageTransformer notifications = ctx.getNotifications(); final Document document = message.getDocument(); - return XmlDocumentUtils.notificationToDomNodes(document, Optional.fromNullable(notifications)); + return XmlDocumentUtils.notificationToDomNodes(document, Optional.fromNullable(notifications), ctx); } @Override diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/NetconfToNotificationTest.java b/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/NetconfToNotificationTest.java new file mode 100644 index 0000000000..521a55df68 --- /dev/null +++ b/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/NetconfToNotificationTest.java @@ -0,0 +1,64 @@ +package org.opendaylight.controller.sal.connect.netconf; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; + +import java.io.InputStream; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilderFactory; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer; +import org.opendaylight.yangtools.yang.data.api.CompositeNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser; +import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; +import org.w3c.dom.Document; + +/** + * @author Lukas Sedlak + */ +public class NetconfToNotificationTest { + + NetconfMessageTransformer messageTransformer; + + NetconfMessage userNotification; + + @Before + public void setup() throws Exception { + final List modelsToParse = Collections.singletonList(getClass().getResourceAsStream("/schemas/user-notification.yang")); + final YangContextParser parser = new YangParserImpl(); + final Set modules = parser.parseYangModelsFromStreams(modelsToParse); + assertTrue(!modules.isEmpty()); + final SchemaContext schemaContext = parser.resolveSchemaContext(modules); + assertNotNull(schemaContext); + + messageTransformer = new NetconfMessageTransformer(); + messageTransformer.onGlobalContextUpdated(schemaContext); + + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml"); + assertNotNull(notifyPayloadStream); + + final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream); + assertNotNull(doc); + userNotification = new NetconfMessage(doc); + } + + @Test + public void test() throws Exception { + final CompositeNode root = messageTransformer.toNotification(userNotification); + + assertNotNull(root); + assertEquals(6, root.size()); + assertEquals("user-visited-page", root.getKey().getLocalName()); + } +} diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/resources/notification-payload.xml b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/notification-payload.xml new file mode 100644 index 0000000000..837fcc1a70 --- /dev/null +++ b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/notification-payload.xml @@ -0,0 +1,14 @@ + +2014-07-08T11:20:48UTC + + ui:public-user + 172.23.29.104 + 00:11:00:ff:dd:02 + Chrome 35.0.1916.153 m + + Slovakia + UTC/GMT+2 + + 2014-07-08 11:20:48 + + \ No newline at end of file diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/resources/schemas/user-notification.yang b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/schemas/user-notification.yang new file mode 100644 index 0000000000..50d220689f --- /dev/null +++ b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/schemas/user-notification.yang @@ -0,0 +1,56 @@ +module user-notification { + yang-version 1; + namespace "org:opendaylight:notification:test:ns:yang:user-notification"; + prefix "user"; + + organization "Cisco Systems"; + contact "Lukas Sedlak"; + description "Test model for testing notifications"; + + revision "2014-07-08" { + description "Initial revision"; + } + + identity user-identity { + description "Identity of user incoming to Web Page"; + } + + identity public-user { + base user-identity; + description "Identity of random public non-registered user"; + } + + notification user-visited-page { + leaf incoming-user { + type identityref { + base "user-identity"; + } + } + + leaf ip-address { + type string; + } + + leaf mac { + type string; + } + + leaf browser-id { + type string; + } + + container region { + leaf name { + type string; + } + + leaf time-zone { + type string; + } + } + + leaf visiting-date { + type string; + } + } +} \ No newline at end of file