Merge "Bug 2806 - Immediate and infinite reconnect attempts during negotiation" into...
[netconf.git] / opendaylight / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / 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.netconf.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.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_CONTENT;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.CREATE_SUBSCRIPTION_RPC_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.GET_SCHEMA_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_CANDIDATE_QNAME;
20 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME;
21 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DATA_QNAME;
22 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME;
23 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME;
24 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_CONFIG_QNAME;
25 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
26 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_LOCK_QNAME;
27 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_RUNNING_QNAME;
28 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.createEditConfigStructure;
29 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toFilterStructure;
30 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toId;
31 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
32
33 import com.google.common.base.Optional;
34 import com.google.common.collect.Iterables;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Maps;
37 import java.io.IOException;
38 import java.util.Collections;
39 import java.util.List;
40 import java.util.Map;
41 import javax.xml.transform.dom.DOMSource;
42 import org.custommonkey.xmlunit.Diff;
43 import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
44 import org.custommonkey.xmlunit.XMLUnit;
45 import org.hamcrest.CoreMatchers;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.opendaylight.controller.config.util.xml.XmlUtil;
49 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
50 import org.opendaylight.netconf.api.NetconfMessage;
51 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
52 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
53 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
59 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
66 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
67 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
68 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
69 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
70 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
71 import org.w3c.dom.Element;
72 import org.xml.sax.SAXException;
73
74 public class NetconfMessageTransformerTest {
75
76     private NetconfMessageTransformer netconfMessageTransformer;
77     private SchemaContext schema;
78
79     @Before
80     public void setUp() throws Exception {
81         XMLUnit.setIgnoreWhitespace(true);
82         XMLUnit.setIgnoreAttributeOrder(true);
83         XMLUnit.setIgnoreComments(true);
84
85         schema = getSchema(true);
86         netconfMessageTransformer = getTransformer(schema);
87
88     }
89
90     @Test
91     public void testLockRequestBaseSchemaNotPresent() throws Exception {
92         final SchemaContext partialSchema = getSchema(false);
93         final NetconfMessageTransformer transformer = getTransformer(partialSchema);
94         final NetconfMessage netconfMessage = transformer.toRpcRequest(toPath(NETCONF_LOCK_QNAME),
95                 NetconfBaseOps.getLockContent(NETCONF_CANDIDATE_QNAME));
96
97         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<lock"));
98         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
99     }
100
101     @Test
102     public void testCreateSubscriberNotificationSchemaNotPresent() throws Exception {
103         final SchemaContext partialSchema = getSchema(true);
104         final NetconfMessageTransformer transformer = new NetconfMessageTransformer(
105                 partialSchema,
106                 true,
107                 NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS
108         );
109         NetconfMessage netconfMessage = transformer.toRpcRequest(
110                 toPath(CREATE_SUBSCRIPTION_RPC_QNAME),
111                 CREATE_SUBSCRIPTION_RPC_CONTENT
112         );
113         String documentString = XmlUtil.toString(netconfMessage.getDocument());
114         assertThat(documentString, CoreMatchers.containsString("<create-subscription"));
115         assertThat(documentString, CoreMatchers.containsString("<rpc"));
116     }
117
118     @Test
119     public void tesLockSchemaRequest() throws Exception {
120         final SchemaContext partialSchema = getSchema(false);
121         final NetconfMessageTransformer transformer = getTransformer(partialSchema);
122         final String result = "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>";
123
124         transformer.toRpcResult(new NetconfMessage(XmlUtil.readXmlToDocument(result)), toPath(NETCONF_LOCK_QNAME));
125     }
126
127     @Test
128     public void testDiscardChangesRequest() throws Exception {
129         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
130         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
131         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
132         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
133     }
134
135     @Test
136     public void tesGetSchemaRequest() throws Exception {
137         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
138                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
139         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
140                 "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
141                 "<format>yang</format>\n" +
142                 "<identifier>module</identifier>\n" +
143                 "<version>2012-12-12</version>\n" +
144                 "</get-schema>\n" +
145                 "</rpc>");
146     }
147
148     @Test
149     public void tesGetSchemaResponse() throws Exception {
150         final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
151         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
152                 "<rpc-reply message-id=\"101\"\n" +
153                         "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
154                         "<data\n" +
155                         "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
156                         "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
157                         "Random YANG SCHEMA\n" +
158                         "</xs:schema>\n" +
159                         "</data>\n" +
160                         "</rpc-reply>"
161         ));
162         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
163         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
164         assertNotNull(compositeNodeRpcResult.getResult());
165         final DOMSource schemaContent = ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
166         assertThat(((Element) schemaContent.getNode()).getTextContent(), CoreMatchers.containsString("Random YANG SCHEMA"));
167     }
168
169     @Test
170     public void testGetConfigResponse() throws Exception {
171         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n" +
172                 "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
173                 "<data>\n" +
174                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
175                 "<schemas>\n" +
176                 "<schema>\n" +
177                 "<identifier>module</identifier>\n" +
178                 "<version>2012-12-12</version>\n" +
179                 "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n" +
180                 "</schema>\n" +
181                 "</schemas>\n" +
182                 "</netconf-state>\n" +
183                 "</data>\n" +
184                 "</rpc-reply>"));
185
186         final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
187         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
188         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
189         assertNotNull(compositeNodeRpcResult.getResult());
190
191         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
192                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
193
194         final Map<QName, Object> keys = Maps.newHashMap();
195         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
196             keys.put(value.getNodeType(), value.getValue());
197         }
198
199         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
200         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
201
202         final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
203         final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
204         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
205         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
206         assertEquals(1, Iterables.size(schemaParent.getValue()));
207
208         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
209     }
210
211     @Test
212     public void testGetConfigRequest() throws Exception {
213         final DataContainerChild<?, ?> filter = toFilterStructure(
214                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
215
216         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
217
218         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
219                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
220
221         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
222                 "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
223                 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
224                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
225                 "<schemas/>\n" +
226                 "</netconf-state>" +
227                 "</filter>\n" +
228                 "<source>\n" +
229                 "<running/>\n" +
230                 "</source>\n" +
231                 "</get-config>" +
232                 "</rpc>");
233     }
234
235     @Test
236     public void testEditConfigRequest() throws Exception {
237         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
238             NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
239
240         final Map<QName, Object> keys = Maps.newHashMap();
241         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
242             keys.put(value.getNodeType(), value.getValue());
243         }
244
245         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates = new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
246         final MapEntryNode schemaNode = Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
247
248         final YangInstanceIdentifier id = YangInstanceIdentifier.builder().node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME).nodeWithKey(Schema.QNAME, keys).build();
249         final DataContainerChild<?, ?> editConfigStructure = createEditConfigStructure(NetconfMessageTransformer.BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id, Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
250
251         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
252
253         final ContainerNode wrap = NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
254         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
255
256         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
257                 "<edit-config>\n" +
258                 "<target>\n" +
259                 "<candidate/>\n" +
260                 "</target>\n" +
261                 "<config>\n" +
262                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
263                 "<schemas>\n" +
264                 "<schema>\n" +
265                 "<identifier>module</identifier>\n" +
266                 "<version>2012-12-12</version>\n" +
267                 "<format>yang</format>\n" +
268                 "</schema>\n" +
269                 "</schemas>\n" +
270                 "</netconf-state>\n" +
271                 "</config>\n" +
272                 "</edit-config>\n" +
273                 "</rpc>");
274     }
275
276     private void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent) throws SAXException, IOException {
277         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
278         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
279         assertTrue(diff.toString(), diff.similar());
280     }
281
282     @Test
283     public void testGetRequest() throws Exception {
284
285         final QName capability = QName.create(Capabilities.QNAME, "capability");
286         final DataContainerChild<?, ?> filter = toFilterStructure(
287                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability), new YangInstanceIdentifier.NodeWithValue(capability, "a:b:c")), schema);
288
289         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
290                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
291
292         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
293                 "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
294                 "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n" +
295                 "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
296                 "<capabilities>\n" +
297                 "<capability>a:b:c</capability>\n" +
298                 "</capabilities>\n" +
299                 "</netconf-state>" +
300                 "</filter>\n" +
301                 "</get>" +
302                 "</rpc>");
303     }
304
305     private NetconfMessageTransformer getTransformer(final SchemaContext schema) {
306         return new NetconfMessageTransformer(schema, true);
307     }
308
309     @Test
310     public void testCommitResponse() throws Exception {
311         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
312                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
313         ));
314         final DOMRpcResult compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
315         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
316         assertNull(compositeNodeRpcResult.getResult());
317     }
318
319     public SchemaContext getSchema(boolean addBase) {
320         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
321         if(addBase) {
322             moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
323         }
324         moduleInfoBackedContext.addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
325         return moduleInfoBackedContext.tryToCreateSchemaContext().get();
326     }
327 }