BUG-2314 Allow rpcs with no input in netconf connector
[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_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;
30
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;
38 import java.util.Map;
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;
72
73 public class NetconfMessageTransformerTest {
74
75     private NetconfMessageTransformer netconfMessageTransformer;
76     private SchemaContext schema;
77
78     @Before
79     public void setUp() throws Exception {
80         XMLUnit.setIgnoreWhitespace(true);
81         XMLUnit.setIgnoreAttributeOrder(true);
82         XMLUnit.setIgnoreComments(true);
83
84         schema = getSchema(true);
85         netconfMessageTransformer = getTransformer(schema);
86
87     }
88
89     @Test
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));
95
96         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
97     }
98
99     @Test
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>";
104
105         transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
106     }
107
108     @Test
109     public void testDiscardChangesRequest() throws Exception {
110         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
111         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
112     }
113
114     @Test
115     public void tesGetSchemaRequest() throws Exception {
116         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
117                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
118         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
119                 "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
120                 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
121                 "<identifier>module</identifier>\n" +
122                 "<version>2012-12-12</version>\n" +
123                 "</get-schema>\n" +
124                 "</rpc>");
125     }
126
127     @Test
128     public void tesGetSchemaResponse() throws Exception {
129         final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
130         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
131                 "<rpc-reply message-id=\"101\"\n" +
132                         "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
133                         "<data\n" +
134                         "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
135                         "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
136                         "Random YANG SCHEMA\n" +
137                         "</xs:schema>\n" +
138                         "</data>\n" +
139                         "</rpc-reply>"
140         ));
141         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
142         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
143         assertNotNull(compositeNodeRpcResult.getResult());
144         final DOMSource schemaContent = ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
145         assertThat(((Element) schemaContent.getNode()).getTextContent(), CoreMatchers.containsString("Random YANG SCHEMA"));
146     }
147
148     @Test
149     public void testGetConfigResponse() throws Exception {
150         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
151                 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
152                 "<data>\n" +
153                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
154                 "<schemas>\n" +
155                 "<schema>\n" +
156                 "<identifier>module</identifier>\n" +
157                 "<version>2012-12-12</version>\n" +
158                 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
159                 "</schema>\n" +
160                 "</schemas>\n" +
161                 "</netconf-state>\n" +
162                 "</data>\n" +
163                 "</rpc-reply>"));
164
165         final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
166         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
167         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
168         assertNotNull(compositeNodeRpcResult.getResult());
169
170         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
171                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
172
173         final Map<QName, Object> keys = Maps.newHashMap();
174         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
175             keys.put(value.getNodeType(), value.getValue());
176         }
177
178         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
179         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
180
181         final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
182         final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
183         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
184         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
185         assertEquals(1, Iterables.size(schemaParent.getValue()));
186
187         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
188     }
189
190     @Test
191     public void testGetConfigRequest() throws Exception {
192         final DataContainerChild<?, ?> filter = toFilterStructure(
193                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
194
195         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
196
197         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
198                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
199
200         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
201                 "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
202                 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
203                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
204                 "<schemas/>\n" +
205                 "</netconf-state>" +
206                 "</filter>\n" +
207                 "<source>\n" +
208                 "<running/>\n" +
209                 "</source>\n" +
210                 "</get-config>" +
211                 "</rpc>");
212     }
213
214     @Test
215     public void testEditConfigRequest() throws Exception {
216         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
217             NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
218
219         final Map<QName, Object> keys = Maps.newHashMap();
220         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
221             keys.put(value.getNodeType(), value.getValue());
222         }
223
224         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
225         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
226
227         final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build();
228         final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(NetconfDevice.INIT_SCHEMA_CTX, id, Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
229
230         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
231
232         final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
233         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
234
235         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
236                 "<edit-config>\n" +
237                 "<target>\n" +
238                 "<candidate/>\n" +
239                 "</target>\n" +
240                 "<config>\n" +
241                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
242                 "<schemas>\n" +
243                 "<schema>\n" +
244                 "<identifier>module</identifier>\n" +
245                 "<version>2012-12-12</version>\n" +
246                 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
247                 "</schema>\n" +
248                 "</schemas>\n" +
249                 "</netconf-state>\n" +
250                 "</config>\n" +
251                 "</edit-config>\n" +
252                 "</rpc>");
253     }
254
255     private void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent) throws SAXException, IOException {
256         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
257         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
258         assertTrue(diff.toString(), diff.similar());
259     }
260
261     @Test
262     public void testGetRequest() throws Exception {
263
264         final QName capability = QName.create(Capabilities.QNAME, "capability");
265         final DataContainerChild<?, ?> filter = toFilterStructure(
266                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability), new YangInstanceIdentifier.NodeWithValue(capability, "a:b:c")), schema);
267
268         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
269                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
270
271         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
272                 "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
273                 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
274                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
275                 "<capabilities>\n" +
276                 "<capability>a:b:c</capability>\n" +
277                 "</capabilities>\n" +
278                 "</netconf-state>" +
279                 "</filter>\n" +
280                 "</get>" +
281                 "</rpc>");
282     }
283
284     private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
285         return new NetconfMessageTransformer(schema);
286     }
287
288     @Test
289     public void testCommitResponse() throws Exception {
290         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
291                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
292         ));
293         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
294         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
295         assertNull(compositeNodeRpcResult.getResult());
296     }
297
298     public SchemaContext getSchema(boolean addBase) {
299         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
300         if(addBase) {
301             moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
302         }
303         moduleInfoBackedContext.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
304         return moduleInfoBackedContext.tryToCreateSchemaContext().get();
305     }
306 }