blob: 138db2b507c008708c63840a8d8d8878ba7c9f76 [file] [log] [blame]
Ray Milkey2d572dd2017-04-14 10:01:24 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey2d572dd2017-04-14 10:01:24 -07003 *
Shravan Ambati1e8471e2016-07-13 14:03:17 -07004 * 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 Milkey2d572dd2017-04-14 10:01:24 -07007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Shravan Ambati1e8471e2016-07-13 14:03:17 -070010 * 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 */
16package org.onosproject.kafkaintegration.impl;
17
Shravan Ambati1e8471e2016-07-13 14:03:17 -070018import org.onosproject.kafkaintegration.api.KafkaEventStorageService;
19import org.onosproject.kafkaintegration.api.dto.OnosEvent;
Shravan Ambatia4875d82017-01-09 13:06:51 -080020import org.onosproject.store.serializers.KryoNamespaces;
21import org.onosproject.store.service.Serializer;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070022import org.onosproject.store.service.StorageService;
Shravan Ambatia4875d82017-01-09 13:06:51 -080023import org.onosproject.store.service.Task;
24import org.onosproject.store.service.WorkQueue;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070025import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Component;
27import org.osgi.service.component.annotations.Deactivate;
28import org.osgi.service.component.annotations.Reference;
29import org.osgi.service.component.annotations.ReferenceCardinality;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070030import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
Shravan Ambatia4875d82017-01-09 13:06:51 -080033import java.util.concurrent.CompletableFuture;
34import java.util.concurrent.ExecutionException;
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070035
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Component(service = KafkaEventStorageService.class)
Shravan Ambati1e8471e2016-07-13 14:03:17 -070037public class KafkaStorageManager implements KafkaEventStorageService {
38
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Shravan Ambati1e8471e2016-07-13 14:03:17 -070040 protected StorageService storageService;
41
Shravan Ambati1e8471e2016-07-13 14:03:17 -070042 private final Logger log = LoggerFactory.getLogger(getClass());
43
Shravan Ambatia4875d82017-01-09 13:06:51 -080044 private static final String KAFKA_WORK_QUEUE = "Kafka-Work-Queue";
Shravan Ambati1e8471e2016-07-13 14:03:17 -070045
Shravan Ambatia4875d82017-01-09 13:06:51 -080046 private WorkQueue<OnosEvent> queue;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070047
48 @Activate
49 protected void activate() {
Shravan Ambatia4875d82017-01-09 13:06:51 -080050 queue = storageService.<OnosEvent>getWorkQueue(KAFKA_WORK_QUEUE,
51 Serializer.using(KryoNamespaces.API,
52 OnosEvent.class,
53 OnosEvent.Type.class));
Shravan Ambati1e8471e2016-07-13 14:03:17 -070054
55 log.info("Started");
56 }
57
Shravan Ambati1e8471e2016-07-13 14:03:17 -070058 @Deactivate
59 protected void deactivate() {
Shravan Ambatia4875d82017-01-09 13:06:51 -080060 queue = null;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070061 log.info("Stopped");
62 }
63
Shravan Ambatia4875d82017-01-09 13:06:51 -080064 @Override
65 public void publishEvent(OnosEvent e) {
66 queue.addOne(e);
67 log.debug("Published {} Event to Distributed Work Queue", e.type());
Shravan Ambati1e8471e2016-07-13 14:03:17 -070068 }
69
70 @Override
Shravan Ambatia4875d82017-01-09 13:06:51 -080071 public OnosEvent consumeEvent() {
72 Task<OnosEvent> task = null;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070073
Shravan Ambatia4875d82017-01-09 13:06:51 -080074 CompletableFuture<Task<OnosEvent>> future = queue.take();
75 try {
76 task = future.get();
77 } catch (InterruptedException e) {
Ray Milkeyba547f92018-02-01 15:22:31 -080078 Thread.currentThread().interrupt();
Shravan Ambatia4875d82017-01-09 13:06:51 -080079 } catch (ExecutionException e) {
Ray Milkeyba547f92018-02-01 15:22:31 -080080 log.warn("consumeEvent()", e);
Shravan Ambati1e8471e2016-07-13 14:03:17 -070081 }
Shravan Ambatia4875d82017-01-09 13:06:51 -080082
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 Ambati1e8471e2016-07-13 14:03:17 -070091 }
92
93}