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; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.Map; |
| 25 | import 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 | */ |
| 31 | public 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'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame^] | 41 | private final IntentBatchDelegate delegate; |
| 42 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Creates an intent operation accumulator. |
| 45 | */ |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame^] | 46 | protected IntentAccumulator(IntentBatchDelegate delegate) { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 47 | 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^] | 48 | this.delegate = delegate; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void processEvents(List<IntentData> ops) { |
| 53 | Map<String, IntentData> opMap = reduce(ops); |
Brian O'Connor | b499b35 | 2015-02-03 16:46:15 -0800 | [diff] [blame^] | 54 | delegate.execute(opMap.values()); |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 55 | // 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 | } |