blob: b6223c92001d99156587c5321ec94373f7ee558a [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;
Brian O'Connor72a034c2014-11-26 18:24:23 -080023import org.onlab.onos.core.ApplicationId;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070024import org.onlab.onos.net.intent.IntentBatchDelegate;
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080025import org.onlab.onos.net.intent.IntentBatchListener;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070026import org.onlab.onos.net.intent.IntentBatchService;
27import org.onlab.onos.net.intent.IntentOperations;
28import org.slf4j.Logger;
29
Brian O'Connor427a1762014-11-19 18:40:32 -080030import java.util.LinkedList;
31import java.util.Queue;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070032import java.util.Set;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35import static com.google.common.base.Preconditions.checkState;
36import static org.slf4j.LoggerFactory.getLogger;
37
38@Component(immediate = true)
39@Service
40public class SimpleIntentBatchQueue implements IntentBatchService {
41
42 private final Logger log = getLogger(getClass());
Brian O'Connor427a1762014-11-19 18:40:32 -080043 private final Queue<IntentOperations> pendingBatches = new LinkedList<>();
44 private final Set<IntentOperations> currentBatches = Sets.newHashSet();
Brian O'Connorfa81eae2014-10-30 13:20:05 -070045 private IntentBatchDelegate delegate;
46
47 @Activate
48 public void activate() {
49 log.info("Started");
50 }
51
52 @Deactivate
53 public void deactivate() {
54 log.info("Stopped");
55 }
56
57 @Override
58 public void addIntentOperations(IntentOperations operations) {
59 checkState(delegate != null, "No delegate set");
Brian O'Connor427a1762014-11-19 18:40:32 -080060 synchronized (this) {
61 pendingBatches.add(operations);
62 if (currentBatches.isEmpty()) {
63 IntentOperations work = pendingBatches.poll();
64 currentBatches.add(work);
65 delegate.execute(work);
66 }
67 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -070068 }
69
70 @Override
71 public void removeIntentOperations(IntentOperations operations) {
Brian O'Connor427a1762014-11-19 18:40:32 -080072 // we allow at most one outstanding batch at a time
73 synchronized (this) {
74 checkState(currentBatches.remove(operations), "Operations not found in current ops.");
75 checkState(currentBatches.isEmpty(), "More than one outstanding batch.");
76 IntentOperations work = pendingBatches.poll();
77 if (work != null) {
78 currentBatches.add(work);
79 delegate.execute(work);
80 }
81 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -070082 }
83
84 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080085 public Set<IntentOperations> getPendingOperations() {
86 synchronized (this) {
Brian O'Connor86f6f7f2014-12-01 17:02:45 -080087 Set<IntentOperations> set = Sets.newHashSet(pendingBatches);
88 set.addAll(currentBatches); // TODO refactor this current vs. pending
89 return set;
Brian O'Connor427a1762014-11-19 18:40:32 -080090 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -070091 }
92
93 @Override
Brian O'Connor72a034c2014-11-26 18:24:23 -080094 public boolean isLocalLeader(ApplicationId applicationId) {
95 return true;
96 }
97
98 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -070099 public void setDelegate(IntentBatchDelegate delegate) {
100 this.delegate = checkNotNull(delegate, "Delegate cannot be null");
101 }
102
103 @Override
104 public void unsetDelegate(IntentBatchDelegate delegate) {
105 if (this.delegate != null && this.delegate.equals(delegate)) {
106 this.delegate = null;
107 }
108 }
Brian O'Connor86f6f7f2014-12-01 17:02:45 -0800109
110 @Override
111 public void addListener(IntentBatchListener listener) {
112 // no-op
113 //TODO: we are always the master
114 }
115
116 @Override
117 public void removeListener(IntentBatchListener listener) {
118 // no-op
119 //TODO: we are always the master
120 }
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700121}