Switch time keeping to java.time interfaces
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / netconf / sal / streams / listeners / AbstractQueryParams.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.netconf.sal.streams.listeners;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import java.io.StringReader;
13 import java.time.Instant;
14 import java.util.Optional;
15 import javax.xml.XMLConstants;
16 import javax.xml.parsers.DocumentBuilderFactory;
17 import javax.xml.parsers.ParserConfigurationException;
18 import javax.xml.xpath.XPath;
19 import javax.xml.xpath.XPathConstants;
20 import javax.xml.xpath.XPathFactory;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.w3c.dom.Document;
23 import org.xml.sax.InputSource;
24
25 /**
26  * Features of query parameters part of both notifications
27  *
28  */
29 abstract class AbstractQueryParams extends AbstractNotificationsData {
30     // FIXME: BUG-7956: switch to using UntrustedXML
31     private static final DocumentBuilderFactory DBF;
32     static {
33         final DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
34         f.setCoalescing(true);
35         f.setExpandEntityReferences(false);
36         f.setIgnoringElementContentWhitespace(true);
37         f.setIgnoringComments(true);
38         f.setXIncludeAware(false);
39         try {
40             f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
41             f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
42             f.setFeature("http://xml.org/sax/features/external-general-entities", false);
43             f.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
44         } catch (final ParserConfigurationException e) {
45             throw new ExceptionInInitializerError(e);
46         }
47         DBF = f;
48     }
49
50     // FIXME: these should be final
51     private Instant start = null;
52     private Instant stop = null;
53     private String filter = null;
54
55     @VisibleForTesting
56     public final Instant getStart() {
57         return start;
58     }
59
60     /**
61      * Set query parameters for listener
62      *
63      * @param start
64      *            - start-time of getting notification
65      * @param stop
66      *            - stop-time of getting notification
67      * @param filter
68      *            - indicate which subset of all possible events are of interest
69      */
70     public void setQueryParams(final Instant start, final Optional<Instant> stop, final Optional<String> filter) {
71         this.start = Preconditions.checkNotNull(start);
72         this.stop = stop.orElse(null);
73         this.filter = filter.orElse(null);
74     }
75
76     /**
77      * Checking query parameters on specific notification
78      *
79      * @param xml
80      *            - data of notification
81      * @param listener
82      *            - listener of notification
83      * @return true if notification meets the requirements of query parameters,
84      *         false otherwise
85      */
86     protected <T extends BaseListenerInterface> boolean checkQueryParams(final String xml, final T listener) {
87         final Instant now = Instant.now();
88         if (this.stop != null) {
89             if ((this.start.compareTo(now) < 0) && (this.stop.compareTo(now) > 0)) {
90                 return checkFilter(xml);
91             }
92             if (this.stop.compareTo(now) < 0) {
93                 try {
94                     listener.close();
95                 } catch (final Exception e) {
96                     throw new RestconfDocumentedException("Problem with unregister listener." + e);
97                 }
98             }
99         } else if (this.start != null) {
100             if (this.start.compareTo(now) < 0) {
101                 this.start = null;
102                 return checkFilter(xml);
103             }
104         } else {
105             return checkFilter(xml);
106         }
107         return false;
108     }
109
110     /**
111      * Check if is filter used and then prepare and post data do client
112      *
113      * @param change
114      *            - data of notification
115      */
116     private boolean checkFilter(final String xml) {
117         if (this.filter == null) {
118             return true;
119         }
120
121         try {
122             return parseFilterParam(xml);
123         } catch (final Exception e) {
124             throw new RestconfDocumentedException("Problem while parsing filter.", e);
125         }
126     }
127
128     /**
129      * Parse and evaluate filter value by xml
130      *
131      * @return true or false - depends on filter expression and data of
132      *         notifiaction
133      * @throws Exception
134      */
135     private boolean parseFilterParam(final String xml) throws Exception {
136         final Document docOfXml = DBF.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
137         final XPath xPath = XPathFactory.newInstance().newXPath();
138         // FIXME: BUG-7956: xPath.setNamespaceContext(nsContext);
139         return (boolean) xPath.compile(this.filter).evaluate(docOfXml, XPathConstants.BOOLEAN);
140     }
141 }