Merge "Changed NetconfDeviceDatastoreAdapter and NetconfDeviceTopologyAdapter to...
[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),
111                 NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT);
112         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
113     }
114
115     @Test
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" +
124                 "</get-schema>\n" +
125                 "</rpc>");
126     }
127
128
129     @Test
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" +
135                         "<data\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" +
139                         "</xs:schema>\n" +
140                         "</data>\n" +
141                         "</rpc-reply>"
142         ));
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"));
148     }
149
150     @Test
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" +
154                 "<data>\n" +
155                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
156                 "<schemas>\n" +
157                 "<schema>\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" +
161                 "</schema>\n" +
162                 "</schemas>\n" +
163                 "</netconf-state>\n" +
164                 "</data>\n" +
165                 "</rpc-reply>"));
166
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());
171
172         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
173                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
174
175         final Map<QName, Object> keys = Maps.newHashMap();
176         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
177             keys.put(value.getNodeType(), value.getValue());
178         }
179
180         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
181         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
182
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()));
188
189         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
190     }
191
192     @Test
193     public void testGetConfigRequest() throws Exception {
194         final DataContainerChild<?, ?> filter = toFilterStructure(
195                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
196
197         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
198
199         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
200                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
201
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" +
206                 "<schemas/>\n" +
207                 "</netconf-state>" +
208                 "</filter>\n" +
209                 "<source>\n" +
210                 "<running/>\n" +
211                 "</source>\n" +
212                 "</get-config>" +
213                 "</rpc>");
214     }
215
216     @Test
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());
220
221         final Map<QName, Object> keys = Maps.newHashMap();
222         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
223             keys.put(value.getNodeType(), value.getValue());
224         }
225
226         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
227         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
228
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));
231
232         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
233
234         final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
235         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
236
237         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
238                 "<edit-config>\n" +
239                 "<target>\n" +
240                 "<candidate/>\n" +
241                 "</target>\n" +
242                 "<config>\n" +
243                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
244                 "<schemas>\n" +
245                 "<schema>\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" +
249                 "</schema>\n" +
250                 "</schemas>\n" +
251                 "</netconf-state>\n" +
252                 "</config>\n" +
253                 "</edit-config>\n" +
254                 "</rpc>");
255     }
256
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());
261     }
262
263     @Test
264     public void testGetRequest() throws Exception {
265
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);
269
270         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
271                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
272
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" +
277                 "<capabilities>\n" +
278                 "<capability>a:b:c</capability>\n" +
279                 "</capabilities>\n" +
280                 "</netconf-state>" +
281                 "</filter>\n" +
282                 "</get>" +
283                 "</rpc>");
284     }
285
286     private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
287         return new NetconfMessageTransformer(schema);
288     }
289
290     @Test
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>"
294         ));
295         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
296         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
297         assertNull(compositeNodeRpcResult.getResult());
298     }
299
300     public SchemaContext getSchema(boolean addBase) {
301         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
302         if(addBase) {
303             moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
304         }
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();
307     }
308 }