Add SchemaContext to NormalizedNode parser in NETCONF
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / schema / mapping / NetconfMessageTransformerTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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.
7  */
8
9 package org.opendaylight.controller.sal.connect.netconf.schema.mapping;
10
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;
29
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;
37 import java.util.Map;
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;
71
72 public class NetconfMessageTransformerTest {
73
74     private NetconfMessageTransformer netconfMessageTransformer;
75     private SchemaContext schema;
76
77     @Before
78     public void setUp() throws Exception {
79         XMLUnit.setIgnoreWhitespace(true);
80         XMLUnit.setIgnoreAttributeOrder(true);
81         XMLUnit.setIgnoreComments(true);
82
83         schema = getSchema();
84         netconfMessageTransformer = getTransformer(schema);
85
86     }
87
88     @Test
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"));
93     }
94
95     @Test
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" +
104                 "</get-schema>\n" +
105                 "</rpc>");
106     }
107
108     @Test
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" +
114                         "<data\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" +
118                         "</xs:schema>\n" +
119                         "</data>\n" +
120                         "</rpc-reply>"
121         ));
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"));
127     }
128
129     @Test
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" +
133                 "<data>\n" +
134                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
135                 "<schemas>\n" +
136                 "<schema>\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" +
140                 "</schema>\n" +
141                 "</schemas>\n" +
142                 "</netconf-state>\n" +
143                 "</data>\n" +
144                 "</rpc-reply>"));
145
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());
150
151         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
152                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
153
154         final Map<QName, Object> keys = Maps.newHashMap();
155         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
156             keys.put(value.getNodeType(), value.getValue());
157         }
158
159         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
160         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
161
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()));
167
168         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
169     }
170
171     @Test
172     public void testGetConfigRequest() throws Exception {
173         final DataContainerChild<?, ?> filter = toFilterStructure(
174                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
175
176         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
177
178         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
179                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
180
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" +
185                 "<schemas/>\n" +
186                 "</netconf-state>" +
187                 "</filter>\n" +
188                 "<source>\n" +
189                 "<running/>\n" +
190                 "</source>\n" +
191                 "</get-config>" +
192                 "</rpc>");
193     }
194
195     @Test
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());
199
200         final Map<QName, Object> keys = Maps.newHashMap();
201         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
202             keys.put(value.getNodeType(), value.getValue());
203         }
204
205         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
206         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
207
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));
210
211         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
212
213         final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
214         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
215
216         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
217                 "<edit-config>\n" +
218                 "<target>\n" +
219                 "<candidate/>\n" +
220                 "</target>\n" +
221                 "<config>\n" +
222                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
223                 "<schemas>\n" +
224                 "<schema>\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" +
228                 "</schema>\n" +
229                 "</schemas>\n" +
230                 "</netconf-state>\n" +
231                 "</config>\n" +
232                 "</edit-config>\n" +
233                 "</rpc>");
234     }
235
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());
240     }
241
242     @Test
243     public void testGetRequest() throws Exception {
244
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);
248
249         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
250                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
251
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" +
256                 "<capabilities>\n" +
257                 "<capability>a:b:c</capability>\n" +
258                 "</capabilities>\n" +
259                 "</netconf-state>" +
260                 "</filter>\n" +
261                 "</get>" +
262                 "</rpc>");
263     }
264
265     private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
266         return new NetconfMessageTransformer(schema);
267     }
268
269     @Test
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>"
273         ));
274         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
275         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
276         assertNull(compositeNodeRpcResult.getResult());
277     }
278
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();
284     }
285 }