X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-netconf-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Fconnect%2Fnetconf%2Fschema%2Fmapping%2FNetconfMessageTransformerTest.java;h=a43f807e0f8bfd95d527d0ba4cf0151dd63d510c;hb=ccfab2e4f9599c13c58d9fa62a3275b17d3d6caa;hp=5a2c97c549b270a0d4a8de040cdbc59391d9c6ac;hpb=edf5bfcee83c750853253ccfd991ba7000f5f65b;p=controller.git diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java b/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java index 5a2c97c549..a43f807e0f 100644 --- a/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java +++ b/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformerTest.java @@ -10,63 +10,300 @@ package org.opendaylight.controller.sal.connect.netconf.schema.mapping; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toId; +import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath; -import java.io.InputStream; +import com.google.common.base.Optional; +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import java.io.IOException; import java.util.Collections; import java.util.List; -import java.util.Set; +import java.util.Map; +import javax.xml.transform.dom.DOMSource; +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier; +import org.custommonkey.xmlunit.XMLUnit; import org.hamcrest.CoreMatchers; +import org.junit.Before; import org.junit.Test; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.util.xml.XmlUtil; -import org.opendaylight.controller.sal.connect.netconf.NetconfToRpcRequestTest; +import org.opendaylight.controller.sal.connect.netconf.NetconfDevice; +import org.opendaylight.controller.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider; +import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps; +import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema; +import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.impl.NodeFactory; -import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.data.api.ModifyAction; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild; +import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode; +import org.opendaylight.yangtools.yang.data.api.schema.MapNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.model.api.SchemaContext; -import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; +import org.w3c.dom.Element; +import org.xml.sax.SAXException; public class NetconfMessageTransformerTest { - private static final QName COMMIT_Q_NAME = QName.create("namespace", "2012-12-12", "commit"); + private NetconfMessageTransformer netconfMessageTransformer; + private SchemaContext schema; + + @Before + public void setUp() throws Exception { + XMLUnit.setIgnoreWhitespace(true); + XMLUnit.setIgnoreAttributeOrder(true); + XMLUnit.setIgnoreComments(true); + + schema = getSchema(true); + netconfMessageTransformer = getTransformer(schema); + + } + + @Test + public void testLockRequestBaseSchemaNotPresent() throws Exception { + final SchemaContext partialSchema = getSchema(false); + final NetconfMessageTransformer transformer = getTransformer(partialSchema); + final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME), + NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME)); + + assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString(""; + + transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME)); + } + + @Test + public void testDiscardChangesRequest() throws Exception { + final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null); + assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("\n" + + "\n" + + "x:yang\n" + + "module\n" + + "2012-12-12\n" + + "\n" + + ""); + } + + @Test + public void tesGetSchemaResponse() throws Exception { + final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true)); + final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument( + "\n" + + "\n" + + "\n" + + "Random YANG SCHEMA\n" + + "\n" + + "\n" + + "" + )); + final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME)); + assertTrue(compositeNodeRpcResult.getErrors().isEmpty()); + assertNotNull(compositeNodeRpcResult.getResult()); + final DOMSource schemaContent = ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue(); + assertThat(((Element) schemaContent.getNode()).getTextContent(), CoreMatchers.containsString("Random YANG SCHEMA")); + } + + @Test + public void testGetConfigResponse() throws Exception { + final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "module\n" + + "2012-12-12\n" + + "x:yang\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "")); + + final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true)); + final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME)); + assertTrue(compositeNodeRpcResult.getErrors().isEmpty()); + assertNotNull(compositeNodeRpcResult.getResult()); + + final List> values = Lists.newArrayList( + NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue()); + + final Map keys = Maps.newHashMap(); + for (final DataContainerChild value : values) { + keys.put(value.getNodeType(), value.getValue()); + } + + final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys); + final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build(); + + final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getChild(toId(NETCONF_DATA_QNAME)).get(); + final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get(); + final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get(); + final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get(); + assertEquals(1, Iterables.size(schemaParent.getValue())); + + assertEquals(schemaNode, schemaParent.getValue().iterator().next()); + } + + @Test + public void testGetConfigRequest() throws Exception { + final DataContainerChild filter = toFilterStructure( + YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema); + + final DataContainerChild source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME); + + final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME), + NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter)); + + assertSimilarXml(netconfMessage, "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "" + + "\n" + + "\n" + + "\n" + + "\n" + + "" + + ""); + } + + @Test + public void testEditConfigRequest() throws Exception { + final List> values = Lists.newArrayList( + NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue()); + + final Map keys = Maps.newHashMap(); + for (final DataContainerChild value : values) { + keys.put(value.getNodeType(), value.getValue()); + } + + final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys); + final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build(); + + final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build(); + final DataContainerChild editConfigStructure = createEditConfigStructure(NetconfDevice.INIT_SCHEMA_CTX, id, Optional.absent(), Optional.>fromNullable(schemaNode)); + + final DataContainerChild target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME); + + final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target); + final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap); + + assertSimilarXml(netconfMessage, "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "module\n" + + "2012-12-12\n" + + "x:yang\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + ""); + } + + private void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent) throws SAXException, IOException { + final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent)); + diff.overrideElementQualifier(new ElementNameAndAttributeQualifier()); + assertTrue(diff.toString(), diff.similar()); + } @Test - public void testToRpcRequestNoSchemaForRequest() throws Exception { - final NetconfMessageTransformer netconfMessageTransformer = getTransformer(); - final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(COMMIT_Q_NAME, - NodeFactory.createImmutableCompositeNode(COMMIT_Q_NAME, null, Collections.>emptyList())); - assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString(" filter = toFilterStructure( + YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability), new YangInstanceIdentifier.NodeWithValue(capability, "a:b:c")), schema); + + final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME), + NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter)); + + assertSimilarXml(netconfMessage, "" + + "\n" + + "\n" + + "\n" + + "\n" + + "a:b:c\n" + + "\n" + + "" + + "\n" + + "" + + ""); } - private NetconfMessageTransformer getTransformer() { - final NetconfMessageTransformer netconfMessageTransformer = new NetconfMessageTransformer(); - netconfMessageTransformer.onGlobalContextUpdated(getSchema()); - return netconfMessageTransformer; + private NetconfMessageTransformer getTransformer(final SchemaContext schema) { + return new NetconfMessageTransformer(schema, true); } @Test - public void testToRpcResultNoSchemaForResult() throws Exception { - final NetconfMessageTransformer netconfMessageTransformer = getTransformer(); + public void testCommitResponse() throws Exception { final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument( - "" + "" )); - final RpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, COMMIT_Q_NAME); - assertTrue(compositeNodeRpcResult.isSuccessful()); - assertEquals("ok", compositeNodeRpcResult.getResult().getValue().get(0).getKey().getLocalName()); + final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME)); + assertTrue(compositeNodeRpcResult.getErrors().isEmpty()); + assertNull(compositeNodeRpcResult.getResult()); } - public SchemaContext getSchema() { - final List modelsToParse = Collections - .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang")); - final YangParserImpl parser = new YangParserImpl(); - final Set configModules = parser.parseYangModelsFromStreams(modelsToParse); - final SchemaContext cfgCtx = parser.resolveSchemaContext(configModules); - assertNotNull(cfgCtx); - return cfgCtx; + public SchemaContext getSchema(boolean addBase) { + final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create(); + if(addBase) { + moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance())); + } + moduleInfoBackedContext.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance())); + return moduleInfoBackedContext.tryToCreateSchemaContext().get(); } }