blob: 2a3e68dc9f0741528bb9182c9a565a9ebbd8addf [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;
22
Sho SHIMIZU46f48fb2015-02-03 17:53:27 -080023import java.util.Collection;
Brian O'Connorcff03322015-02-03 15:28:59 -080024import java.util.List;
25import java.util.Map;
26import java.util.Timer;
27
28/**
29 * An accumulator for building batches of intent operations. Only one batch should
30 * be in process per instance at a time.
31 */
32public class IntentAccumulator extends AbstractAccumulator<IntentData> {
33
34 private static final int DEFAULT_MAX_EVENTS = 1000;
35 private static final int DEFAULT_MAX_IDLE_MS = 10;
36 private static final int DEFAULT_MAX_BATCH_MS = 50;
37
38 // FIXME: Replace with a system-wide timer instance;
39 // TODO: Convert to use HashedWheelTimer or produce a variant of that; then decide which we want to adopt
40 private static final Timer TIMER = new Timer("intent-op-batching");
41
Brian O'Connorb499b352015-02-03 16:46:15 -080042 private final IntentBatchDelegate delegate;
43
Brian O'Connorcff03322015-02-03 15:28:59 -080044 /**
45 * Creates an intent operation accumulator.
46 */
Brian O'Connorb499b352015-02-03 16:46:15 -080047 protected IntentAccumulator(IntentBatchDelegate delegate) {
Brian O'Connorcff03322015-02-03 15:28:59 -080048 super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
Brian O'Connorb499b352015-02-03 16:46:15 -080049 this.delegate = delegate;
Brian O'Connorcff03322015-02-03 15:28:59 -080050 }
51
52 @Override
53 public void processEvents(List<IntentData> ops) {
Sho SHIMIZU46f48fb2015-02-03 17:53:27 -080054 delegate.execute(reduce(ops));
Brian O'Connorcff03322015-02-03 15:28:59 -080055 // FIXME kick off the work
56 //for (IntentData data : opMap.values()) {}
57 }
58
Sho SHIMIZU46f48fb2015-02-03 17:53:27 -080059 private Collection<IntentData> reduce(List<IntentData> ops) {
Brian O'Connorcff03322015-02-03 15:28:59 -080060 Map<String, IntentData> map = Maps.newHashMap();
61 for (IntentData op : ops) {
62 map.put(op.key(), op);
63 }
64 //TODO check the version... or maybe store will handle this.
Sho SHIMIZU46f48fb2015-02-03 17:53:27 -080065 return map.values();
Brian O'Connorcff03322015-02-03 15:28:59 -080066 }
67}