2 * Copyright (c) 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.netconf.notifications;
11 import com.google.common.base.Preconditions;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14 import org.opendaylight.controller.netconf.api.NetconfMessage;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
19 * Special kind of netconf message that contains a timestamp.
21 public final class NetconfNotification extends NetconfMessage {
23 public static final String NOTIFICATION = "notification";
24 public static final String NOTIFICATION_NAMESPACE = "urn:ietf:params:netconf:capability:notification:1.0";
25 public static final String RFC3339_DATE_FORMAT_BLUEPRINT = "yyyy-MM-dd'T'HH:mm:ssXXX";
26 public static final String EVENT_TIME = "eventTime";
29 * Create new notification and capture the timestamp in the constructor
31 public NetconfNotification(final Document notificationContent) {
32 this(notificationContent, new Date());
36 * Create new notification with provided timestamp
38 public NetconfNotification(final Document notificationContent, final Date eventTime) {
39 super(wrapNotification(notificationContent, eventTime));
42 private static Document wrapNotification(final Document notificationContent, final Date eventTime) {
43 Preconditions.checkNotNull(notificationContent);
44 Preconditions.checkNotNull(eventTime);
46 final Element baseNotification = notificationContent.getDocumentElement();
47 final Element entireNotification = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, NOTIFICATION);
48 entireNotification.appendChild(baseNotification);
50 final Element eventTimeElement = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, EVENT_TIME);
51 eventTimeElement.setTextContent(getSerializedEventTime(eventTime));
52 entireNotification.appendChild(eventTimeElement);
54 notificationContent.appendChild(entireNotification);
55 return notificationContent;
58 private static String getSerializedEventTime(final Date eventTime) {
59 // SimpleDateFormat is not threadsafe, cannot be in a constant
60 return new SimpleDateFormat(RFC3339_DATE_FORMAT_BLUEPRINT).format(eventTime);