blob: 46367043fb4fa3bf90907ad1ace3e01c30140302 [file] [log] [blame]
helenyrwu45700842016-06-09 12:01:30 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
helenyrwu45700842016-06-09 12:01:30 -07003 *
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 */
16
17package org.onosproject.provider.netconf.alarm;
18
19import com.google.common.collect.ImmutableSet;
20import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
21import org.onosproject.incubator.net.faultmanagement.alarm.AlarmTranslator;
22import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
23import org.onosproject.net.DeviceId;
24import org.slf4j.Logger;
25import org.w3c.dom.Document;
26import org.w3c.dom.Node;
27import org.xml.sax.InputSource;
28
29import javax.xml.parsers.DocumentBuilder;
30import javax.xml.parsers.DocumentBuilderFactory;
31import javax.xml.parsers.ParserConfigurationException;
32import javax.xml.transform.OutputKeys;
33import javax.xml.transform.Transformer;
34import javax.xml.transform.TransformerException;
35import javax.xml.transform.TransformerFactory;
36import javax.xml.transform.dom.DOMSource;
37import javax.xml.transform.stream.StreamResult;
38import java.io.IOException;
39import java.io.InputStream;
40import java.io.StringWriter;
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070041import java.time.Instant;
42import java.time.format.DateTimeFormatter;
helenyrwu45700842016-06-09 12:01:30 -070043import java.util.ArrayList;
44import java.util.Collection;
45
helenyrwu45700842016-06-09 12:01:30 -070046import org.xml.sax.SAXException;
47
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Translates NETCONF notification messages to actions on alarms.
52 */
53public class NetconfAlarmTranslator implements AlarmTranslator {
54
55 private final Logger log = getLogger(getClass());
56 private static final String EVENTTIME_TAGNAME = "eventTime";
57
58 @Override
59 public Collection<Alarm> translateToAlarm(DeviceId deviceId, InputStream message) {
60 try {
61 Collection<Alarm> alarms = new ArrayList<>();
62 Document doc = createDocFromMessage(message);
63
64 // parse date element value into long
65 Node eventTime = doc.getElementsByTagName(EVENTTIME_TAGNAME).item(0);
66 String date = eventTime.getTextContent();
67 long timeStamp = parseDate(date);
68
69 // event-specific tag names as alarm descriptions
70 Node descriptionNode = eventTime.getNextSibling();
71 while (descriptionNode != null) {
72 if (descriptionNode.getNodeType() == Node.ELEMENT_NODE) {
73 String description = nodeToString(descriptionNode);
74 alarms.add(new DefaultAlarm.Builder(deviceId, description,
75 Alarm.SeverityLevel.WARNING,
76 timeStamp).build());
77 descriptionNode = null;
78 } else {
79 descriptionNode = descriptionNode.getNextSibling();
80 }
81 }
82 return alarms;
83 } catch (SAXException | IOException | ParserConfigurationException |
84 UnsupportedOperationException | IllegalArgumentException |
85 TransformerException e) {
Yuta HIGUCHI100b5522017-05-03 14:26:11 -070086 log.error("Exception thrown translating message from {}.", deviceId, e);
helenyrwu45700842016-06-09 12:01:30 -070087 return ImmutableSet.of();
88 }
89 }
90
91 private Document createDocFromMessage(InputStream message)
92 throws SAXException, IOException, ParserConfigurationException {
93 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
94 DocumentBuilder builder = dbfactory.newDocumentBuilder();
95 return builder.parse(new InputSource(message));
96 }
97
98 private long parseDate(String timeStr)
99 throws UnsupportedOperationException, IllegalArgumentException {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700100 return DateTimeFormatter.ISO_DATE_TIME.parse(timeStr, Instant::from).getEpochSecond();
helenyrwu45700842016-06-09 12:01:30 -0700101 }
102
103 private static String nodeToString(Node rootNode) throws TransformerException {
104 TransformerFactory tf = TransformerFactory.newInstance();
105 Transformer transformer = tf.newTransformer();
106 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
107 StringWriter writer = new StringWriter();
108 DOMSource source = new DOMSource(rootNode);
109 transformer.transform(source, new StreamResult(writer));
110 return writer.getBuffer().toString();
111 }
112}