Add support for notification subtree filter
[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     private final Date eventTime;
38
39     /**
40      * Create new notification and capture the timestamp in the constructor
41      */
42     public NetconfNotification(final Document notificationContent) {
43         this(notificationContent, new Date());
44     }
45
46     /**
47      * Create new notification with provided timestamp
48      */
49     public NetconfNotification(final Document notificationContent, final Date eventTime) {
50         super(wrapNotification(notificationContent, eventTime));
51         this.eventTime = eventTime;
52     }
53
54     /**
55      * @return notification event time
56      */
57     public Date getEventTime() {
58         return eventTime;
59     }
60
61     private static Document wrapNotification(final Document notificationContent, final Date eventTime) {
62         Preconditions.checkNotNull(notificationContent);
63         Preconditions.checkNotNull(eventTime);
64
65         final Element baseNotification = notificationContent.getDocumentElement();
66         final Element entireNotification = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, NOTIFICATION);
67         entireNotification.appendChild(baseNotification);
68
69         final Element eventTimeElement = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, EVENT_TIME);
70         eventTimeElement.setTextContent(getSerializedEventTime(eventTime));
71         entireNotification.appendChild(eventTimeElement);
72
73         notificationContent.appendChild(entireNotification);
74         return notificationContent;
75     }
76
77     private static String getSerializedEventTime(final Date eventTime) {
78         // SimpleDateFormat is not threadsafe, cannot be in a constant
79         return new SimpleDateFormat(RFC3339_DATE_FORMAT_BLUEPRINT).format(eventTime);
80     }
81 }