Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 16 | package org.onosproject.alarm; |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 17 | |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import org.xml.sax.InputSource; |
| 22 | import org.xml.sax.SAXException; |
| 23 | import org.w3c.dom.Document; |
| 24 | import org.w3c.dom.Node; |
| 25 | |
| 26 | import javax.xml.parsers.DocumentBuilder; |
| 27 | import javax.xml.parsers.DocumentBuilderFactory; |
| 28 | import javax.xml.parsers.ParserConfigurationException; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
Laszlo Papp | f1a7b7f | 2018-09-19 07:38:00 +0000 | [diff] [blame] | 31 | import java.time.DateTimeException; |
| 32 | import java.time.OffsetDateTime; |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 33 | import java.time.format.DateTimeFormatter; |
| 34 | |
| 35 | /** |
| 36 | * Parser for Netconf notifications. |
| 37 | */ |
| 38 | public 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 Papp | f1a7b7f | 2018-09-19 07:38:00 +0000 | [diff] [blame] | 81 | 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 Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 92 | public static long getEventTime(Document doc) |
| 93 | throws UnsupportedOperationException, IllegalArgumentException { |
| 94 | String dateTime = getEventTimeNode(doc).getTextContent(); |
Laszlo Papp | f1a7b7f | 2018-09-19 07:38:00 +0000 | [diff] [blame] | 95 | return getEventTime(dateTime); |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 96 | } |
| 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 | } |