blob: 0806d8e24c7f0eaa110e0cb44da4d4f8c7d74016 [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
18import com.google.common.collect.ImmutableSet;
19import 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
28import java.util.HashSet;
29import java.util.Set;
30
31import static com.google.common.base.Preconditions.checkNotNull;
32import static com.google.common.base.Preconditions.checkState;
33import static org.slf4j.LoggerFactory.getLogger;
34
35@Component(immediate = true)
36@Service
37public class DistributedIntentBatchQueue implements IntentBatchService {
38
39 private final Logger log = getLogger(getClass());
40 private final Set<IntentOperations> pendingBatches = new HashSet<>();
41 private IntentBatchDelegate delegate;
42
43 @Activate
44 public void activate() {
45 log.info("Started");
46 }
47
48 @Deactivate
49 public void deactivate() {
50 log.info("Stopped");
51 }
52
53 @Override
54 public void addIntentOperations(IntentOperations operations) {
55 checkState(delegate != null, "No delegate set");
56 pendingBatches.add(operations);
57 delegate.execute(operations);
58 }
59
60 @Override
61 public void removeIntentOperations(IntentOperations operations) {
62 pendingBatches.remove(operations);
63 }
64
65 @Override
66 public Set<IntentOperations> getIntentOperations() {
67 return ImmutableSet.copyOf(pendingBatches);
68 }
69
70 @Override
71 public void setDelegate(IntentBatchDelegate delegate) {
72 this.delegate = checkNotNull(delegate, "Delegate cannot be null");
73 }
74
75 @Override
76 public void unsetDelegate(IntentBatchDelegate delegate) {
77 if (this.delegate != null && this.delegate.equals(delegate)) {
78 this.delegate = null;
79 }
80 }
81}