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