2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html.
9 package org.opendaylight.controller.sal.connect.netconf.schema.mapping;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
17 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
18 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
19 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
20 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
21 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
22 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
23 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
24 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
25 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
26 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
27 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
28 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
30 import com.google.common.base.Optional;
31 import com.google.common.collect.Iterables;
32 import com.google.common.collect.Lists;
33 import com.google.common.collect.Maps;
34 import java.io.IOException;
35 import java.util.Collections;
36 import java.util.List;
38 import javax.xml.transform.dom.DOMSource;
39 import org.custommonkey.xmlunit.Diff;
40 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
41 import org.custommonkey.xmlunit.XMLUnit;
42 import org.hamcrest.CoreMatchers;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
46 import org.opendaylight.controller.netconf.api.NetconfMessage;
47 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
48 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
49 import org.opendaylight.controller.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
50 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
51 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
57 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
61 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
64 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
67 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
68 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
69 import org.w3c.dom.Element;
70 import org.xml.sax.SAXException;
72 public class NetconfMessageTransformerTest {
74 private NetconfMessageTransformer netconfMessageTransformer;
75 private SchemaContext schema;
78 public void setUp() throws Exception {
79 XMLUnit.setIgnoreWhitespace(true);
80 XMLUnit.setIgnoreAttributeOrder(true);
81 XMLUnit.setIgnoreComments(true);
84 netconfMessageTransformer = getTransformer(schema);
89 public void testDiscardChangesRequest() throws Exception {
90 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME),
91 NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT);
92 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
96 public void tesGetSchemaRequest() throws Exception {
97 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
98 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
99 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
100 "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
101 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
102 "<identifier>module</identifier>\n" +
103 "<version>2012-12-12</version>\n" +
109 public void tesGetSchemaResponse() throws Exception {
110 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema());
111 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
112 "<rpc-reply message-id=\"101\"\n" +
113 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
115 "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
116 "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
117 "Random YANG SCHEMA\n" +
122 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
123 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
124 assertNotNull(compositeNodeRpcResult.getResult());
125 final DOMSource schemaContent = ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
126 assertThat(((Element) schemaContent.getNode()).getTextContent(), CoreMatchers.containsString("Random YANG SCHEMA"));
130 public void testGetConfigResponse() throws Exception {
131 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
132 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
134 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
137 "<identifier>module</identifier>\n" +
138 "<version>2012-12-12</version>\n" +
139 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
142 "</netconf-state>\n" +
146 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema());
147 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
148 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
149 assertNotNull(compositeNodeRpcResult.getResult());
151 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
152 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
154 final Map<QName, Object> keys = Maps.newHashMap();
155 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
156 keys.put(value.getNodeType(), value.getValue());
159 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
160 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
162 final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
163 final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
164 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
165 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
166 assertEquals(1, Iterables.size(schemaParent.getValue()));
168 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
172 public void testGetConfigRequest() throws Exception {
173 final DataContainerChild<?, ?> filter = toFilterStructure(
174 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
176 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
178 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
179 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
181 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
182 "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
183 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
184 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
196 public void testEditConfigRequest() throws Exception {
197 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
198 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
200 final Map<QName, Object> keys = Maps.newHashMap();
201 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
202 keys.put(value.getNodeType(), value.getValue());
205 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
206 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
208 final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build();
209 final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(NetconfDevice.INIT_SCHEMA_CTX, id, Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
211 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
213 final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
214 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
216 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
222 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
225 "<identifier>module</identifier>\n" +
226 "<version>2012-12-12</version>\n" +
227 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
230 "</netconf-state>\n" +
236 private void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent) throws SAXException, IOException {
237 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
238 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
239 assertTrue(diff.toString(), diff.similar());
243 public void testGetRequest() throws Exception {
245 final QName capability = QName.create(Capabilities.QNAME, "capability");
246 final DataContainerChild<?, ?> filter = toFilterStructure(
247 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability), new YangInstanceIdentifier.NodeWithValue(capability, "a:b:c")), schema);
249 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
250 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
252 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
253 "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
254 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
255 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
257 "<capability>a:b:c</capability>\n" +
258 "</capabilities>\n" +
265 private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
266 return new NetconfMessageTransformer(schema);
270 public void testCommitResponse() throws Exception {
271 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
272 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
274 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
275 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
276 assertNull(compositeNodeRpcResult.getResult());
279 public SchemaContext getSchema() {
280 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
281 moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
282 moduleInfoBackedContext.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
283 return moduleInfoBackedContext.tryToCreateSchemaContext().get();