blob: f92eca09b09e54356f6bd889a342bbcd78aba897 [file] [log] [blame]
Brian O'Connorfa81eae2014-10-30 13:20:05 -07001/*
2 * Copyright 2014 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.onlab.onos.store.trivial.impl;
17
Brian O'Connor427a1762014-11-19 18:40:32 -080018import com.google.common.collect.Sets;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Service;
23import org.onlab.onos.net.intent.IntentBatchDelegate;
24import org.onlab.onos.net.intent.IntentBatchService;
25import org.onlab.onos.net.intent.IntentOperations;
26import org.slf4j.Logger;
27
Brian O'Connor427a1762014-11-19 18:40:32 -080028import java.util.LinkedList;
29import java.util.Queue;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070030import java.util.Set;
31
32import static com.google.common.base.Preconditions.checkNotNull;
33import static com.google.common.base.Preconditions.checkState;
34import static org.slf4j.LoggerFactory.getLogger;
35
36@Component(immediate = true)
37@Service
38public class SimpleIntentBatchQueue implements IntentBatchService {
39
40 private final Logger log = getLogger(getClass());
Brian O'Connor427a1762014-11-19 18:40:32 -080041 private final Queue<IntentOperations> pendingBatches = new LinkedList<>();
42 private final Set<IntentOperations> currentBatches = Sets.newHashSet();
Brian O'Connorfa81eae2014-10-30 13:20:05 -070043 private IntentBatchDelegate delegate;
44
45 @Activate
46 public void activate() {
47 log.info("Started");
48 }
49
50 @Deactivate
51 public void deactivate() {
52 log.info("Stopped");
53 }
54
55 @Override
56 public void addIntentOperations(IntentOperations operations) {
57 checkState(delegate != null, "No delegate set");
Brian O'Connor427a1762014-11-19 18:40:32 -080058 synchronized (this) {
59 pendingBatches.add(operations);
60 if (currentBatches.isEmpty()) {
61 IntentOperations work = pendingBatches.poll();
62 currentBatches.add(work);
63 delegate.execute(work);
64 }
65 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -070066 }
67
68 @Override
69 public void removeIntentOperations(IntentOperations operations) {
Brian O'Connor427a1762014-11-19 18:40:32 -080070 // we allow at most one outstanding batch at a time
71 synchronized (this) {
72 checkState(currentBatches.remove(operations), "Operations not found in current ops.");
73 checkState(currentBatches.isEmpty(), "More than one outstanding batch.");
74 IntentOperations work = pendingBatches.poll();
75 if (work != null) {
76 currentBatches.add(work);
77 delegate.execute(work);
78 }
79 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -070080 }
81
82 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080083 public Set<IntentOperations> getPendingOperations() {
84 synchronized (this) {
85 return Sets.newHashSet(pendingBatches);
86 }
87 }
88
89 @Override
90 public Set<IntentOperations> getCurrentOperations() {
91 synchronized (this) {
92 return Sets.newHashSet(currentBatches);
93 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -070094 }
95
96 @Override
97 public void setDelegate(IntentBatchDelegate delegate) {
98 this.delegate = checkNotNull(delegate, "Delegate cannot be null");
99 }
100
101 @Override
102 public void unsetDelegate(IntentBatchDelegate delegate) {
103 if (this.delegate != null && this.delegate.equals(delegate)) {
104 this.delegate = null;
105 }
106 }
107}