helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.provider.netconf.alarm; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 21 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmTranslator; |
| 22 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.w3c.dom.Document; |
| 26 | import org.w3c.dom.Node; |
| 27 | import org.xml.sax.InputSource; |
| 28 | |
| 29 | import javax.xml.parsers.DocumentBuilder; |
| 30 | import javax.xml.parsers.DocumentBuilderFactory; |
| 31 | import javax.xml.parsers.ParserConfigurationException; |
| 32 | import javax.xml.transform.OutputKeys; |
| 33 | import javax.xml.transform.Transformer; |
| 34 | import javax.xml.transform.TransformerException; |
| 35 | import javax.xml.transform.TransformerFactory; |
| 36 | import javax.xml.transform.dom.DOMSource; |
| 37 | import javax.xml.transform.stream.StreamResult; |
| 38 | import java.io.IOException; |
| 39 | import java.io.InputStream; |
| 40 | import java.io.StringWriter; |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.Collection; |
| 43 | |
| 44 | import org.joda.time.format.ISODateTimeFormat; |
| 45 | import org.xml.sax.SAXException; |
| 46 | |
| 47 | import static org.slf4j.LoggerFactory.getLogger; |
| 48 | |
| 49 | /** |
| 50 | * Translates NETCONF notification messages to actions on alarms. |
| 51 | */ |
| 52 | public 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 HIGUCHI | 100b552 | 2017-05-03 14:26:11 -0700 | [diff] [blame] | 85 | log.error("Exception thrown translating message from {}.", deviceId, e); |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 86 | 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 HIGUCHI | 5aa4cdf | 2017-05-03 14:48:34 -0700 | [diff] [blame] | 99 | return ISODateTimeFormat.dateTime().parseMillis(timeStr); |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 100 | } |
| 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 | } |