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.controller.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.Collections;
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.controller.netconf.api.NetconfMessage;
28 import org.opendaylight.controller.netconf.notifications.NetconfNotification;
29 import org.opendaylight.controller.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.model.parser.api.YangContextParser;
34 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
35 import org.w3c.dom.Document;
37 public class NetconfToNotificationTest {
39 NetconfMessageTransformer messageTransformer;
41 NetconfMessage userNotification;
43 @SuppressWarnings("deprecation")
45 public void setup() throws Exception {
46 final SchemaContext schemaContext = getNotificationSchemaContext(getClass());
48 messageTransformer = new NetconfMessageTransformer(schemaContext, true);
50 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
51 factory.setNamespaceAware(true);
52 InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml");
53 assertNotNull(notifyPayloadStream);
55 final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream);
57 userNotification = new NetconfMessage(doc);
60 static SchemaContext getNotificationSchemaContext(Class<?> loadClass) {
61 final List<InputStream> modelsToParse = Collections.singletonList(loadClass.getResourceAsStream("/schemas/user-notification.yang"));
62 final YangContextParser parser = new YangParserImpl();
63 final Set<Module> modules = parser.parseYangModelsFromStreams(modelsToParse);
64 assertTrue(!modules.isEmpty());
65 final SchemaContext schemaContext = parser.resolveSchemaContext(modules);
66 assertNotNull(schemaContext);
71 public void test() throws Exception {
72 final DOMNotification domNotification = messageTransformer.toNotification(userNotification);
73 final ContainerNode root = domNotification.getBody();
75 assertEquals(6, Iterables.size(root.getValue()));
76 assertEquals("user-visited-page", root.getNodeType().getLocalName());
77 assertEquals(new SimpleDateFormat(NetconfNotification.RFC3339_DATE_FORMAT_BLUEPRINT).parse("2007-07-08T00:01:00Z"),
78 ((DOMEvent) domNotification).getEventTime());