Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Open Networking Foundation |
| 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.drivers.polatis.netconf; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | |
| 21 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 22 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId; |
| 23 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 24 | import org.onosproject.incubator.net.faultmanagement.alarm.DeviceAlarmConfig; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.XmlEventParser; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 28 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 29 | |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import org.xml.sax.SAXException; |
| 33 | import org.w3c.dom.Document; |
| 34 | import org.w3c.dom.Node; |
| 35 | |
| 36 | import javax.xml.parsers.ParserConfigurationException; |
| 37 | import java.io.ByteArrayInputStream; |
| 38 | import java.io.IOException; |
| 39 | import java.io.InputStream; |
| 40 | import java.nio.charset.StandardCharsets; |
| 41 | import java.util.HashSet; |
| 42 | import java.util.List; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | import static org.slf4j.LoggerFactory.getLogger; |
| 46 | |
| 47 | /** |
| 48 | * Polatis specific implementation to provide asynchronous alarms via NETCONF. |
| 49 | */ |
| 50 | public class PolatisAlarmConfig extends AbstractHandlerBehaviour implements DeviceAlarmConfig { |
| 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
| 53 | private DeviceId deviceId; |
Laszlo Papp | 52a3590 | 2018-09-19 10:13:45 +0000 | [diff] [blame] | 54 | private static final String ALARM_TYPE_LOS = "port-power-alarm"; |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public boolean configureDevice(IpAddress address, int port, String protocol) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public <T> Set<Alarm> translateAlarms(List<T> unparsedAlarms) { |
| 63 | deviceId = handler().data().deviceId(); |
| 64 | Set<Alarm> alarms = new HashSet<>(); |
| 65 | for (T alarm : unparsedAlarms) { |
| 66 | if (alarm instanceof NetconfDeviceOutputEvent) { |
| 67 | NetconfDeviceOutputEvent event = (NetconfDeviceOutputEvent) alarm; |
| 68 | if (event.type() == NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION) { |
| 69 | String message = event.getMessagePayload(); |
| 70 | InputStream in = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)); |
| 71 | try { |
| 72 | Document doc = XmlEventParser.createDocFromMessage(in); |
| 73 | long timeStamp = XmlEventParser.getEventTime(doc); |
| 74 | Node descriptionNode = XmlEventParser.getDescriptionNode(doc); |
| 75 | while (descriptionNode != null) { |
| 76 | if (descriptionNode.getNodeType() == Node.ELEMENT_NODE) { |
| 77 | String nodeName = descriptionNode.getNodeName(); |
Laszlo Papp | 52a3590 | 2018-09-19 10:13:45 +0000 | [diff] [blame] | 78 | if (nodeName.equals(ALARM_TYPE_LOS)) { |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 79 | Node portIdNode = descriptionNode.getChildNodes().item(1); |
| 80 | String portId = portIdNode.getTextContent(); |
| 81 | String description = "Loss of Service alarm raised for fibre " + portId; |
| 82 | alarms.add(new DefaultAlarm.Builder(AlarmId.alarmId(deviceId, |
Laszlo Papp | 52a3590 | 2018-09-19 10:13:45 +0000 | [diff] [blame] | 83 | description), deviceId, description, |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 84 | Alarm.SeverityLevel.MAJOR, timeStamp).build()); |
| 85 | descriptionNode = null; |
| 86 | } |
| 87 | } else { |
| 88 | descriptionNode = descriptionNode.getNextSibling(); |
| 89 | } |
| 90 | } |
| 91 | } catch (SAXException | IOException | ParserConfigurationException | |
| 92 | UnsupportedOperationException | IllegalArgumentException e) { |
| 93 | log.error("Exception thrown translating message from {}.", deviceId, e); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return alarms; |
| 99 | } |
| 100 | } |