Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +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 com.google.common.collect.ImmutableList; |
| 20 | import org.apache.commons.configuration.HierarchicalConfiguration; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 21 | import org.onosproject.alarm.Alarm; |
| 22 | import org.onosproject.alarm.AlarmConsumer; |
| 23 | import org.onosproject.alarm.AlarmId; |
| 24 | import org.onosproject.alarm.DefaultAlarm; |
| 25 | import org.onosproject.alarm.XmlEventParser; |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 28 | import org.onosproject.net.driver.DriverHandler; |
| 29 | import org.onosproject.netconf.NetconfController; |
| 30 | import org.onosproject.netconf.NetconfException; |
| 31 | import org.onosproject.mastership.MastershipService; |
| 32 | |
| 33 | import org.slf4j.Logger; |
| 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 37 | |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame^] | 38 | import static org.onosproject.alarm.Alarm.SeverityLevel; |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.configsAt; |
| 41 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.xmlEmpty; |
| 42 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_DATA_SYSTEMALARMS; |
| 43 | import static org.onosproject.drivers.polatis.netconf.PolatisNetconfUtility.KEY_SYSTEMALARMS_XMLNS; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * Polatis specific implementation to provide a list of current alarms. |
| 48 | */ |
| 49 | public class PolatisAlarmConsumer extends AbstractHandlerBehaviour implements AlarmConsumer { |
| 50 | private final Logger log = getLogger(getClass()); |
| 51 | |
| 52 | private static final String ALARM_TIME = "alarm-time"; |
| 53 | private static final String ALARM_TYPE = "alarm-type"; |
Laszlo Papp | 23e0c51 | 2018-09-18 16:19:17 +0000 | [diff] [blame] | 54 | private static final String ALARM_TYPE_LOS = "NOTIF_PORT_POWER"; |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 55 | private static final String ALARM_MESSAGE = "alarm-message"; |
| 56 | |
| 57 | private DeviceId deviceId; |
| 58 | |
| 59 | @Override |
| 60 | public List<Alarm> consumeAlarms() { |
| 61 | DriverHandler handler = handler(); |
| 62 | NetconfController controller = handler.get(NetconfController.class); |
| 63 | checkNotNull(controller, "Netconf controller is null"); |
| 64 | |
| 65 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 66 | deviceId = handler.data().deviceId(); |
| 67 | |
| 68 | List<Alarm> alarms = new ArrayList<>(); |
| 69 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 70 | log.warn("Not master for {} Use {} to execute command", |
| 71 | deviceId, |
| 72 | mastershipService.getMasterFor(deviceId)); |
| 73 | return ImmutableList.copyOf(alarms); |
| 74 | } |
| 75 | |
| 76 | try { |
| 77 | String request = xmlEmpty(KEY_SYSTEMALARMS_XMLNS); |
| 78 | String reply = controller.getDevicesMap() |
| 79 | .get(deviceId) |
| 80 | .getSession() |
| 81 | .get(request, null); |
| 82 | if (reply != null) { |
| 83 | alarms = parseAlarms(reply); |
| 84 | } |
| 85 | } catch (NetconfException e) { |
| 86 | log.error("Error reading alarms for device {} exception {}", deviceId, e); |
| 87 | } |
| 88 | |
| 89 | return ImmutableList.copyOf(alarms); |
| 90 | } |
| 91 | |
| 92 | private List<Alarm> parseAlarms(String content) { |
| 93 | List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_SYSTEMALARMS); |
| 94 | List<Alarm> alarms = new ArrayList<>(); |
| 95 | for (HierarchicalConfiguration alarm : subtrees) { |
| 96 | alarms.add(parseAlarm(alarm)); |
| 97 | } |
| 98 | return alarms; |
| 99 | } |
| 100 | |
| 101 | private Alarm parseAlarm(HierarchicalConfiguration cfg) { |
| 102 | boolean cleared = false; |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 103 | String alarmType = cfg.getString(ALARM_TYPE); |
| 104 | String alarmMessage = cfg.getString(ALARM_MESSAGE); |
| 105 | SeverityLevel alarmLevel = SeverityLevel.INDETERMINATE; |
Laszlo Papp | 23e0c51 | 2018-09-18 16:19:17 +0000 | [diff] [blame] | 106 | if (alarmType.equals(ALARM_TYPE_LOS)) { |
| 107 | alarmLevel = SeverityLevel.MAJOR; |
| 108 | } |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 109 | long timeRaised = getTimeRaised(cfg); |
| 110 | DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder( |
Laszlo Papp | 52a3590 | 2018-09-19 10:13:45 +0000 | [diff] [blame] | 111 | AlarmId.alarmId(deviceId, alarmMessage), |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 112 | deviceId, alarmMessage, alarmLevel, timeRaised); |
| 113 | return alarmBuilder.build(); |
| 114 | } |
| 115 | |
| 116 | private long getTimeRaised(HierarchicalConfiguration cfg) { |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 117 | String alarmTime = cfg.getString(ALARM_TIME); |
Laszlo Papp | f1a7b7f | 2018-09-19 07:38:00 +0000 | [diff] [blame] | 118 | return XmlEventParser.getEventTime(alarmTime); |
Laszlo Papp | 4ffd6ae | 2018-03-28 18:09:55 +0100 | [diff] [blame] | 119 | } |
| 120 | } |