Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 1 | /** |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 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.impl; |
| 16 | |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 17 | import org.apache.felix.scr.annotations.Activate; |
| 18 | import org.apache.felix.scr.annotations.Component; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Service; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.onosproject.kafkaintegration.api.KafkaEventStorageService; |
| 24 | import org.onosproject.kafkaintegration.api.dto.OnosEvent; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 25 | import org.onosproject.store.serializers.KryoNamespaces; |
| 26 | import org.onosproject.store.service.Serializer; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 27 | import org.onosproject.store.service.StorageService; |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 28 | import org.onosproject.store.service.Task; |
| 29 | import org.onosproject.store.service.WorkQueue; |
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 | |
Shravan Ambati | 5a11e17 | 2016-07-21 15:55:28 -0700 | [diff] [blame] | 36 | @Component(immediate = false) |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 37 | @Service |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 38 | public class KafkaStorageManager implements KafkaEventStorageService { |
| 39 | |
| 40 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 41 | protected StorageService storageService; |
| 42 | |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 43 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 44 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 45 | private static final String KAFKA_WORK_QUEUE = "Kafka-Work-Queue"; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 46 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 47 | private WorkQueue<OnosEvent> queue; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 48 | |
| 49 | @Activate |
| 50 | protected void activate() { |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 51 | queue = storageService.<OnosEvent>getWorkQueue(KAFKA_WORK_QUEUE, |
| 52 | Serializer.using(KryoNamespaces.API, |
| 53 | OnosEvent.class, |
| 54 | OnosEvent.Type.class)); |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 55 | |
| 56 | log.info("Started"); |
| 57 | } |
| 58 | |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 59 | @Deactivate |
| 60 | protected void deactivate() { |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 61 | queue = null; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 62 | log.info("Stopped"); |
| 63 | } |
| 64 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 65 | @Override |
| 66 | public void publishEvent(OnosEvent e) { |
| 67 | queue.addOne(e); |
| 68 | log.debug("Published {} Event to Distributed Work Queue", e.type()); |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 72 | public OnosEvent consumeEvent() { |
| 73 | Task<OnosEvent> task = null; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 74 | |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 75 | CompletableFuture<Task<OnosEvent>> future = queue.take(); |
| 76 | try { |
| 77 | task = future.get(); |
| 78 | } catch (InterruptedException e) { |
| 79 | e.printStackTrace(); |
| 80 | } catch (ExecutionException e) { |
| 81 | e.printStackTrace(); |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 82 | } |
Shravan Ambati | a4875d8 | 2017-01-09 13:06:51 -0800 | [diff] [blame] | 83 | |
| 84 | if (task != null) { |
| 85 | queue.complete(task.taskId()); |
| 86 | log.debug("Consumed {} Event from Distributed Work Queue with id {}", |
| 87 | task.payload().type(), task.taskId()); |
| 88 | return task.payload(); |
| 89 | } |
| 90 | |
| 91 | return null; |
Shravan Ambati | 1e8471e | 2016-07-13 14:03:17 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } |