2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.sal.connect.netconf;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
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;
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;
38 public class NetconfToNotificationTest {
40 NetconfMessageTransformer messageTransformer;
42 NetconfMessage userNotification;
44 @SuppressWarnings("deprecation")
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);
52 final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
54 userNotification = new NetconfMessage(doc);
57 static SchemaContext getNotificationSchemaContext(Class<?> loadClass, boolean getExceptionTest) {
58 final List<InputStream> modelsToParse = new ArrayList<>();
60 if (getExceptionTest) {
61 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification4.yang"));
62 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification3.yang"));
64 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
65 modelsToParse.add(loadClass.getResourceAsStream("/schemas/user-notification2.yang"));
68 final SchemaContext context = parseYangStreams(modelsToParse);
69 final Set<Module> modules = context.getModules();
70 assertTrue(!modules.isEmpty());
71 assertNotNull(context);
75 private static SchemaContext parseYangStreams(final List<InputStream> streams) {
76 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
78 final SchemaContext schemaContext;
80 schemaContext = reactor.buildEffective(streams);
81 } catch (ReactorException e) {
82 throw new RuntimeException("Unable to build schema context from " + streams, e);
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);
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();
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());