Shravan Ambati | bb6b445 | 2016-05-04 13:25:28 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | package org.onosproject.kafkaintegration.listener; |
| 16 | |
| 17 | import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.DEVICE; |
| 18 | |
| 19 | import org.onosproject.event.ListenerService; |
| 20 | import org.onosproject.kafkaintegration.impl.Dispatcher; |
| 21 | import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type; |
| 22 | import org.onosproject.kafkaintegration.converter.ConversionFactory; |
| 23 | import org.onosproject.kafkaintegration.converter.EventConverter; |
| 24 | import org.onosproject.net.device.DeviceEvent; |
| 25 | import org.onosproject.net.device.DeviceListener; |
| 26 | import org.onosproject.net.device.DeviceService; |
| 27 | |
| 28 | import com.google.protobuf.GeneratedMessage; |
| 29 | |
| 30 | /** |
| 31 | * Listens for ONOS Device events. |
| 32 | * |
| 33 | */ |
| 34 | final class DeviceEventsListener implements OnosEventListener { |
| 35 | |
| 36 | private boolean listenerRunning = false; |
| 37 | |
| 38 | private InnerListener listener = null; |
| 39 | |
| 40 | // Exists to defeat instantiation |
| 41 | private DeviceEventsListener() { |
| 42 | } |
| 43 | |
| 44 | private static class SingletonHolder { |
| 45 | private static final DeviceEventsListener INSTANCE = |
| 46 | new DeviceEventsListener(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns a static reference to the Listener Factory. |
| 51 | * |
| 52 | * @return singleton object |
| 53 | */ |
| 54 | public static DeviceEventsListener getInstance() { |
| 55 | return SingletonHolder.INSTANCE; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void startListener(Type e, ListenerService<?, ?> service) { |
| 60 | if (!listenerRunning) { |
| 61 | listener = new InnerListener(); |
| 62 | DeviceService deviceService = (DeviceService) service; |
| 63 | deviceService.addListener(listener); |
| 64 | listenerRunning = true; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private class InnerListener implements DeviceListener { |
| 69 | |
| 70 | @Override |
| 71 | public void event(DeviceEvent arg0) { |
| 72 | |
| 73 | // Convert the event to GPB format |
| 74 | ConversionFactory conversionFactory = |
| 75 | ConversionFactory.getInstance(); |
| 76 | EventConverter converter = conversionFactory.getConverter(DEVICE); |
| 77 | GeneratedMessage message = converter.convertToProtoMessage(arg0); |
| 78 | |
| 79 | // Call Dispatcher and publish event |
| 80 | Dispatcher.getInstance().publish(DEVICE, message); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void stopListener(Type e, ListenerService<?, ?> service) { |
| 86 | if (listenerRunning) { |
| 87 | DeviceService deviceService = (DeviceService) service; |
| 88 | deviceService.removeListener(listener); |
| 89 | listener = null; |
| 90 | listenerRunning = false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | } |