blob: 769d8b4c1357e4a23202a4bd8be63a39d92f53da [file] [log] [blame]
Shravan Ambatibb6b4452016-05-04 13:25:28 -07001/**
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 */
15package org.onosproject.kafkaintegration.listener;
16
17import static org.onosproject.kafkaintegration.api.dto.OnosEvent.Type.LINK;
18
19import org.onosproject.event.ListenerService;
20import org.onosproject.kafkaintegration.impl.Dispatcher;
21import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
22import org.onosproject.kafkaintegration.converter.ConversionFactory;
23import org.onosproject.kafkaintegration.converter.EventConverter;
24import org.onosproject.net.link.LinkEvent;
25import org.onosproject.net.link.LinkListener;
26import org.onosproject.net.link.LinkService;
27
28import com.google.protobuf.GeneratedMessage;
29
30/**
31 * Listens for ONOS Link Events.
32 *
33 */
34final class LinkEventsListener implements OnosEventListener {
35
36 private boolean listenerRunning = false;
37
38 private InnerListener listener = null;
39
40 // Exists to defeat instantiation
41 private LinkEventsListener() {
42 }
43
44 private static class SingletonHolder {
45 private static final LinkEventsListener INSTANCE =
46 new LinkEventsListener();
47 }
48
49 /**
50 * Returns a static reference to the Listener Factory.
51 *
52 * @return singleton object
53 */
54 public static LinkEventsListener 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 LinkService linkService = (LinkService) service;
63 linkService.addListener(listener);
64 listenerRunning = true;
65 }
66 }
67
68 private class InnerListener implements LinkListener {
69
70 @Override
71 public void event(LinkEvent arg0) {
72
73 // Convert the event to GPB format
74 ConversionFactory conversionFactory =
75 ConversionFactory.getInstance();
76 EventConverter converter = conversionFactory.getConverter(LINK);
77 GeneratedMessage message = converter.convertToProtoMessage(arg0);
78
79 // Call Dispatcher and publish event
80 Dispatcher.getInstance().publish(LINK, message);
81 }
82 }
83
84 @Override
85 public void stopListener(Type e, ListenerService<?, ?> service) {
86 if (listenerRunning) {
87 LinkService linkService = (LinkService) service;
88 linkService.removeListener(listener);
89 listenerRunning = false;
90 }
91 }
92}