blob: 68cda001d2fee09d728c1be7e0f92f2d425812d1 [file] [log] [blame]
Brian O'Connora8e7dd42014-11-17 16:55:44 -08001/*
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.intent.impl;
17
Brian O'Connore2ff25a2014-11-18 19:25:43 -080018import com.google.common.collect.Sets;
Brian O'Connora8e7dd42014-11-17 16:55:44 -080019import 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'Connore2ff25a2014-11-18 19:25:43 -080028import java.util.LinkedList;
29import java.util.Queue;
Brian O'Connora8e7dd42014-11-17 16:55:44 -080030import 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
Yuta HIGUCHI4490a732014-11-18 20:20:30 -080036// FIXME This is not distributed yet.
Brian O'Connora8e7dd42014-11-17 16:55:44 -080037@Component(immediate = true)
38@Service
39public class DistributedIntentBatchQueue implements IntentBatchService {
40
41 private final Logger log = getLogger(getClass());
Brian O'Connore2ff25a2014-11-18 19:25:43 -080042 private final Queue<IntentOperations> pendingBatches = new LinkedList<>();
43 private final Set<IntentOperations> currentBatches = Sets.newHashSet();
Brian O'Connora8e7dd42014-11-17 16:55:44 -080044 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'Connore2ff25a2014-11-18 19:25:43 -080059 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'Connora8e7dd42014-11-17 16:55:44 -080067 }
68
69 @Override
70 public void removeIntentOperations(IntentOperations operations) {
Brian O'Connore2ff25a2014-11-18 19:25:43 -080071 // 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'Connora8e7dd42014-11-17 16:55:44 -080081 }
82
83 @Override
Brian O'Connor427a1762014-11-19 18:40:32 -080084 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'Connora8e7dd42014-11-17 16:55:44 -080095 }
96
97 @Override
98 public void setDelegate(IntentBatchDelegate delegate) {
99 this.delegate = checkNotNull(delegate, "Delegate cannot be null");
100 }
101
102 @Override
103 public void unsetDelegate(IntentBatchDelegate delegate) {
104 if (this.delegate != null && this.delegate.equals(delegate)) {
105 this.delegate = null;
106 }
107 }
108}