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