helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.provider.netconf.alarm; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 20 | import org.onosproject.alarm.Alarm; |
| 21 | import org.onosproject.alarm.AlarmId; |
| 22 | import org.onosproject.alarm.AlarmTranslator; |
| 23 | import org.onosproject.alarm.DefaultAlarm; |
| 24 | import org.onosproject.alarm.XmlEventParser; |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.w3c.dom.Document; |
| 28 | import org.w3c.dom.Node; |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 29 | |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 30 | import javax.xml.parsers.ParserConfigurationException; |
| 31 | import javax.xml.transform.OutputKeys; |
| 32 | import javax.xml.transform.Transformer; |
| 33 | import javax.xml.transform.TransformerException; |
| 34 | import javax.xml.transform.TransformerFactory; |
| 35 | import javax.xml.transform.dom.DOMSource; |
| 36 | import javax.xml.transform.stream.StreamResult; |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
| 39 | import java.io.StringWriter; |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.Collection; |
| 42 | |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 43 | import org.xml.sax.SAXException; |
| 44 | |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Translates NETCONF notification messages to actions on alarms. |
| 49 | */ |
| 50 | public 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 Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 59 | Document doc = XmlEventParser.createDocFromMessage(message); |
| 60 | long timeStamp = XmlEventParser.getEventTime(doc); |
| 61 | Node descriptionNode = XmlEventParser.getDescriptionNode(doc); |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 62 | while (descriptionNode != null) { |
| 63 | if (descriptionNode.getNodeType() == Node.ELEMENT_NODE) { |
| 64 | String description = nodeToString(descriptionNode); |
Ray Milkey | 0a1a091 | 2018-02-15 11:35:58 -0800 | [diff] [blame] | 65 | alarms.add(new DefaultAlarm.Builder(AlarmId.alarmId(deviceId, Long.toString(timeStamp)), |
| 66 | deviceId, description, |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 67 | 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 HIGUCHI | 100b552 | 2017-05-03 14:26:11 -0700 | [diff] [blame] | 78 | log.error("Exception thrown translating message from {}.", deviceId, e); |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 79 | return ImmutableSet.of(); |
| 80 | } |
| 81 | } |
| 82 | |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 83 | 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 Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 92 | } |