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