helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 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.provider.netconf.alarm; |
| 18 | |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 21 | import org.osgi.service.component.annotations.Activate; |
| 22 | import org.osgi.service.component.annotations.Component; |
| 23 | import org.osgi.service.component.annotations.Deactivate; |
| 24 | import org.osgi.service.component.annotations.Reference; |
| 25 | import org.osgi.service.component.annotations.ReferenceCardinality; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 26 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 27 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProvider; |
| 28 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderService; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderRegistry; |
helenyrwu | 82b50d4 | 2016-07-15 11:35:32 -0700 | [diff] [blame] | 30 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmTranslator; |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 31 | import org.onosproject.incubator.net.faultmanagement.alarm.DeviceAlarmConfig; |
| 32 | import org.onosproject.net.Device; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 33 | import org.onosproject.net.DeviceId; |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 34 | import org.onosproject.net.device.DeviceService; |
| 35 | import org.onosproject.net.driver.DefaultDriverData; |
| 36 | import org.onosproject.net.driver.DefaultDriverHandler; |
| 37 | import org.onosproject.net.driver.Driver; |
| 38 | import org.onosproject.net.driver.DriverData; |
| 39 | import org.onosproject.net.driver.DriverService; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 40 | import org.onosproject.net.provider.AbstractProvider; |
| 41 | import org.onosproject.net.provider.ProviderId; |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 42 | import org.onosproject.netconf.FilteringNetconfDeviceOutputEventListener; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 43 | import org.onosproject.netconf.NetconfController; |
| 44 | import org.onosproject.netconf.NetconfDevice; |
| 45 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 46 | import org.onosproject.netconf.NetconfDeviceListener; |
| 47 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 48 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 49 | import org.onosproject.netconf.NetconfSession; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 50 | import org.slf4j.Logger; |
| 51 | |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 52 | import java.io.ByteArrayInputStream; |
| 53 | import java.io.InputStream; |
| 54 | import java.nio.charset.StandardCharsets; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 55 | import java.util.Collection; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 56 | import java.util.Map; |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 57 | import java.util.Set; |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 58 | |
| 59 | import static org.slf4j.LoggerFactory.getLogger; |
| 60 | |
| 61 | /** |
| 62 | * Provider which uses an Alarm Manager to keep track of device notifications. |
| 63 | */ |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 64 | @Component(immediate = true, service = AlarmProvider.class) |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 65 | public class NetconfAlarmProvider extends AbstractProvider implements AlarmProvider { |
| 66 | |
| 67 | public static final String ACTIVE = "active"; |
| 68 | private final Logger log = getLogger(getClass()); |
helenyrwu | 82b50d4 | 2016-07-15 11:35:32 -0700 | [diff] [blame] | 69 | private final AlarmTranslator translator = new NetconfAlarmTranslator(); |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 70 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 72 | protected AlarmProviderRegistry providerRegistry; |
| 73 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 75 | protected NetconfController controller; |
| 76 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 78 | protected DriverService driverService; |
| 79 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame^] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 81 | protected DeviceService deviceService; |
| 82 | |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 83 | protected AlarmProviderService providerService; |
| 84 | |
| 85 | private Map<DeviceId, InternalNotificationListener> idNotificationListenerMap = Maps.newHashMap(); |
| 86 | |
| 87 | public NetconfAlarmProvider() { |
| 88 | super(new ProviderId("netconf", "org.onosproject.netconf")); |
| 89 | } |
| 90 | |
| 91 | private NetconfDeviceListener deviceListener = new InnerDeviceListener(); |
| 92 | |
| 93 | @Activate |
| 94 | public void activate() { |
| 95 | providerService = providerRegistry.register(this); |
| 96 | controller.getNetconfDevices().forEach(id -> { |
| 97 | NetconfDevice device = controller.getNetconfDevice(id); |
| 98 | NetconfSession session = device.getSession(); |
| 99 | InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo()); |
| 100 | session.addDeviceOutputListener(listener); |
| 101 | idNotificationListenerMap.put(id, listener); |
| 102 | }); |
| 103 | controller.addDeviceListener(deviceListener); |
| 104 | log.info("NetconfAlarmProvider Started"); |
| 105 | } |
| 106 | |
| 107 | @Deactivate |
| 108 | public void deactivate() { |
| 109 | providerRegistry.unregister(this); |
| 110 | idNotificationListenerMap.forEach((id, listener) -> { |
| 111 | controller.getNetconfDevice(id) |
helenyrwu | 4570084 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 112 | .getSession() |
| 113 | .removeDeviceOutputListener(listener); |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 114 | }); |
| 115 | controller.removeDeviceListener(deviceListener); |
| 116 | providerService = null; |
| 117 | log.info("NetconfAlarmProvider Stopped"); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void triggerProbe(DeviceId deviceId) { |
Yuta HIGUCHI | 0454d70 | 2017-03-17 10:08:38 -0700 | [diff] [blame] | 122 | log.debug("Alarm probe triggered with {}", deviceId); |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private void triggerProbe(DeviceId deviceId, Collection<Alarm> alarms) { |
| 126 | providerService.updateAlarmList(deviceId, alarms); |
| 127 | triggerProbe(deviceId); |
| 128 | } |
| 129 | |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 130 | private class InternalNotificationListener |
| 131 | extends FilteringNetconfDeviceOutputEventListener |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 132 | implements NetconfDeviceOutputEventListener { |
| 133 | |
| 134 | InternalNotificationListener(NetconfDeviceInfo deviceInfo) { |
| 135 | super(deviceInfo); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void event(NetconfDeviceOutputEvent event) { |
| 140 | if (event.type() == NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION) { |
| 141 | DeviceId deviceId = event.getDeviceInfo().getDeviceId(); |
Laszlo Papp | 35cc7c4 | 2018-09-17 07:41:09 +0100 | [diff] [blame] | 142 | Driver deviceDriver = driverService.getDriver(deviceId); |
| 143 | Device device = deviceService.getDevice(deviceId); |
| 144 | if (deviceDriver != null && device.is(DeviceAlarmConfig.class)) { |
| 145 | DeviceAlarmConfig alarmTranslator = device.as(DeviceAlarmConfig.class); |
| 146 | DriverData driverData = new DefaultDriverData(deviceDriver, deviceId); |
| 147 | alarmTranslator.setHandler(new DefaultDriverHandler(driverData)); |
| 148 | Set<Alarm> alarms = alarmTranslator.translateAlarms(ImmutableList.of(event)); |
| 149 | triggerProbe(deviceId, alarms); |
| 150 | } else { |
| 151 | String message = event.getMessagePayload(); |
| 152 | InputStream in = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8)); |
| 153 | Collection<Alarm> newAlarms = translator.translateToAlarm(deviceId, in); |
| 154 | triggerProbe(deviceId, newAlarms); |
| 155 | } |
helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | private class InnerDeviceListener implements NetconfDeviceListener { |
| 161 | |
| 162 | @Override |
| 163 | public void deviceAdded(DeviceId deviceId) { |
| 164 | NetconfDevice device = controller.getNetconfDevice(deviceId); |
| 165 | NetconfSession session = device.getSession(); |
| 166 | InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo()); |
| 167 | session.addDeviceOutputListener(listener); |
| 168 | idNotificationListenerMap.put(deviceId, listener); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void deviceRemoved(DeviceId deviceId) { |
| 173 | idNotificationListenerMap.remove(deviceId); |
| 174 | } |
| 175 | } |
| 176 | } |