blob: 032838357ac9a46677a2c7ea715a95b806d065e8 [file] [log] [blame]
Yoonseon Han096cea02017-05-15 15:10:41 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.incubator.net.virtual.impl.intent;
18
19import com.google.common.collect.Maps;
20import org.onlab.util.AbstractAccumulator;
21import org.onosproject.net.intent.IntentBatchDelegate;
22import org.onosproject.net.intent.IntentData;
23import org.onosproject.net.intent.Key;
24
25import java.util.Collection;
26import java.util.List;
27import java.util.Map;
28import java.util.Timer;
29
30/**
31 * An accumulator for building batches of intent operations for virtual network.
32 * Only one batch should be in process per instance at a time.
33 */
34public class VirtualIntentAccumulator extends AbstractAccumulator<IntentData> {
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
41 private static final Timer TIMER = new Timer("virtual-intent-op-batching");
42
43 private final IntentBatchDelegate delegate;
44
45 private volatile boolean ready;
46
47 /**
48 * Creates an intent operation accumulator.
49 *
50 * @param delegate the intent batch delegate
51 */
52 public VirtualIntentAccumulator(IntentBatchDelegate delegate) {
53 super(TIMER, DEFAULT_MAX_EVENTS, DEFAULT_MAX_BATCH_MS, DEFAULT_MAX_IDLE_MS);
54 this.delegate = delegate;
55 // Assume that the delegate is ready for work at the start
56 ready = true; //TODO validate the assumption that delegate is ready
57 }
58
59 @Override
60 public void processItems(List<IntentData> items) {
61 ready = false;
62 delegate.execute(reduce(items));
63 }
64
65 private Collection<IntentData> reduce(List<IntentData> ops) {
66 Map<Key, IntentData> map = Maps.newHashMap();
67 for (IntentData op : ops) {
68 map.put(op.key(), op);
69 }
70 //TODO check the version... or maybe store will handle this.
71 return map.values();
72 }
73
74 @Override
75 public boolean isReady() {
76 return ready;
77 }
78
79 public void ready() {
80 ready = true;
81 }
82}