Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onlab.onos.store.trivial.impl; |
| 17 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Sets; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Service; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 23 | import org.onlab.onos.core.ApplicationId; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.intent.IntentBatchDelegate; |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 25 | import org.onlab.onos.net.intent.IntentBatchListener; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.intent.IntentBatchService; |
| 27 | import org.onlab.onos.net.intent.IntentOperations; |
| 28 | import org.slf4j.Logger; |
| 29 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 30 | import java.util.LinkedList; |
| 31 | import java.util.Queue; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 32 | import java.util.Set; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | import static com.google.common.base.Preconditions.checkState; |
| 36 | import static org.slf4j.LoggerFactory.getLogger; |
| 37 | |
| 38 | @Component(immediate = true) |
| 39 | @Service |
| 40 | public class SimpleIntentBatchQueue implements IntentBatchService { |
| 41 | |
| 42 | private final Logger log = getLogger(getClass()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 43 | private final Queue<IntentOperations> pendingBatches = new LinkedList<>(); |
| 44 | private final Set<IntentOperations> currentBatches = Sets.newHashSet(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 45 | 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'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 60 | 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void removeIntentOperations(IntentOperations operations) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 72 | // 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 85 | public Set<IntentOperations> getPendingOperations() { |
| 86 | synchronized (this) { |
Brian O'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 87 | Set<IntentOperations> set = Sets.newHashSet(pendingBatches); |
| 88 | set.addAll(currentBatches); // TODO refactor this current vs. pending |
| 89 | return set; |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 90 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 94 | public boolean isLocalLeader(ApplicationId applicationId) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 99 | 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'Connor | 86f6f7f | 2014-12-01 17:02:45 -0800 | [diff] [blame] | 109 | |
| 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 121 | } |