blob: 6ed2d7a803f9c0782f1bb945a2c4115a5f2f81ef [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;
31import java.time.Instant;
32import java.time.format.DateTimeFormatter;
33
34/**
35 * Parser for Netconf notifications.
36 */
37public final class XmlEventParser {
38 public static final Logger log = LoggerFactory
39 .getLogger(XmlEventParser.class);
40
41 private static final String DISALLOW_DTD_FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
42 private static final String DISALLOW_EXTERNAL_DTD =
43 "http://apache.org/xml/features/nonvalidating/load-external-dtd";
44 private static final String EVENTTIME_TAGNAME = "eventTime";
45
46 private XmlEventParser() {
47 }
48
49 /**
50 * Creates a document from the input stream message and returns the result.
51 *
52 * @param message input stream message
53 * @return the document result
54 * @throws SAXException Throws SAX Exception
55 * @throws IOException Throws IO Exception
56 * @throws ParserConfigurationException Throws ParserConfigurationException
57 */
58 public static Document createDocFromMessage(InputStream message)
59 throws SAXException, IOException, ParserConfigurationException {
60 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
61 //Disabling DTDs in order to avoid XXE xml-based attacks.
62 disableFeature(dbfactory, DISALLOW_DTD_FEATURE);
63 disableFeature(dbfactory, DISALLOW_EXTERNAL_DTD);
64 dbfactory.setXIncludeAware(false);
65 dbfactory.setExpandEntityReferences(false);
66 DocumentBuilder builder = dbfactory.newDocumentBuilder();
67 return builder.parse(new InputSource(message));
68 }
69
70 private static void disableFeature(DocumentBuilderFactory dbfactory, String feature) {
71 try {
72 dbfactory.setFeature(feature, true);
73 } catch (ParserConfigurationException e) {
74 // This should catch a failed setFeature feature
75 log.info("ParserConfigurationException was thrown. The feature '" +
76 feature + "' is probably not supported by your XML processor.");
77 }
78 }
79
80 public static long getEventTime(Document doc)
81 throws UnsupportedOperationException, IllegalArgumentException {
82 String dateTime = getEventTimeNode(doc).getTextContent();
83 return DateTimeFormatter.ISO_DATE_TIME.parse(dateTime, Instant::from).getEpochSecond();
84 }
85
86 public static Node getDescriptionNode(Document doc) {
87 return getEventTimeNode(doc).getNextSibling();
88 }
89
90 private static Node getEventTimeNode(Document doc) {
91 return doc.getElementsByTagName(EVENTTIME_TAGNAME).item(0);
92 }
93}