Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 3 | * |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 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 |
Ray Milkey | 2d572dd | 2017-04-14 10:01:24 -0700 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 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 | package org.onosproject.kafkaintegration.impl; |
| 17 | |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 18 | import org.onosproject.kafkaintegration.api.KafkaEventStorageService; |
| 19 | import org.onosproject.kafkaintegration.api.dto.OnosEvent; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 20 | import org.onosproject.store.serializers.KryoNamespaces; |
| 21 | import org.onosproject.store.service.Serializer; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 22 | import org.onosproject.store.service.StorageService; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 23 | import org.onosproject.store.service.Task; |
| 24 | import org.onosproject.store.service.WorkQueue; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Component; |
| 27 | import org.osgi.service.component.annotations.Deactivate; |
| 28 | import org.osgi.service.component.annotations.Reference; |
| 29 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 33 | import java.util.concurrent.CompletableFuture; |
| 34 | import java.util.concurrent.ExecutionException; |
Sanjana Agarwal | cb4a3db | 2016-07-14 11:42:48 -0700 | [diff] [blame] | 35 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | @Component(service = KafkaEventStorageService.class) |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 37 | public class KafkaStorageManager implements KafkaEventStorageService { |
| 38 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 40 | protected StorageService storageService; |
| 41 | |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 42 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 43 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 44 | private static final String KAFKA_WORK_QUEUE = "Kafka-Work-Queue"; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 45 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 46 | private WorkQueue<OnosEvent> queue; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 47 | |
| 48 | @Activate |
| 49 | protected void activate() { |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 50 | queue = storageService.<OnosEvent>getWorkQueue(KAFKA_WORK_QUEUE, |
| 51 | Serializer.using(KryoNamespaces.API, |
| 52 | OnosEvent.class, |
| 53 | OnosEvent.Type.class)); |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 54 | |
| 55 | log.info("Started"); |
| 56 | } |
| 57 | |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 58 | @Deactivate |
| 59 | protected void deactivate() { |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 60 | queue = null; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 61 | log.info("Stopped"); |
| 62 | } |
| 63 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 64 | @Override |
| 65 | public void publishEvent(OnosEvent e) { |
| 66 | queue.addOne(e); |
| 67 | log.debug("Published {} Event to Distributed Work Queue", e.type()); |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 71 | public OnosEvent consumeEvent() { |
| 72 | Task<OnosEvent> task = null; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 73 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 74 | CompletableFuture<Task<OnosEvent>> future = queue.take(); |
| 75 | try { |
| 76 | task = future.get(); |
| 77 | } catch (InterruptedException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 78 | Thread.currentThread().interrupt(); |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 79 | } catch (ExecutionException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 80 | log.warn("consumeEvent()", e); |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 81 | } |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 82 | |
| 83 | if (task != null) { |
| 84 | queue.complete(task.taskId()); |
| 85 | log.debug("Consumed {} Event from Distributed Work Queue with id {}", |
| 86 | task.payload().type(), task.taskId()); |
| 87 | return task.payload(); |
| 88 | } |
| 89 | |
| 90 | return null; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } |