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.XMLUnit;
41 import org.hamcrest.CoreMatchers;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
45 import org.opendaylight.controller.netconf.api.NetconfMessage;
46 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
47 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
48 import org.opendaylight.controller.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
49 import org.opendaylight.controller.sal.connect.netconf.util.NetconfBaseOps;
50 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
56 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
62 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
63 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
66 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 import org.w3c.dom.Element;
69 import org.xml.sax.SAXException;
71 public class NetconfMessageTransformerTest {
73 private NetconfMessageTransformer netconfMessageTransformer;
74 private SchemaContext schema;
77 public void setUp() throws Exception {
78 XMLUnit.setIgnoreWhitespace(true);
79 XMLUnit.setIgnoreAttributeOrder(true);
80 XMLUnit.setIgnoreComments(true);
83 netconfMessageTransformer = getTransformer(schema);
88 public void testDiscardChangesRequest() throws Exception {
89 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME),
90 NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT);
91 assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
95 public void tesGetSchemaRequest() throws Exception {
96 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
97 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
98 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
99 "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
100 "<format>yang</format>\n" +
101 "<identifier>module</identifier>\n" +
102 "<version>2012-12-12</version>\n" +
108 public void tesGetSchemaResponse() throws Exception {
109 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema());
110 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
111 "<rpc-reply message-id=\"101\"\n" +
112 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
114 "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
115 "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
116 "Random YANG SCHEMA\n" +
121 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
122 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
123 assertNotNull(compositeNodeRpcResult.getResult());
124 final DOMSource schemaContent = ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
125 assertThat(((Element) schemaContent.getNode()).getTextContent(), CoreMatchers.containsString("Random YANG SCHEMA"));
129 public void testGetConfigResponse() throws Exception {
130 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
131 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
133 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
136 "<identifier>module</identifier>\n" +
137 "<version>2012-12-12</version>\n" +
138 "<format>yang</format>\n" +
141 "</netconf-state>\n" +
145 final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema());
146 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
147 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
148 assertNotNull(compositeNodeRpcResult.getResult());
150 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
151 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
153 final Map<QName, Object> keys = Maps.newHashMap();
154 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
155 keys.put(value.getNodeType(), value.getValue());
158 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
159 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
161 final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
162 final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
163 final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
164 final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
165 assertEquals(1, Iterables.size(schemaParent.getValue()));
167 assertEquals(schemaNode, schemaParent.getValue().iterator().next());
171 public void testGetConfigRequest() throws Exception {
172 final DataContainerChild<?, ?> filter = toFilterStructure(
173 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
175 final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
177 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
178 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
180 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
181 "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
182 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
183 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
195 public void testEditConfigRequest() throws Exception {
196 final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
197 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
199 final Map<QName, Object> keys = Maps.newHashMap();
200 for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
201 keys.put(value.getNodeType(), value.getValue());
204 final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
205 final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
207 final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build();
208 final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(NetconfDevice.INIT_SCHEMA_CTX, id, Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
210 final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
212 final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
213 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
215 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
221 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
224 "<identifier>module</identifier>\n" +
225 "<version>2012-12-12</version>\n" +
226 "<format>yang</format>\n" +
229 "</netconf-state>\n" +
235 private void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent) throws SAXException, IOException {
236 final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
237 assertTrue(diff.toString(), diff.similar());
241 public void testGetRequest() throws Exception {
243 final QName capability = QName.create(Capabilities.QNAME, "capability");
244 final DataContainerChild<?, ?> filter = toFilterStructure(
245 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability), new YangInstanceIdentifier.NodeWithValue(capability, "a:b:c")), schema);
247 final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
248 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
250 assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
251 "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
252 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
253 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
255 "<capability>a:b:c</capability>\n" +
256 "</capabilities>\n" +
263 private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
264 return new NetconfMessageTransformer(schema);
268 public void testCommitResponse() throws Exception {
269 final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
270 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
272 final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
273 assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
274 assertNull(compositeNodeRpcResult.getResult());
277 public SchemaContext getSchema() {
278 final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
279 moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
280 moduleInfoBackedContext.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
281 return moduleInfoBackedContext.tryToCreateSchemaContext().get();