blob: 4651cd4ecaddbab2c5d4e6de6b7ca80ec7f3c054 [file] [log] [blame]
jaegonkim6a7b5242018-09-12 23:09:42 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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 */
16package org.onosproject.workflow.impl;
17
18import com.google.common.collect.ArrayListMultimap;
19import com.google.common.collect.ListMultimap;
20import org.onlab.util.AbstractAccumulator;
21import org.onosproject.workflow.api.HandlerTask;
22import org.onosproject.workflow.api.HandlerTaskBatchDelegate;
23
24import java.util.Collection;
25import java.util.List;
26import java.util.Timer;
27
28/**
29 * An accumulator for building batches of event task operations. Only one batch should
30 * be in process per instance at a time.
31 */
32public class HandlerTaskAccumulator extends AbstractAccumulator<HandlerTask> {
33
34 private static final int DEFAULT_MAX_EVENTS = 5000;
35 private static final int DEFAULT_MAX_IDLE_MS = 10;
36 private static final int DEFAULT_MAX_BATCH_MS = 50;
37
38 private static final Timer TIMER = new Timer("onos-workflow-handlertask-batching");
39
40 private final HandlerTaskBatchDelegate delegate;
41
42 private volatile boolean ready;
43
44 /**
45 * Creates an event-task operation accumulator.
46 *
47 * @param delegate the event-task batch delegate
48 */
49 protected HandlerTaskAccumulator(HandlerTaskBatchDelegate delegate) {
50 super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
51 this.delegate = delegate;
52 // Assume that the delegate is ready for workletType at the start
53 ready = true; //TODO validate the assumption that delegate is ready
54 }
55
56 @Override
57 public void processItems(List<HandlerTask> items) {
58 ready = false;
59 delegate.execute(epoch(items));
60 }
61
62 /**
63 * Gets epoch.
64 * @param ops handler tasks
65 * @return collection of collection of handler task.
66 */
67 private Collection<Collection<HandlerTask>> epoch(List<HandlerTask> ops) {
68
69 ListMultimap<String, HandlerTask> tasks = ArrayListMultimap.create();
70
71 // align event-tasks with context
72 for (HandlerTask op : ops) {
73 tasks.put(op.context().name(), op);
74 }
75
76 return tasks.asMap().values();
77 }
78
79 @Override
80 public boolean isReady() {
81 return ready;
82 }
83
84 /**
85 * Making accumulator to be ready.
86 */
87 public void ready() {
88 ready = true;
89 }
90}