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