blob: c449cab564548f5ea47891ff3b3baf46af0aae1b [file] [log] [blame]
Laszlo Papp35cc7c42018-09-17 07:41:09 +01001/*
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
17package org.onosproject.drivers.polatis.netconf;
18
19import org.onlab.packet.IpAddress;
20
21import org.onosproject.incubator.net.faultmanagement.alarm.Alarm;
22import org.onosproject.incubator.net.faultmanagement.alarm.AlarmId;
23import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm;
24import org.onosproject.incubator.net.faultmanagement.alarm.DeviceAlarmConfig;
25import org.onosproject.incubator.net.faultmanagement.alarm.XmlEventParser;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.netconf.NetconfDeviceOutputEvent;
29
30import org.slf4j.Logger;
31
32import org.xml.sax.SAXException;
33import org.w3c.dom.Document;
34import org.w3c.dom.Node;
35
36import javax.xml.parsers.ParserConfigurationException;
37import java.io.ByteArrayInputStream;
38import java.io.IOException;
39import java.io.InputStream;
40import java.nio.charset.StandardCharsets;
41import java.util.HashSet;
42import java.util.List;
43import java.util.Set;
44
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
48 * Polatis specific implementation to provide asynchronous alarms via NETCONF.
49 */
50public class PolatisAlarmConfig extends AbstractHandlerBehaviour implements DeviceAlarmConfig {
51 private final Logger log = getLogger(getClass());
52
53 private DeviceId deviceId;
Laszlo Papp52a35902018-09-19 10:13:45 +000054 private static final String ALARM_TYPE_LOS = "port-power-alarm";
Laszlo Papp35cc7c42018-09-17 07:41:09 +010055
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 Papp52a35902018-09-19 10:13:45 +000078 if (nodeName.equals(ALARM_TYPE_LOS)) {
Laszlo Papp35cc7c42018-09-17 07:41:09 +010079 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 Papp52a35902018-09-19 10:13:45 +000083 description), deviceId, description,
Laszlo Papp35cc7c42018-09-17 07:41:09 +010084 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}