summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/listeners/AbstractQueryParams.java
blob: 469764628cfe452c39b4a6afd8959a9df3eea232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.netconf.sal.streams.listeners;

import static java.util.Objects.requireNonNull;

import com.google.common.annotations.VisibleForTesting;
import java.io.StringReader;
import java.time.Instant;
import java.util.Optional;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
 * Features of query parameters part of both notifications.
 *
 */
abstract class AbstractQueryParams extends AbstractNotificationsData {
    // FIXME: BUG-7956: switch to using UntrustedXML
    private static final DocumentBuilderFactory DBF;

    static {
        final DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        f.setCoalescing(true);
        f.setExpandEntityReferences(false);
        f.setIgnoringElementContentWhitespace(true);
        f.setIgnoringComments(true);
        f.setXIncludeAware(false);
        try {
            f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
            f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
            f.setFeature("http://xml.org/sax/features/external-general-entities", false);
            f.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        } catch (final ParserConfigurationException e) {
            throw new ExceptionInInitializerError(e);
        }
        DBF = f;
    }

    // FIXME: these should be final
    private Instant start = null;
    private Instant stop = null;
    private String filter = null;
    private boolean leafNodesOnly = false;
    private boolean skipNotificationData = false;

    @VisibleForTesting
    public final Instant getStart() {
        return start;
    }

    /**
     * Set query parameters for listener.
     *
     * @param start
     *            start-time of getting notification
     * @param stop
     *            stop-time of getting notification
     * @param filter
     *            indicate which subset of all possible events are of interest
     * @param leafNodesOnly
     *            if true, notifications will contain changes to leaf nodes only
     * @param skipNotificationData
     *            if true, notification will not contain changed data
     */
    @SuppressWarnings("checkstyle:hiddenField")
    public void setQueryParams(final Instant start, final Optional<Instant> stop, final Optional<String> filter,
                               final boolean leafNodesOnly, final boolean skipNotificationData) {
        this.start = requireNonNull(start);
        this.stop = stop.orElse(null);
        this.filter = filter.orElse(null);
        this.leafNodesOnly = leafNodesOnly;
        this.skipNotificationData = skipNotificationData;
    }

    /**
     * Check whether this query should only notify about leaf node changes.
     *
     * @return true if this query should only notify about leaf node changes
     */
    public boolean getLeafNodesOnly() {
        return leafNodesOnly;
    }

    /**
     * Check whether this query should notify changes without data.
     *
     * @return true if this query should notify about changes with  data
     */
    public boolean isSkipNotificationData() {
        return skipNotificationData;
    }

    @SuppressWarnings("checkstyle:IllegalCatch")
    <T extends BaseListenerInterface> boolean checkStartStop(final Instant now, final T listener) {
        if (this.stop != null) {
            if (this.start.compareTo(now) < 0 && this.stop.compareTo(now) > 0) {
                return true;
            }
            if (this.stop.compareTo(now) < 0) {
                try {
                    listener.close();
                } catch (final Exception e) {
                    throw new RestconfDocumentedException("Problem with unregister listener." + e);
                }
            }
        } else if (this.start != null) {
            if (this.start.compareTo(now) < 0) {
                this.start = null;
                return true;
            }
        } else {
            return true;
        }
        return false;
    }

    /**
     * Check if is filter used and then prepare and post data do client.
     *
     * @param xml   data of notification
     */
    @SuppressWarnings("checkstyle:IllegalCatch")
    boolean checkFilter(final String xml) {
        if (this.filter == null) {
            return true;
        }

        try {
            return parseFilterParam(xml);
        } catch (final Exception e) {
            throw new RestconfDocumentedException("Problem while parsing filter.", e);
        }
    }

    /**
     * Parse and evaluate filter value by xml.
     *
     * @return true or false - depends on filter expression and data of
     *         notifiaction
     * @throws Exception if operation fails
     */
    private boolean parseFilterParam(final String xml) throws Exception {
        final Document docOfXml = DBF.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));
        final XPath xPath = XPathFactory.newInstance().newXPath();
        // FIXME: BUG-7956: xPath.setNamespaceContext(nsContext);
        return (boolean) xPath.compile(this.filter).evaluate(docOfXml, XPathConstants.BOOLEAN);
    }
}