blob: fdd66777948a86cc9f1be3852cceec4d5ed93a52 [file] [log] [blame]
Brian O'Connorcff03322015-02-03 15:28:59 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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.net.intent.impl;
17
18import com.google.common.collect.Maps;
19import org.onlab.util.AbstractAccumulator;
Brian O'Connorb499b352015-02-03 16:46:15 -080020import org.onosproject.net.intent.IntentBatchDelegate;
Brian O'Connorcff03322015-02-03 15:28:59 -080021import org.onosproject.net.intent.IntentData;
Ray Milkey5b3717e2015-02-05 11:44:08 -080022import org.onosproject.net.intent.Key;
Brian O'Connorcff03322015-02-03 15:28:59 -080023
Sho SHIMIZU46f48fb2015-02-03 17:53:27 -080024import java.util.Collection;
Brian O'Connorcff03322015-02-03 15:28:59 -080025import java.util.List;
26import java.util.Map;
27import java.util.Timer;
28
29/**
30 * An accumulator for building batches of intent operations. Only one batch should
31 * be in process per instance at a time.
32 */
33public class IntentAccumulator extends AbstractAccumulator<IntentData> {
34
35 private static final int DEFAULT_MAX_EVENTS = 1000;
36 private static final int DEFAULT_MAX_IDLE_MS = 10;
37 private static final int DEFAULT_MAX_BATCH_MS = 50;
38
39 // FIXME: Replace with a system-wide timer instance;
40 // TODO: Convert to use HashedWheelTimer or produce a variant of that; then decide which we want to adopt
Thomas Vachuskaa132e3a2015-02-21 01:53:14 -080041 private static final Timer TIMER = new Timer("onos-intent-op-batching");
Brian O'Connorcff03322015-02-03 15:28:59 -080042
Brian O'Connorb499b352015-02-03 16:46:15 -080043 private final IntentBatchDelegate delegate;
44
Brian O'Connorcff03322015-02-03 15:28:59 -080045 /**
46 * Creates an intent operation accumulator.
Jonathan Hartd0ba2172015-02-11 13:54:33 -080047 *
48 * @param delegate the intent batch delegate
Brian O'Connorcff03322015-02-03 15:28:59 -080049 */
Brian O'Connorb499b352015-02-03 16:46:15 -080050 protected IntentAccumulator(IntentBatchDelegate delegate) {
Brian O'Connorcff03322015-02-03 15:28:59 -080051 super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
Brian O'Connorb499b352015-02-03 16:46:15 -080052 this.delegate = delegate;
Brian O'Connorcff03322015-02-03 15:28:59 -080053 }
54
55 @Override
Thomas Vachuskaecb63c52015-02-19 10:03:48 -080056 public void processItems(List<IntentData> items) {
57 delegate.execute(reduce(items));
Brian O'Connorcff03322015-02-03 15:28:59 -080058 // FIXME kick off the work
59 //for (IntentData data : opMap.values()) {}
60 }
61
Sho SHIMIZU46f48fb2015-02-03 17:53:27 -080062 private Collection<IntentData> reduce(List<IntentData> ops) {
Ray Milkey5b3717e2015-02-05 11:44:08 -080063 Map<Key, IntentData> map = Maps.newHashMap();
Brian O'Connorcff03322015-02-03 15:28:59 -080064 for (IntentData op : ops) {
65 map.put(op.key(), op);
66 }
67 //TODO check the version... or maybe store will handle this.
Sho SHIMIZU46f48fb2015-02-03 17:53:27 -080068 return map.values();
Brian O'Connorcff03322015-02-03 15:28:59 -080069 }
70}