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; |
| 25 | import org.onlab.onos.net.intent.IntentBatchService; |
| 26 | import org.onlab.onos.net.intent.IntentOperations; |
| 27 | import org.slf4j.Logger; |
| 28 | |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 29 | import java.util.LinkedList; |
| 30 | import java.util.Queue; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 31 | import java.util.Set; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | import static com.google.common.base.Preconditions.checkState; |
| 35 | import static org.slf4j.LoggerFactory.getLogger; |
| 36 | |
| 37 | @Component(immediate = true) |
| 38 | @Service |
| 39 | public class SimpleIntentBatchQueue implements IntentBatchService { |
| 40 | |
| 41 | private final Logger log = getLogger(getClass()); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 42 | private final Queue<IntentOperations> pendingBatches = new LinkedList<>(); |
| 43 | private final Set<IntentOperations> currentBatches = Sets.newHashSet(); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 44 | private IntentBatchDelegate delegate; |
| 45 | |
| 46 | @Activate |
| 47 | public void activate() { |
| 48 | log.info("Started"); |
| 49 | } |
| 50 | |
| 51 | @Deactivate |
| 52 | public void deactivate() { |
| 53 | log.info("Stopped"); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void addIntentOperations(IntentOperations operations) { |
| 58 | checkState(delegate != null, "No delegate set"); |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 59 | synchronized (this) { |
| 60 | pendingBatches.add(operations); |
| 61 | if (currentBatches.isEmpty()) { |
| 62 | IntentOperations work = pendingBatches.poll(); |
| 63 | currentBatches.add(work); |
| 64 | delegate.execute(work); |
| 65 | } |
| 66 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void removeIntentOperations(IntentOperations operations) { |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 71 | // we allow at most one outstanding batch at a time |
| 72 | synchronized (this) { |
| 73 | checkState(currentBatches.remove(operations), "Operations not found in current ops."); |
| 74 | checkState(currentBatches.isEmpty(), "More than one outstanding batch."); |
| 75 | IntentOperations work = pendingBatches.poll(); |
| 76 | if (work != null) { |
| 77 | currentBatches.add(work); |
| 78 | delegate.execute(work); |
| 79 | } |
| 80 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
Brian O'Connor | 427a176 | 2014-11-19 18:40:32 -0800 | [diff] [blame] | 84 | public Set<IntentOperations> getPendingOperations() { |
| 85 | synchronized (this) { |
| 86 | return Sets.newHashSet(pendingBatches); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Set<IntentOperations> getCurrentOperations() { |
| 92 | synchronized (this) { |
| 93 | return Sets.newHashSet(currentBatches); |
| 94 | } |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame^] | 98 | public boolean isLocalLeader(ApplicationId applicationId) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 103 | public void setDelegate(IntentBatchDelegate delegate) { |
| 104 | this.delegate = checkNotNull(delegate, "Delegate cannot be null"); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void unsetDelegate(IntentBatchDelegate delegate) { |
| 109 | if (this.delegate != null && this.delegate.equals(delegate)) { |
| 110 | this.delegate = null; |
| 111 | } |
| 112 | } |
| 113 | } |