Merge "Startup arch - remove artifactId prefix from dir names."
[controller.git] / opendaylight / netconf / netconf-notifications-api / src / main / java / org / opendaylight / controller / 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.controller.netconf.notifications;
10
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;
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     public static final String EVENT_TIME = "eventTime";
27
28     /**
29      * Create new notification and capture the timestamp in the constructor
30      */
31     public NetconfNotification(final Document notificationContent) {
32         this(notificationContent, new Date());
33     }
34
35     /**
36      * Create new notification with provided timestamp
37      */
38     public NetconfNotification(final Document notificationContent, final Date eventTime) {
39         super(wrapNotification(notificationContent, eventTime));
40     }
41
42     private static Document wrapNotification(final Document notificationContent, final Date eventTime) {
43         Preconditions.checkNotNull(notificationContent);
44         Preconditions.checkNotNull(eventTime);
45
46         final Element baseNotification = notificationContent.getDocumentElement();
47         final Element entireNotification = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, NOTIFICATION);
48         entireNotification.appendChild(baseNotification);
49
50         final Element eventTimeElement = notificationContent.createElementNS(NOTIFICATION_NAMESPACE, EVENT_TIME);
51         eventTimeElement.setTextContent(getSerializedEventTime(eventTime));
52         entireNotification.appendChild(eventTimeElement);
53
54         notificationContent.appendChild(entireNotification);
55         return notificationContent;
56     }
57
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);
61     }
62 }