Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 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 | 3c4b00d | 2015-02-22 20:42:26 -0800 | [diff] [blame] | 45 | private volatile boolean ready; |
| 46 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 47 | /** |
| 48 | * Creates an intent operation accumulator. |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 49 | * |
| 50 | * @param delegate the intent batch delegate |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 51 | */ |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame] | 52 | protected IntentAccumulator(IntentBatchDelegate delegate) { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 53 | 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] | 54 | this.delegate = delegate; |
Brian O'Connor | 3c4b00d | 2015-02-22 20:42:26 -0800 | [diff] [blame] | 55 | // Assume that the delegate is ready for work at the start |
| 56 | ready = true; //TODO validate the assumption that delegate is ready |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 60 | public void processItems(List<IntentData> items) { |
Brian O'Connor | 3c4b00d | 2015-02-22 20:42:26 -0800 | [diff] [blame] | 61 | ready = false; |
Thomas Vachuska | ecb63c5 | 2015-02-19 10:03:48 -0800 | [diff] [blame] | 62 | delegate.execute(reduce(items)); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Sho SHIMIZU | 46f48fb | 2015-02-03 17:53:27 -0800 | [diff] [blame] | 65 | private Collection<IntentData> reduce(List<IntentData> ops) { |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 66 | Map<Key, IntentData> map = Maps.newHashMap(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 67 | for (IntentData op : ops) { |
| 68 | map.put(op.key(), op); |
| 69 | } |
| 70 | //TODO check the version... or maybe store will handle this. |
Sho SHIMIZU | 46f48fb | 2015-02-03 17:53:27 -0800 | [diff] [blame] | 71 | return map.values(); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 72 | } |
Brian O'Connor | 3c4b00d | 2015-02-22 20:42:26 -0800 | [diff] [blame] | 73 | |
| 74 | @Override |
| 75 | public boolean isReady() { |
| 76 | return ready; |
| 77 | } |
| 78 | |
| 79 | public void ready() { |
| 80 | ready = true; |
| 81 | } |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 82 | } |