Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -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 | */ |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 16 | package org.onosproject.kafkaintegration.kafka; |
| 17 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 23 | import org.apache.kafka.clients.producer.ProducerRecord; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.LeadershipService; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.kafkaintegration.api.KafkaConfigService; |
Shravan Ambati | 4c6295e | 2016-12-20 14:53:06 -0800 | [diff] [blame] | 28 | import org.onosproject.kafkaintegration.api.KafkaPublisherService; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 29 | import org.onosproject.kafkaintegration.api.KafkaEventStorageService; |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 32 | import org.onosproject.kafkaintegration.api.dto.OnosEvent; |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 33 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 34 | import java.util.Objects; |
| 35 | import java.util.concurrent.ExecutionException; |
| 36 | import java.util.concurrent.ScheduledExecutorService; |
| 37 | import java.util.concurrent.Executors; |
| 38 | import java.util.concurrent.TimeUnit; |
| 39 | |
| 40 | @Component(immediate = true) |
| 41 | public class EventPublisher { |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 42 | |
| 43 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 44 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected LeadershipService leadershipService; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected ClusterService clusterService; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected KafkaConfigService kafkaConfigService; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 55 | protected KafkaEventStorageService kafkaStore; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected KafkaPublisherService kafkaPublisher; |
| 59 | |
| 60 | protected ScheduledExecutorService exService; |
| 61 | |
| 62 | private static final String SUBSCRIBER_TOPIC = "WORK_QUEUE_SUBSCRIBER"; |
| 63 | |
| 64 | private NodeId localNodeId; |
| 65 | |
| 66 | // Thread Scheduler Parameters |
| 67 | private final long delay = 0; |
| 68 | private final long period = 1; |
| 69 | |
| 70 | private EventCollector eventCollector; |
| 71 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 72 | @Activate |
| 73 | protected void activate() { |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 74 | |
| 75 | leadershipService.runForLeadership(SUBSCRIBER_TOPIC); |
| 76 | |
| 77 | localNodeId = clusterService.getLocalNode().id(); |
| 78 | |
| 79 | startCollector(); |
| 80 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 81 | log.info("Started"); |
| 82 | } |
| 83 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 84 | private void startCollector() { |
| 85 | exService = Executors.newSingleThreadScheduledExecutor(); |
| 86 | eventCollector = new EventCollector(); |
| 87 | exService.scheduleAtFixedRate(eventCollector, delay, period, TimeUnit.SECONDS); |
| 88 | } |
| 89 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 90 | @Deactivate |
| 91 | protected void deactivate() { |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 92 | stopCollector(); |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 93 | log.info("Stopped"); |
| 94 | } |
| 95 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 96 | private void stopCollector() { |
| 97 | exService.shutdown(); |
| 98 | } |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 99 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 100 | private class EventCollector implements Runnable { |
| 101 | |
| 102 | @Override |
| 103 | public void run() { |
| 104 | |
| 105 | // do not allow to proceed without leadership |
| 106 | NodeId leaderNodeId = leadershipService.getLeader(SUBSCRIBER_TOPIC); |
| 107 | if (!Objects.equals(localNodeId, leaderNodeId)) { |
| 108 | log.debug("Not a Leader so cannot consume event"); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | try { |
| 113 | OnosEvent onosEvent = kafkaStore.consumeEvent(); |
| 114 | |
| 115 | if (onosEvent != null) { |
| 116 | kafkaPublisher.send(new ProducerRecord<>(onosEvent.type().toString(), |
| 117 | onosEvent.subject())).get(); |
| 118 | |
| 119 | log.info("Event Type - {}, Subject {} sent successfully.", |
| 120 | onosEvent.type(), onosEvent.subject()); |
| 121 | } |
| 122 | } catch (InterruptedException e1) { |
| 123 | log.error("Thread interupted"); |
| 124 | Thread.currentThread().interrupt(); |
| 125 | } catch (ExecutionException e2) { |
| 126 | log.error("Cannot publish data to Kafka - {}", e2); |
| 127 | } |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 128 | } |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 131 | } |