helenyrwu | 0407c64 | 2016-06-09 12:01:30 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.incubator.net.faultmanagement.alarm.Alarm; |
| 26 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProvider; |
| 27 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderService; |
| 28 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmService; |
| 29 | import org.onosproject.incubator.net.faultmanagement.alarm.AlarmProviderRegistry; |
| 30 | import org.onosproject.incubator.net.faultmanagement.alarm.DefaultAlarm; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.provider.AbstractProvider; |
| 33 | import org.onosproject.net.provider.ProviderId; |
| 34 | import org.onosproject.netconf.NetconfController; |
| 35 | import org.onosproject.netconf.NetconfDevice; |
| 36 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 37 | import org.onosproject.netconf.NetconfDeviceListener; |
| 38 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 39 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 40 | import org.onosproject.netconf.NetconfSession; |
| 41 | import org.onosproject.netconf.ctl.NetconfDeviceOutputEventListenerImpl; |
| 42 | import org.slf4j.Logger; |
| 43 | |
| 44 | import java.util.Collection; |
| 45 | import java.util.Collections; |
| 46 | import java.util.Map; |
| 47 | |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Provider which uses an Alarm Manager to keep track of device notifications. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | public class NetconfAlarmProvider extends AbstractProvider implements AlarmProvider { |
| 55 | |
| 56 | public static final String ACTIVE = "active"; |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected AlarmProviderRegistry providerRegistry; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected NetconfController controller; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected AlarmService alarmService; |
| 67 | |
| 68 | protected AlarmProviderService providerService; |
| 69 | |
| 70 | private Map<DeviceId, InternalNotificationListener> idNotificationListenerMap = Maps.newHashMap(); |
| 71 | |
| 72 | public NetconfAlarmProvider() { |
| 73 | super(new ProviderId("netconf", "org.onosproject.netconf")); |
| 74 | } |
| 75 | |
| 76 | private NetconfDeviceListener deviceListener = new InnerDeviceListener(); |
| 77 | |
| 78 | @Activate |
| 79 | public void activate() { |
| 80 | providerService = providerRegistry.register(this); |
| 81 | controller.getNetconfDevices().forEach(id -> { |
| 82 | NetconfDevice device = controller.getNetconfDevice(id); |
| 83 | NetconfSession session = device.getSession(); |
| 84 | InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo()); |
| 85 | session.addDeviceOutputListener(listener); |
| 86 | idNotificationListenerMap.put(id, listener); |
| 87 | }); |
| 88 | controller.addDeviceListener(deviceListener); |
| 89 | log.info("NetconfAlarmProvider Started"); |
| 90 | } |
| 91 | |
| 92 | @Deactivate |
| 93 | public void deactivate() { |
| 94 | providerRegistry.unregister(this); |
| 95 | idNotificationListenerMap.forEach((id, listener) -> { |
| 96 | controller.getNetconfDevice(id) |
| 97 | .getSession() |
| 98 | .removeDeviceOutputListener(listener); |
| 99 | }); |
| 100 | controller.removeDeviceListener(deviceListener); |
| 101 | providerService = null; |
| 102 | log.info("NetconfAlarmProvider Stopped"); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void triggerProbe(DeviceId deviceId) { |
| 107 | log.debug("Alarm probe triggered with " + deviceId); |
| 108 | } |
| 109 | |
| 110 | private void triggerProbe(DeviceId deviceId, Collection<Alarm> alarms) { |
| 111 | providerService.updateAlarmList(deviceId, alarms); |
| 112 | triggerProbe(deviceId); |
| 113 | } |
| 114 | |
| 115 | private class InternalNotificationListener extends NetconfDeviceOutputEventListenerImpl |
| 116 | implements NetconfDeviceOutputEventListener { |
| 117 | |
| 118 | InternalNotificationListener(NetconfDeviceInfo deviceInfo) { |
| 119 | super(deviceInfo); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void event(NetconfDeviceOutputEvent event) { |
| 124 | if (event.type() == NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION) { |
| 125 | DeviceId deviceId = event.getDeviceInfo().getDeviceId(); |
| 126 | Alarm newAlarm = new DefaultAlarm.Builder(deviceId, event.getMessagePayload(), |
| 127 | Alarm.SeverityLevel.WARNING, 0).build(); |
| 128 | Collection<Alarm> alarms = Collections.singleton(newAlarm); |
| 129 | triggerProbe(deviceId, alarms); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private class InnerDeviceListener implements NetconfDeviceListener { |
| 135 | |
| 136 | @Override |
| 137 | public void deviceAdded(DeviceId deviceId) { |
| 138 | NetconfDevice device = controller.getNetconfDevice(deviceId); |
| 139 | NetconfSession session = device.getSession(); |
| 140 | InternalNotificationListener listener = new InternalNotificationListener(device.getDeviceInfo()); |
| 141 | session.addDeviceOutputListener(listener); |
| 142 | idNotificationListenerMap.put(deviceId, listener); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public void deviceRemoved(DeviceId deviceId) { |
| 147 | idNotificationListenerMap.remove(deviceId); |
| 148 | } |
| 149 | } |
| 150 | } |