blob: 05847e1aa287503ee420822893968cb898860efe [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;
Ray Milkey0a1a0912018-02-15 11:35:58 -080021import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
helenyrwu45700842016-06-09 12:01:30 -070022import org.onosproject.incubator.net.faultmanagement.alarm.AlarmTranslator;
23import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
Laszlo Papp35cc7c42018-09-17 07:41:09 +010024import org.onosproject.incubator.net.faultmanagement.alarm.XmlEventParser;
helenyrwu45700842016-06-09 12:01:30 -070025import org.onosproject.net.DeviceId;
26import org.slf4j.Logger;
27import org.w3c.dom.Document;
28import org.w3c.dom.Node;
helenyrwu45700842016-06-09 12:01:30 -070029
helenyrwu45700842016-06-09 12:01:30 -070030import javax.xml.parsers.ParserConfigurationException;
31import javax.xml.transform.OutputKeys;
32import javax.xml.transform.Transformer;
33import javax.xml.transform.TransformerException;
34import javax.xml.transform.TransformerFactory;
35import javax.xml.transform.dom.DOMSource;
36import javax.xml.transform.stream.StreamResult;
37import java.io.IOException;
38import java.io.InputStream;
39import java.io.StringWriter;
40import java.util.ArrayList;
41import java.util.Collection;
42
helenyrwu45700842016-06-09 12:01:30 -070043import org.xml.sax.SAXException;
44
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Translates NETCONF notification messages to actions on alarms.
49 */
50public class NetconfAlarmTranslator implements AlarmTranslator {
51
52 private final Logger log = getLogger(getClass());
53 private static final String EVENTTIME_TAGNAME = "eventTime";
54
55 @Override
56 public Collection<Alarm> translateToAlarm(DeviceId deviceId, InputStream message) {
57 try {
58 Collection<Alarm> alarms = new ArrayList<>();
Laszlo Papp35cc7c42018-09-17 07:41:09 +010059 Document doc = XmlEventParser.createDocFromMessage(message);
60 long timeStamp = XmlEventParser.getEventTime(doc);
61 Node descriptionNode = XmlEventParser.getDescriptionNode(doc);
helenyrwu45700842016-06-09 12:01:30 -070062 while (descriptionNode != null) {
63 if (descriptionNode.getNodeType() == Node.ELEMENT_NODE) {
64 String description = nodeToString(descriptionNode);
Ray Milkey0a1a0912018-02-15 11:35:58 -080065 alarms.add(new DefaultAlarm.Builder(AlarmId.alarmId(deviceId, Long.toString(timeStamp)),
66 deviceId, description,
helenyrwu45700842016-06-09 12:01:30 -070067 Alarm.SeverityLevel.WARNING,
68 timeStamp).build());
69 descriptionNode = null;
70 } else {
71 descriptionNode = descriptionNode.getNextSibling();
72 }
73 }
74 return alarms;
75 } catch (SAXException | IOException | ParserConfigurationException |
76 UnsupportedOperationException | IllegalArgumentException |
77 TransformerException e) {
Yuta HIGUCHI100b5522017-05-03 14:26:11 -070078 log.error("Exception thrown translating message from {}.", deviceId, e);
helenyrwu45700842016-06-09 12:01:30 -070079 return ImmutableSet.of();
80 }
81 }
82
helenyrwu45700842016-06-09 12:01:30 -070083 private static String nodeToString(Node rootNode) throws TransformerException {
84 TransformerFactory tf = TransformerFactory.newInstance();
85 Transformer transformer = tf.newTransformer();
86 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
87 StringWriter writer = new StringWriter();
88 DOMSource source = new DOMSource(rootNode);
89 transformer.transform(source, new StreamResult(writer));
90 return writer.getBuffer().toString();
91 }
Laszlo Papp35cc7c42018-09-17 07:41:09 +010092}