Brian O'Connor
Committed by Gerrit Code Review

Adding DistributedIntentBatchQueue

Change-Id: I4ce3c2cb5a31ad8ace5d1d20830a49a869e63900
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.intent.impl;
17 +
18 +import com.google.common.collect.ImmutableSet;
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;
23 +import org.onlab.onos.net.intent.IntentBatchDelegate;
24 +import org.onlab.onos.net.intent.IntentBatchService;
25 +import org.onlab.onos.net.intent.IntentOperations;
26 +import org.slf4j.Logger;
27 +
28 +import java.util.HashSet;
29 +import java.util.Set;
30 +
31 +import static com.google.common.base.Preconditions.checkNotNull;
32 +import static com.google.common.base.Preconditions.checkState;
33 +import static org.slf4j.LoggerFactory.getLogger;
34 +
35 +@Component(immediate = true)
36 +@Service
37 +public 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 +}