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 org.custommonkey.xmlunit.Diff;
39 import org.custommonkey.xmlunit.XMLUnit;
40 import org.hamcrest.CoreMatchers;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
44 import org.opendaylight.controller.netconf.api.NetconfMessage;
45 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
46 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
47 import org.opendaylight.controller.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
48 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
49 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
55 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
56 import org.opendaylight.yangtools.yang.common.QName;
57 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
58 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
59 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
60 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
62 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
65 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.xml.sax.SAXException;
69 public class NetconfMessageTransformerTest {
71 private NetconfMessageTransformer netconfMessageTransformer;
72 private SchemaContext schema;
75 public void setUp() throws Exception {
76 XMLUnit.setIgnoreWhitespace(true);
77 XMLUnit.setIgnoreAttributeOrder(true);
78 XMLUnit.setIgnoreComments(true);
81 netconfMessageTransformer = getTransformer(schema);
86 public void testDiscardChangesRequest() throws Exception {
87 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME),
88 NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT);
89 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
93 public void tesGetSchemaRequest() throws Exception {
94 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
95 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
96 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
97 "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
98 "<format>yang</format>\n" +
99 "<identifier>module</identifier>\n" +
100 "<version>2012-12-12</version>\n" +
106 public void tesGetSchemaResponse() throws Exception {
107 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema());
108 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
109 "<rpc-reply message-id=\"101\"\n" +
110 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
112 "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
113 "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
114 "Random YANG SCHEMA\n" +
119 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
120 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
121 assertNotNull(compositeNodeRpcResult.getResult());
122 final Object schemaContent = ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue().getValue();
123 assertThat(schemaContent.toString(), CoreMatchers.containsString("Random YANG SCHEMA"));
127 public void testGetConfigResponse() throws Exception {
128 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
129 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
131 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
134 "<identifier>module</identifier>\n" +
135 "<version>2012-12-12</version>\n" +
136 "<format>yang</format>\n" +
139 "</netconf-state>\n" +
143 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema());
144 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
145 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
146 assertNotNull(compositeNodeRpcResult.getResult());
148 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
149 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
151 final Map<QName, Object> keys = Maps.newHashMap();
152 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
153 keys.put(value.getNodeType(), value.getValue());
156 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
157 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
159 final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
160 final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
161 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
162 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
163 assertEquals(1, Iterables.size(schemaParent.getValue()));
165 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
169 public void testGetConfigRequest() throws Exception {
170 final DataContainerChild<?, ?> filter = toFilterStructure(
171 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
173 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
175 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
176 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
178 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
179 "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
180 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
181 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
193 public void testEditConfigRequest() throws Exception {
194 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
195 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
197 final Map<QName, Object> keys = Maps.newHashMap();
198 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
199 keys.put(value.getNodeType(), value.getValue());
202 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
203 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
205 final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build();
206 final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(NetconfDevice.INIT_SCHEMA_CTX, id, Optional.of(ModifyAction.REPLACE), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
208 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
210 final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
211 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
213 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
219 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
222 "<identifier>module</identifier>\n" +
223 "<version>2012-12-12</version>\n" +
224 "<format>yang</format>\n" +
227 "</netconf-state>\n" +
233 private void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent) throws SAXException, IOException {
234 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
235 assertTrue(diff.toString(), diff.similar());
239 public void testGetRequest() throws Exception {
241 final QName capability = QName.create(Capabilities.QNAME, "capability");
242 final DataContainerChild<?, ?> filter = toFilterStructure(
243 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability), new YangInstanceIdentifier.NodeWithValue(capability, "a:b:c")), schema);
245 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
246 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
248 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
249 "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
250 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
251 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
253 "<capability>a:b:c</capability>\n" +
254 "</capabilities>\n" +
261 private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
262 return new NetconfMessageTransformer(schema);
266 public void testCommitResponse() throws Exception {
267 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
268 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
270 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
271 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
272 assertNull(compositeNodeRpcResult.getResult());
275 public SchemaContext getSchema() {
276 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
277 moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
278 moduleInfoBackedContext.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
279 return moduleInfoBackedContext.tryToCreateSchemaContext().get();