blob: 2ebe17edc1cd00236819bbdb7703dd551b34037e [file] [log] [blame]
Laszlo Papp35cc7c42018-09-17 07:41:09 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.incubator.net.faultmanagement.alarm;
17
18import org.slf4j.Logger;
19import org.slf4j.LoggerFactory;
20
21import org.xml.sax.InputSource;
22import org.xml.sax.SAXException;
23import org.w3c.dom.Document;
24import org.w3c.dom.Node;
25
26import javax.xml.parsers.DocumentBuilder;
27import javax.xml.parsers.DocumentBuilderFactory;
28import javax.xml.parsers.ParserConfigurationException;
29import java.io.IOException;
30import java.io.InputStream;
Laszlo Pappf1a7b7f2018-09-19 07:38:00 +000031import java.time.DateTimeException;
32import java.time.OffsetDateTime;
Laszlo Papp35cc7c42018-09-17 07:41:09 +010033import java.time.format.DateTimeFormatter;
34
35/**
36 * Parser for Netconf notifications.
37 */
38public final class XmlEventParser {
39 public static final Logger log = LoggerFactory
40 .getLogger(XmlEventParser.class);
41
42 private static final String DISALLOW_DTD_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
43 private static final String DISALLOW_EXTERNAL_DTD =
44 "http://apache.org/xml/features/nonvalidating/load-external-dtd";
45 private static final String EVENTTIME_TAGNAME = "eventTime";
46
47 private XmlEventParser() {
48 }
49
50 /**
51 * Creates a document from the input stream message and returns the result.
52 *
53 * @param message input stream message
54 * @return the document result
55 * @throws SAXException Throws SAX Exception
56 * @throws IOException Throws IO Exception
57 * @throws ParserConfigurationException Throws ParserConfigurationException
58 */
59 public static Document createDocFromMessage(InputStream message)
60 throws SAXException, IOException, ParserConfigurationException {
61 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
62 //Disabling DTDs in order to avoid XXE xml-based attacks.
63 disableFeature(dbfactory, DISALLOW_DTD_FEATURE);
64 disableFeature(dbfactory, DISALLOW_EXTERNAL_DTD);
65 dbfactory.setXIncludeAware(false);
66 dbfactory.setExpandEntityReferences(false);
67 DocumentBuilder builder = dbfactory.newDocumentBuilder();
68 return builder.parse(new InputSource(message));
69 }
70
71 private static void disableFeature(DocumentBuilderFactory dbfactory, String feature) {
72 try {
73 dbfactory.setFeature(feature, true);
74 } catch (ParserConfigurationException e) {
75 // This should catch a failed setFeature feature
76 log.info("ParserConfigurationException was thrown. The feature '" +
77 feature + "' is probably not supported by your XML processor.");
78 }
79 }
80
Laszlo Pappf1a7b7f2018-09-19 07:38:00 +000081 public static long getEventTime(String dateTime)
82 throws UnsupportedOperationException, IllegalArgumentException {
83 try {
84 OffsetDateTime date = OffsetDateTime.parse(dateTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
85 return date.toInstant().toEpochMilli();
86 } catch (DateTimeException e) {
87 log.error("Cannot parse exception {} {}", dateTime, e);
88 }
89 return System.currentTimeMillis();
90 }
91
Laszlo Papp35cc7c42018-09-17 07:41:09 +010092 public static long getEventTime(Document doc)
93 throws UnsupportedOperationException, IllegalArgumentException {
94 String dateTime = getEventTimeNode(doc).getTextContent();
Laszlo Pappf1a7b7f2018-09-19 07:38:00 +000095 return getEventTime(dateTime);
Laszlo Papp35cc7c42018-09-17 07:41:09 +010096 }
97
98 public static Node getDescriptionNode(Document doc) {
99 return getEventTimeNode(doc).getNextSibling();
100 }
101
102 private static Node getEventTimeNode(Document doc) {
103 return doc.getElementsByTagName(EVENTTIME_TAGNAME).item(0);
104 }
105}