Add 'features/protocol-framework/' from commit 'cb42405784db97d0ce2c5991d12a89b46d185949'
[netconf.git] / 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.mdsal.binding.generator.impl.ModuleInfoBackedContext;
51 import org.opendaylight.netconf.api.NetconfMessage;
52 import org.opendaylight.netconf.sal.connect.netconf.schema.NetconfRemoteSchemaYangSourceProvider;
53 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
54 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.$YangModuleInfoImpl;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
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                 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 =
130                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_DISCARD_CHANGES_QNAME), null);
131         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<discard"));
132         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<rpc"));
133         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("message-id"));
134     }
135
136     @Test
137     public void tesGetSchemaRequest() throws Exception {
138         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(GET_SCHEMA_QNAME),
139                 NetconfRemoteSchemaYangSourceProvider.createGetSchemaRequest("module", Optional.of("2012-12-12")));
140         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
141                 + "<get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
142                 + "<format>yang</format>\n"
143                 + "<identifier>module</identifier>\n"
144                 + "<version>2012-12-12</version>\n"
145                 + "</get-schema>\n"
146                 + "</rpc>");
147     }
148
149     @Test
150     public void tesGetSchemaResponse() throws Exception {
151         final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
152         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
153                 "<rpc-reply message-id=\"101\"\n"
154                         + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
155                         + "<data\n"
156                         + "xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
157                         + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
158                         + "Random YANG SCHEMA\n"
159                         + "</xs:schema>\n"
160                         + "</data>\n"
161                         + "</rpc-reply>"
162         ));
163         final DOMRpcResult compositeNodeRpcResult =
164                 netconfMessageTransformer.toRpcResult(response, toPath(GET_SCHEMA_QNAME));
165         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
166         assertNotNull(compositeNodeRpcResult.getResult());
167         final DOMSource schemaContent =
168             ((AnyXmlNode) ((ContainerNode) compositeNodeRpcResult.getResult()).getValue().iterator().next()).getValue();
169         assertThat(((Element) schemaContent.getNode()).getTextContent(),
170                 CoreMatchers.containsString("Random YANG SCHEMA"));
171     }
172
173     @Test
174     public void testGetConfigResponse() throws Exception {
175         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc-reply message-id=\"101\"\n"
176                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
177                 + "<data>\n"
178                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
179                 + "<schemas>\n"
180                 + "<schema>\n"
181                 + "<identifier>module</identifier>\n"
182                 + "<version>2012-12-12</version>\n"
183                 + "<format xmlns:x=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">x:yang</format>\n"
184                 + "</schema>\n"
185                 + "</schemas>\n"
186                 + "</netconf-state>\n"
187                 + "</data>\n"
188                 + "</rpc-reply>"));
189
190         final NetconfMessageTransformer netconfMessageTransformer = getTransformer(getSchema(true));
191         final DOMRpcResult compositeNodeRpcResult =
192                 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_GET_CONFIG_QNAME));
193         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
194         assertNotNull(compositeNodeRpcResult.getResult());
195
196         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
197                 NetconfRemoteSchemaYangSourceProvider
198                         .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 =
206                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
207         final MapEntryNode schemaNode =
208                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
209
210         final ContainerNode data = (ContainerNode) ((ContainerNode) compositeNodeRpcResult
211                 .getResult()).getChild(toId(NETCONF_DATA_QNAME)).get();
212         final ContainerNode state = (ContainerNode) data.getChild(toId(NetconfState.QNAME)).get();
213         final ContainerNode schemas = (ContainerNode) state.getChild(toId(Schemas.QNAME)).get();
214         final MapNode schemaParent = (MapNode) schemas.getChild(toId(Schema.QNAME)).get();
215         assertEquals(1, Iterables.size(schemaParent.getValue()));
216
217         assertEquals(schemaNode, schemaParent.getValue().iterator().next());
218     }
219
220     @Test
221     public void testGetConfigRequest() throws Exception {
222         final DataContainerChild<?, ?> filter = toFilterStructure(
223                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Schemas.QNAME)), schema);
224
225         final DataContainerChild<?, ?> source = NetconfBaseOps.getSourceNode(NETCONF_RUNNING_QNAME);
226
227         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_CONFIG_QNAME),
228                 NetconfMessageTransformUtil.wrap(NETCONF_GET_CONFIG_QNAME, source, filter));
229
230         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
231                 + "<get-config xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
232                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
233                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
234                 + "<schemas/>\n"
235                 + "</netconf-state>"
236                 + "</filter>\n"
237                 + "<source>\n"
238                 + "<running/>\n"
239                 + "</source>\n"
240                 + "</get-config>"
241                 + "</rpc>");
242     }
243
244     @Test
245     public void testEditConfigRequest() throws Exception {
246         final List<DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> values = Lists.newArrayList(
247                 NetconfRemoteSchemaYangSourceProvider
248                         .createGetSchemaRequest("module", Optional.of("2012-12-12")).getValue());
249
250         final Map<QName, Object> keys = Maps.newHashMap();
251         for (final DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?> value : values) {
252             keys.put(value.getNodeType(), value.getValue());
253         }
254
255         final YangInstanceIdentifier.NodeIdentifierWithPredicates identifierWithPredicates =
256                 new YangInstanceIdentifier.NodeIdentifierWithPredicates(Schema.QNAME, keys);
257         final MapEntryNode schemaNode =
258                 Builders.mapEntryBuilder().withNodeIdentifier(identifierWithPredicates).withValue(values).build();
259
260         final YangInstanceIdentifier id = YangInstanceIdentifier.builder()
261                 .node(NetconfState.QNAME).node(Schemas.QNAME).node(Schema.QNAME)
262                 .nodeWithKey(Schema.QNAME, keys).build();
263         final DataContainerChild<?, ?> editConfigStructure =
264                 createEditConfigStructure(BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext(), id,
265                         Optional.<ModifyAction>absent(), Optional.<NormalizedNode<?, ?>>fromNullable(schemaNode));
266
267         final DataContainerChild<?, ?> target = NetconfBaseOps.getTargetNode(NETCONF_CANDIDATE_QNAME);
268
269         final ContainerNode wrap =
270                 NetconfMessageTransformUtil.wrap(NETCONF_EDIT_CONFIG_QNAME, editConfigStructure, target);
271         final NetconfMessage netconfMessage =
272                 netconfMessageTransformer.toRpcRequest(toPath(NETCONF_EDIT_CONFIG_QNAME), wrap);
273
274         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
275                 + "<edit-config>\n"
276                 + "<target>\n"
277                 + "<candidate/>\n"
278                 + "</target>\n"
279                 + "<config>\n"
280                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
281                 + "<schemas>\n"
282                 + "<schema>\n"
283                 + "<identifier>module</identifier>\n"
284                 + "<version>2012-12-12</version>\n"
285                 + "<format>yang</format>\n"
286                 + "</schema>\n"
287                 + "</schemas>\n"
288                 + "</netconf-state>\n"
289                 + "</config>\n"
290                 + "</edit-config>\n"
291                 + "</rpc>");
292     }
293
294     private static void assertSimilarXml(final NetconfMessage netconfMessage, final String xmlContent)
295             throws SAXException, IOException {
296         final Diff diff = XMLUnit.compareXML(netconfMessage.getDocument(), XmlUtil.readXmlToDocument(xmlContent));
297         diff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
298         assertTrue(diff.toString(), diff.similar());
299     }
300
301     @Test
302     public void testGetRequest() throws Exception {
303
304         final QName capability = QName.create(Capabilities.QNAME, "capability");
305         final DataContainerChild<?, ?> filter = toFilterStructure(
306                 YangInstanceIdentifier.create(toId(NetconfState.QNAME), toId(Capabilities.QNAME), toId(capability),
307                     new YangInstanceIdentifier.NodeWithValue<>(capability, "a:b:c")), schema);
308
309         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(toPath(NETCONF_GET_QNAME),
310                 NetconfMessageTransformUtil.wrap(NETCONF_GET_QNAME, filter));
311
312         assertSimilarXml(netconfMessage, "<rpc message-id=\"m-0\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
313                 + "<get xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
314                 + "<filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
315                 + "<netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
316                 + "<capabilities>\n"
317                 + "<capability>a:b:c</capability>\n"
318                 + "</capabilities>\n"
319                 + "</netconf-state>"
320                 + "</filter>\n"
321                 + "</get>"
322                 + "</rpc>");
323     }
324
325     private static NetconfMessageTransformer getTransformer(final SchemaContext schema) {
326         return new NetconfMessageTransformer(schema, true);
327     }
328
329     @Test
330     public void testCommitResponse() throws Exception {
331         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
332                 "<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><ok/></rpc-reply>"
333         ));
334         final DOMRpcResult compositeNodeRpcResult =
335                 netconfMessageTransformer.toRpcResult(response, toPath(NETCONF_COMMIT_QNAME));
336         assertTrue(compositeNodeRpcResult.getErrors().isEmpty());
337         assertNull(compositeNodeRpcResult.getResult());
338     }
339
340     public SchemaContext getSchema(final boolean addBase) {
341         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
342         if (addBase) {
343             moduleInfoBackedContext.addModuleInfos(Collections.singleton($YangModuleInfoImpl.getInstance()));
344         }
345         moduleInfoBackedContext
346                 .addModuleInfos(Collections.singleton(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf
347                         .netconf.monitoring.rev101004.$YangModuleInfoImpl.getInstance()));
348         return moduleInfoBackedContext.tryToCreateSchemaContext().get();
349     }
350 }