blob: 66bfc18dae0601b08a121519cbcad5dfe5506f8a [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.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
Shravan Ambatia4875d82017-01-09 13:06:51 -080020import org.apache.felix.scr.annotations.Service;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070021import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.kafkaintegration.api.KafkaEventStorageService;
25import org.onosproject.kafkaintegration.api.dto.OnosEvent;
Shravan Ambatia4875d82017-01-09 13:06:51 -080026import org.onosproject.store.serializers.KryoNamespaces;
27import org.onosproject.store.service.Serializer;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070028import org.onosproject.store.service.StorageService;
Shravan Ambatia4875d82017-01-09 13:06:51 -080029import org.onosproject.store.service.Task;
30import org.onosproject.store.service.WorkQueue;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070031import org.slf4j.Logger;
32import org.slf4j.LoggerFactory;
33
Shravan Ambatia4875d82017-01-09 13:06:51 -080034import java.util.concurrent.CompletableFuture;
35import java.util.concurrent.ExecutionException;
Sanjana Agarwalcb4a3db2016-07-14 11:42:48 -070036
Shravan Ambati5a11e172016-07-21 15:55:28 -070037@Component(immediate = false)
Shravan Ambatia4875d82017-01-09 13:06:51 -080038@Service
Shravan Ambati1e8471e2016-07-13 14:03:17 -070039public class KafkaStorageManager implements KafkaEventStorageService {
40
41 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 protected StorageService storageService;
43
Shravan Ambati1e8471e2016-07-13 14:03:17 -070044 private final Logger log = LoggerFactory.getLogger(getClass());
45
Shravan Ambatia4875d82017-01-09 13:06:51 -080046 private static final String KAFKA_WORK_QUEUE = "Kafka-Work-Queue";
Shravan Ambati1e8471e2016-07-13 14:03:17 -070047
Shravan Ambatia4875d82017-01-09 13:06:51 -080048 private WorkQueue<OnosEvent> queue;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070049
50 @Activate
51 protected void activate() {
Shravan Ambatia4875d82017-01-09 13:06:51 -080052 queue = storageService.<OnosEvent>getWorkQueue(KAFKA_WORK_QUEUE,
53 Serializer.using(KryoNamespaces.API,
54 OnosEvent.class,
55 OnosEvent.Type.class));
Shravan Ambati1e8471e2016-07-13 14:03:17 -070056
57 log.info("Started");
58 }
59
Shravan Ambati1e8471e2016-07-13 14:03:17 -070060 @Deactivate
61 protected void deactivate() {
Shravan Ambatia4875d82017-01-09 13:06:51 -080062 queue = null;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070063 log.info("Stopped");
64 }
65
Shravan Ambatia4875d82017-01-09 13:06:51 -080066 @Override
67 public void publishEvent(OnosEvent e) {
68 queue.addOne(e);
69 log.debug("Published {} Event to Distributed Work Queue", e.type());
Shravan Ambati1e8471e2016-07-13 14:03:17 -070070 }
71
72 @Override
Shravan Ambatia4875d82017-01-09 13:06:51 -080073 public OnosEvent consumeEvent() {
74 Task<OnosEvent> task = null;
Shravan Ambati1e8471e2016-07-13 14:03:17 -070075
Shravan Ambatia4875d82017-01-09 13:06:51 -080076 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 Ambati1e8471e2016-07-13 14:03:17 -070083 }
Shravan Ambatia4875d82017-01-09 13:06:51 -080084
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 Ambati1e8471e2016-07-13 14:03:17 -070093 }
94
95}