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_LOCK_QNAME;
25 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
26 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
27 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
28 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
29 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
31 import com.google.common.base.Optional;
32 import com.google.common.collect.Iterables;
33 import com.google.common.collect.Lists;
34 import com.google.common.collect.Maps;
35 import java.io.IOException;
36 import java.util.Collections;
37 import java.util.List;
39 import javax.xml.transform.dom.DOMSource;
40 import org.custommonkey.xmlunit.Diff;
41 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
42 import org.custommonkey.xmlunit.XMLUnit;
43 import org.hamcrest.CoreMatchers;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
47 import org.opendaylight.controller.netconf.api.NetconfMessage;
48 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
49 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
50 import org.opendaylight.controller.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
51 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
52 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
58 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
59 import org.opendaylight.yangtools.yang.common.QName;
60 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
65 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
68 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
70 import org.w3c.dom.Element;
71 import org.xml.sax.SAXException;
73 public class NetconfMessageTransformerTest {
75 private NetconfMessageTransformer netconfMessageTransformer;
76 private SchemaContext schema;
79 public void setUp() throws Exception {
80 XMLUnit.setIgnoreWhitespace(true);
81 XMLUnit.setIgnoreAttributeOrder(true);
82 XMLUnit.setIgnoreComments(true);
84 schema = getSchema(true);
85 netconfMessageTransformer = getTransformer(schema);
90 public void testLockRequestBaseSchemaNotPresent() throws Exception {
91 final SchemaContext partialSchema = getSchema(false);
92 final NetconfMessageTransformer transformer = getTransformer(partialSchema);
93 final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
94 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
96 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
100 public void tesLockSchemaRequest() throws Exception {
101 final SchemaContext partialSchema = getSchema(false);
102 final NetconfMessageTransformer transformer = getTransformer(partialSchema);
103 final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
105 transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
109 public void testDiscardChangesRequest() throws Exception {
110 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME),
111 NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT);
112 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
116 public void tesGetSchemaRequest() throws Exception {
117 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
118 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
119 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
120 "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
121 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
122 "<identifier>module</identifier>\n" +
123 "<version>2012-12-12</version>\n" +
130 public void tesGetSchemaResponse() throws Exception {
131 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
132 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
133 "<rpc-reply message-id=\"101\"\n" +
134 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
136 "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
137 "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
138 "Random YANG SCHEMA\n" +
143 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
144 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
145 assertNotNull(compositeNodeRpcResult.getResult());
146 final DOMSource schemaContent = ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
147 assertThat(((Element) schemaContent.getNode()).getTextContent(), CoreMatchers.containsString("Random YANG SCHEMA"));
151 public void testGetConfigResponse() throws Exception {
152 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
153 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
155 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
158 "<identifier>module</identifier>\n" +
159 "<version>2012-12-12</version>\n" +
160 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
163 "</netconf-state>\n" +
167 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
168 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
169 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
170 assertNotNull(compositeNodeRpcResult.getResult());
172 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
173 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
175 final Map<QName, Object> keys = Maps.newHashMap();
176 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
177 keys.put(value.getNodeType(), value.getValue());
180 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
181 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
183 final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
184 final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
185 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
186 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
187 assertEquals(1, Iterables.size(schemaParent.getValue()));
189 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
193 public void testGetConfigRequest() throws Exception {
194 final DataContainerChild<?, ?> filter = toFilterStructure(
195 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
197 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
199 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
200 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
202 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
203 "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
204 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
205 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
217 public void testEditConfigRequest() throws Exception {
218 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
219 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
221 final Map<QName, Object> keys = Maps.newHashMap();
222 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
223 keys.put(value.getNodeType(), value.getValue());
226 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
227 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
229 final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build();
230 final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(NetconfDevice.INIT_SCHEMA_CTX, id, Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
232 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
234 final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
235 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
237 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
243 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
246 "<identifier>module</identifier>\n" +
247 "<version>2012-12-12</version>\n" +
248 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
251 "</netconf-state>\n" +
257 private void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent) throws SAXException, IOException {
258 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
259 diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
260 assertTrue(diff.toString(), diff.similar());
264 public void testGetRequest() throws Exception {
266 final QName capability = QName.create(Capabilities.QNAME, "capability");
267 final DataContainerChild<?, ?> filter = toFilterStructure(
268 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability), new YangInstanceIdentifier.NodeWithValue(capability, "a:b:c")), schema);
270 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
271 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
273 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
274 "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
275 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
276 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
278 "<capability>a:b:c</capability>\n" +
279 "</capabilities>\n" +
286 private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
287 return new NetconfMessageTransformer(schema);
291 public void testCommitResponse() throws Exception {
292 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
293 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
295 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
296 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
297 assertNull(compositeNodeRpcResult.getResult());
300 public SchemaContext getSchema(boolean addBase) {
301 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
303 moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
305 moduleInfoBackedContext.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
306 return moduleInfoBackedContext.tryToCreateSchemaContext().get();