blob: 4c146d1f1b077786669e6e2559e1bd2c33b4e527 [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.Collection;
29import java.util.LinkedList;
30import java.util.Queue;
Brian O'Connora8e7dd42014-11-17 16:55:44 -080031import java.util.Set;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34import static com.google.common.base.Preconditions.checkState;
35import static org.slf4j.LoggerFactory.getLogger;
36
Yuta HIGUCHI4490a732014-11-18 20:20:30 -080037// FIXME This is not distributed yet.
Brian O'Connora8e7dd42014-11-17 16:55:44 -080038@Component(immediate = true)
39@Service
40public class DistributedIntentBatchQueue implements IntentBatchService {
41
42 private final Logger log = getLogger(getClass());
Brian O'Connore2ff25a2014-11-18 19:25:43 -080043 private final Queue<IntentOperations> pendingBatches = new LinkedList<>();
44 private final Set<IntentOperations> currentBatches = Sets.newHashSet();
Brian O'Connora8e7dd42014-11-17 16:55:44 -080045 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'Connore2ff25a2014-11-18 19:25:43 -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'Connora8e7dd42014-11-17 16:55:44 -080068 }
69
70 @Override
71 public void removeIntentOperations(IntentOperations operations) {
Brian O'Connore2ff25a2014-11-18 19:25:43 -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'Connora8e7dd42014-11-17 16:55:44 -080082 }
83
84 @Override
85 public Set<IntentOperations> getIntentOperations() {
Brian O'Connore2ff25a2014-11-18 19:25:43 -080086 Set<IntentOperations> set = Sets.newHashSet(currentBatches);
87 set.addAll((Collection) pendingBatches);
88 return set;
Brian O'Connora8e7dd42014-11-17 16:55:44 -080089 }
90
91 @Override
92 public void setDelegate(IntentBatchDelegate delegate) {
93 this.delegate = checkNotNull(delegate, "Delegate cannot be null");
94 }
95
96 @Override
97 public void unsetDelegate(IntentBatchDelegate delegate) {
98 if (this.delegate != null && this.delegate.equals(delegate)) {
99 this.delegate = null;
100 }
101 }
102}