Replace old yang parser usages in netconf.
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / NetconfToNotificationTest.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;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.collect.Iterables;
16 import java.io.InputStream;
17 import java.text.SimpleDateFormat;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Set;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.controller.config.util.xml.XmlUtil;
25 import org.opendaylight.controller.md.sal.dom.api.DOMEvent;
26 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
27 import org.opendaylight.netconf.api.NetconfMessage;
28 import org.opendaylight.netconf.notifications.NetconfNotification;
29 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
36 import org.w3c.dom.Document;
37
38 public class NetconfToNotificationTest {
39
40     NetconfMessageTransformer messageTransformer;
41
42     NetconfMessage userNotification;
43
44     @SuppressWarnings("deprecation")
45     @Before
46     public void setup() throws Exception {
47         final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48         factory.setNamespaceAware(true);
49         InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
50         assertNotNull(notifyPayloadStream);
51
52         final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
53         assertNotNull(doc);
54         userNotification = new NetconfMessage(doc);
55     }
56
57     static SchemaContext getNotificationSchemaContext(Class<?> loadClass, boolean getExceptionTest) {
58         final List<InputStream> modelsToParse = new ArrayList<>();
59
60         if (getExceptionTest) {
61             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification4.yang"));
62             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification3.yang"));
63         } else {
64             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
65             modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification2.yang"));
66         }
67
68         final SchemaContext context = parseYangStreams(modelsToParse);
69         final Set<Module> modules = context.getModules();
70         assertTrue(!modules.isEmpty());
71         assertNotNull(context);
72         return context;
73     }
74
75     private static SchemaContext parseYangStreams(final List<InputStream> streams) {
76         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
77                 .newBuild();
78         final SchemaContext schemaContext;
79         try {
80             schemaContext = reactor.buildEffective(streams);
81         } catch (ReactorException e) {
82             throw new RuntimeException("Unable to build schema context from " + streams, e);
83         }
84         return schemaContext;
85     }
86
87     @Test(expected =  IllegalStateException.class)
88     public void testMostRecentWrongYangModel() throws Exception {
89         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), true);
90         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
91         messageTransformer.toNotification(userNotification);
92     }
93
94     @Test
95     public void testToNotificationFunction() throws Exception {
96         final SchemaContext schemaContext = getNotificationSchemaContext(getClass(), false);
97         messageTransformer = new NetconfMessageTransformer(schemaContext, true);
98         final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
99         final ContainerNode root = domNotification.getBody();
100         assertNotNull(root);
101         assertEquals(6, Iterables.size(root.getValue()));
102         assertEquals("user-visited-page", root.getNodeType().getLocalName());
103         assertEquals(new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT).parse("2015-10-23T09:42:27.67175+00:00"),
104                 ((DOMEvent) domNotification).getEventTime());
105     }
106 }