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