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"; |
| 56 | private static final String ALARM_MESSAGE = "alarm-message"; |
| 57 | |
| 58 | private DeviceId deviceId; |
| 59 | |
| 60 | @Override |
| 61 | public List<Alarm> consumeAlarms() { |
| 62 | DriverHandler handler = handler(); |
| 63 | NetconfController controller = handler.get(NetconfController.class); |
| 64 | checkNotNull(controller, "Netconf controller is null"); |
| 65 | |
| 66 | MastershipService mastershipService = handler.get(MastershipService.class); |
| 67 | deviceId = handler.data().deviceId(); |
| 68 | |
| 69 | List<Alarm> alarms = new ArrayList<>(); |
| 70 | if (!mastershipService.isLocalMaster(deviceId)) { |
| 71 | log.warn("Not master for {} Use {} to execute command", |
| 72 | deviceId, |
| 73 | mastershipService.getMasterFor(deviceId)); |
| 74 | return ImmutableList.copyOf(alarms); |
| 75 | } |
| 76 | |
| 77 | try { |
| 78 | String request = xmlEmpty(KEY_SYSTEMALARMS_XMLNS); |
| 79 | String reply = controller.getDevicesMap() |
| 80 | .get(deviceId) |
| 81 | .getSession() |
| 82 | .get(request, null); |
| 83 | if (reply != null) { |
| 84 | alarms = parseAlarms(reply); |
| 85 | } |
| 86 | } catch (NetconfException e) { |
| 87 | log.error("Error reading alarms for device {} exception {}", deviceId, e); |
| 88 | } |
| 89 | |
| 90 | return ImmutableList.copyOf(alarms); |
| 91 | } |
| 92 | |
| 93 | private List<Alarm> parseAlarms(String content) { |
| 94 | List<HierarchicalConfiguration> subtrees = configsAt(content, KEY_DATA_SYSTEMALARMS); |
| 95 | List<Alarm> alarms = new ArrayList<>(); |
| 96 | for (HierarchicalConfiguration alarm : subtrees) { |
| 97 | alarms.add(parseAlarm(alarm)); |
| 98 | } |
| 99 | return alarms; |
| 100 | } |
| 101 | |
| 102 | private Alarm parseAlarm(HierarchicalConfiguration cfg) { |
| 103 | boolean cleared = false; |
| 104 | // TODO: Use the type for severity or in the description? |
| 105 | String alarmType = cfg.getString(ALARM_TYPE); |
| 106 | String alarmMessage = cfg.getString(ALARM_MESSAGE); |
| 107 | SeverityLevel alarmLevel = SeverityLevel.INDETERMINATE; |
| 108 | long timeRaised = getTimeRaised(cfg); |
| 109 | DefaultAlarm.Builder alarmBuilder = new DefaultAlarm.Builder( |
| 110 | AlarmId.alarmId(deviceId, Long.toString(timeRaised)), |
| 111 | deviceId, alarmMessage, alarmLevel, timeRaised); |
| 112 | return alarmBuilder.build(); |
| 113 | } |
| 114 | |
| 115 | private long getTimeRaised(HierarchicalConfiguration cfg) { |
| 116 | long timeRaised; |
| 117 | |
| 118 | String alarmTime = cfg.getString(ALARM_TIME); |
| 119 | try { |
| 120 | OffsetDateTime date = OffsetDateTime.parse(alarmTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME); |
| 121 | timeRaised = date.toInstant().toEpochMilli(); |
| 122 | return timeRaised; |
| 123 | } catch (DateTimeException e) { |
| 124 | log.error("Cannot parse exception {} {}", alarmTime, e); |
| 125 | } |
| 126 | return System.currentTimeMillis(); |
| 127 | } |
| 128 | } |