blob: 12369cc7e0c977b2f5682f5f2808332c2a29af96 [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
23import java.util.List;
24import java.util.Map;
25import java.util.Timer;
26
27/**
28 * An accumulator for building batches of intent operations. Only one batch should
29 * be in process per instance at a time.
30 */
31public class IntentAccumulator extends AbstractAccumulator<IntentData> {
32
33 private static final int DEFAULT_MAX_EVENTS = 1000;
34 private static final int DEFAULT_MAX_IDLE_MS = 10;
35 private static final int DEFAULT_MAX_BATCH_MS = 50;
36
37 // FIXME: Replace with a system-wide timer instance;
38 // TODO: Convert to use HashedWheelTimer or produce a variant of that; then decide which we want to adopt
39 private static final Timer TIMER = new Timer("intent-op-batching");
40
Brian O'Connorb499b352015-02-03 16:46:15 -080041 private final IntentBatchDelegate delegate;
42
Brian O'Connorcff03322015-02-03 15:28:59 -080043 /**
44 * Creates an intent operation accumulator.
45 */
Brian O'Connorb499b352015-02-03 16:46:15 -080046 protected IntentAccumulator(IntentBatchDelegate delegate) {
Brian O'Connorcff03322015-02-03 15:28:59 -080047 super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
Brian O'Connorb499b352015-02-03 16:46:15 -080048 this.delegate = delegate;
Brian O'Connorcff03322015-02-03 15:28:59 -080049 }
50
51 @Override
52 public void processEvents(List<IntentData> ops) {
53 Map<String, IntentData> opMap = reduce(ops);
Brian O'Connorb499b352015-02-03 16:46:15 -080054 delegate.execute(opMap.values());
Brian O'Connorcff03322015-02-03 15:28:59 -080055 // FIXME kick off the work
56 //for (IntentData data : opMap.values()) {}
57 }
58
59 private Map<String, IntentData> reduce(List<IntentData> ops) {
60 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.
65 return map;
66 }
67}