Remove opendaylight directory
[netconf.git] / netconf / netconf-notifications-api / src / main / java / org / opendaylight / netconf / notifications / NetconfNotification.java
1 /*
2  * Copyright (c) 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.notifications;
10
11 import com.google.common.base.Preconditions;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14 import org.opendaylight.netconf.api.NetconfMessage;
15 import org.w3c.dom.Document;
16 import org.w3c.dom.Element;
17
18 /**
19  * Special kind of netconf message that contains a timestamp.
20  */
21 public final class NetconfNotification extends NetconfMessage {
22
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     // The format with milliseconds is a bit fragile, it cannot be used for timestamps without millis (thats why its a separate format)
27     // + it might not work properly with more than 6 digits
28     // TODO try to find a better solution with Java8
29     public static final String RFC3339_DATE_FORMAT_WITH_MILLIS_BLUEPRINT = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX";
30     public static final String EVENT_TIME = "eventTime";
31
32     /**
33      * Used for unknown/un-parse-able event-times
34      */
35     public static final Date UNKNOWN_EVENT_TIME = new Date(0);
36
37     /**
38      * Create new notification and capture the timestamp in the constructor
39      */
40     public NetconfNotification(final Document notificationContent) {
41         this(notificationContent, new Date());
42     }
43
44     /**
45      * Create new notification with provided timestamp
46      */
47     public NetconfNotification(final Document notificationContent, final Date eventTime) {
48         super(wrapNotification(notificationContent, eventTime));
49     }
50
51     private static Document wrapNotification(final Document notificationContent, final Date eventTime) {
52         Preconditions.checkNotNull(notificationContent);
53         Preconditions.checkNotNull(eventTime);
54
55         final Element baseNotification = notificationContent.getDocumentElement();
56         final Element entireNotification = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, NOTIFICATION);
57         entireNotification.appendChild(baseNotification);
58
59         final Element eventTimeElement = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, EVENT_TIME);
60         eventTimeElement.setTextContent(getSerializedEventTime(eventTime));
61         entireNotification.appendChild(eventTimeElement);
62
63         notificationContent.appendChild(entireNotification);
64         return notificationContent;
65     }
66
67     private static String getSerializedEventTime(final Date eventTime) {
68         // SimpleDateFormat is not threadsafe, cannot be in a constant
69         return new SimpleDateFormat(RFC3339_DATE_FORMAT_BLUEPRINT).format(eventTime);
70     }
71 }