Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.intent.impl; |
| 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import org.onlab.util.AbstractAccumulator; |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 20 | import org.onosproject.net.intent.IntentBatchDelegate; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 21 | import org.onosproject.net.intent.IntentData; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 22 | import org.onosproject.net.intent.Key; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 23 | |
Sho SHIMIZU | 46f48fb | 2015-02-03 17:53:27 -0800 | [diff] [blame] | 24 | import java.util.Collection; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 25 | import java.util.List; |
| 26 | import java.util.Map; |
| 27 | import 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 | */ |
| 33 | public 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 Vachuska | a132e3a | 2015-02-21 01:53:14 -0800 | [diff] [blame^] | 41 | private static final Timer TIMER = new Timer("onos-intent-op-batching"); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 42 | |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 43 | private final IntentBatchDelegate delegate; |
| 44 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 45 | /** |
| 46 | * Creates an intent operation accumulator. |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 47 | * |
| 48 | * @param delegate the intent batch delegate |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 49 | */ |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 50 | protected IntentAccumulator(IntentBatchDelegate delegate) { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 51 | super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 52 | this.delegate = delegate; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 56 | public void processItems(List<IntentData> items) { |
| 57 | delegate.execute(reduce(items)); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 58 | // FIXME kick off the work |
| 59 | //for (IntentData data : opMap.values()) {} |
| 60 | } |
| 61 | |
Sho SHIMIZU | 46f48fb | 2015-02-03 17:53:27 -0800 | [diff] [blame] | 62 | private Collection<IntentData> reduce(List<IntentData> ops) { |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 63 | Map<Key, IntentData> map = Maps.newHashMap(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 64 | for (IntentData op : ops) { |
| 65 | map.put(op.key(), op); |
| 66 | } |
| 67 | //TODO check the version... or maybe store will handle this. |
Sho SHIMIZU | 46f48fb | 2015-02-03 17:53:27 -0800 | [diff] [blame] | 68 | return map.values(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 69 | } |
| 70 | } |