blob: eeb5ccda3c5addb1172b50e90b61ec88ab2606a7 [file] [log] [blame]
helenyrwu45700842016-06-09 12:01:30 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
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;
41import java.util.ArrayList;
42import java.util.Collection;
43
44import org.joda.time.format.ISODateTimeFormat;
45import org.xml.sax.SAXException;
46
47import static org.slf4j.LoggerFactory.getLogger;
48
49/**
50 * Translates NETCONF notification messages to actions on alarms.
51 */
52public class NetconfAlarmTranslator implements AlarmTranslator {
53
54 private final Logger log = getLogger(getClass());
55 private static final String EVENTTIME_TAGNAME = "eventTime";
56
57 @Override
58 public Collection<Alarm> translateToAlarm(DeviceId deviceId, InputStream message) {
59 try {
60 Collection<Alarm> alarms = new ArrayList<>();
61 Document doc = createDocFromMessage(message);
62
63 // parse date element value into long
64 Node eventTime = doc.getElementsByTagName(EVENTTIME_TAGNAME).item(0);
65 String date = eventTime.getTextContent();
66 long timeStamp = parseDate(date);
67
68 // event-specific tag names as alarm descriptions
69 Node descriptionNode = eventTime.getNextSibling();
70 while (descriptionNode != null) {
71 if (descriptionNode.getNodeType() == Node.ELEMENT_NODE) {
72 String description = nodeToString(descriptionNode);
73 alarms.add(new DefaultAlarm.Builder(deviceId, description,
74 Alarm.SeverityLevel.WARNING,
75 timeStamp).build());
76 descriptionNode = null;
77 } else {
78 descriptionNode = descriptionNode.getNextSibling();
79 }
80 }
81 return alarms;
82 } catch (SAXException | IOException | ParserConfigurationException |
83 UnsupportedOperationException | IllegalArgumentException |
84 TransformerException e) {
Yuta HIGUCHI100b5522017-05-03 14:26:11 -070085 log.error("Exception thrown translating message from {}.", deviceId, e);
helenyrwu45700842016-06-09 12:01:30 -070086 return ImmutableSet.of();
87 }
88 }
89
90 private Document createDocFromMessage(InputStream message)
91 throws SAXException, IOException, ParserConfigurationException {
92 DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
93 DocumentBuilder builder = dbfactory.newDocumentBuilder();
94 return builder.parse(new InputSource(message));
95 }
96
97 private long parseDate(String timeStr)
98 throws UnsupportedOperationException, IllegalArgumentException {
Yuta HIGUCHI5aa4cdf2017-05-03 14:48:34 -070099 return ISODateTimeFormat.dateTime().parseMillis(timeStr);
helenyrwu45700842016-06-09 12:01:30 -0700100 }
101
102 private static String nodeToString(Node rootNode) throws TransformerException {
103 TransformerFactory tf = TransformerFactory.newInstance();
104 Transformer transformer = tf.newTransformer();
105 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
106 StringWriter writer = new StringWriter();
107 DOMSource source = new DOMSource(rootNode);
108 transformer.transform(source, new StreamResult(writer));
109 return writer.getBuffer().toString();
110 }
111}