blob: e71324335624baa12998f074146f8e0a92d4b4a2 [file] [log] [blame]
Sho SHIMIZU64ae11c2014-12-03 15:17:47 -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.onosproject.net.intent;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableList;
20
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.List;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
Sho SHIMIZU30f45fb2014-12-03 18:13:26 -080027public final class BatchWrite {
Sho SHIMIZU64ae11c2014-12-03 15:17:47 -080028
29 public enum OpType {
30 CREATE_INTENT,
31 REMOVE_INTENT,
32 SET_STATE,
33 SET_INSTALLABLE,
34 REMOVE_INSTALLED
35 }
36
37 List<Operation> operations = new ArrayList<>();
38
Sho SHIMIZU30f45fb2014-12-03 18:13:26 -080039 private BatchWrite() {}
40
41 /**
42 * Returns a new empty batch write operation builder.
43 *
44 * @return BatchWrite
45 */
46 public static BatchWrite newInstance() {
47 return new BatchWrite();
48 }
49
Sho SHIMIZU64ae11c2014-12-03 15:17:47 -080050 public List<Operation> operations() {
51 return Collections.unmodifiableList(operations);
52 }
53
54 public boolean isEmpty() {
55 return operations.isEmpty();
56 }
57
58 public BatchWrite createIntent(Intent intent) {
59 operations.add(Operation.of(OpType.CREATE_INTENT,
60 ImmutableList.of(intent)));
61 return this;
62 }
63
64 public BatchWrite removeIntent(IntentId intentId) {
65 operations.add(Operation.of(OpType.REMOVE_INTENT,
66 ImmutableList.of(intentId)));
67 return this;
68 }
69
70 public BatchWrite setState(Intent intent, IntentState newState) {
71 operations.add(Operation.of(OpType.SET_STATE,
72 ImmutableList.of(intent, newState)));
73 return this;
74 }
75
76 public BatchWrite setInstallableIntents(IntentId intentId, List<Intent> installableIntents) {
77 operations.add(Operation.of(OpType.SET_INSTALLABLE,
78 ImmutableList.of(intentId, installableIntents)));
79 return this;
80 }
81
82 public BatchWrite removeInstalledIntents(IntentId intentId) {
83 operations.add(Operation.of(OpType.REMOVE_INSTALLED,
84 ImmutableList.of(intentId)));
85 return this;
86 }
87
88 @Override
89 public String toString() {
90 return MoreObjects.toStringHelper(getClass())
91 .add("operations", operations)
92 .toString();
93 }
94
Sho SHIMIZU86f61982015-01-20 13:32:57 -080095 public static final class Operation {
Sho SHIMIZU64ae11c2014-12-03 15:17:47 -080096 final OpType type;
97 final ImmutableList<Object> args;
98
99 public static Operation of(OpType type, List<Object> args) {
100 return new Operation(type, args);
101 }
102
Sho SHIMIZU86f61982015-01-20 13:32:57 -0800103 private Operation(OpType type, List<Object> args) {
Sho SHIMIZU64ae11c2014-12-03 15:17:47 -0800104 this.type = checkNotNull(type);
105 this.args = ImmutableList.copyOf(args);
106 }
107
108 public OpType type() {
109 return type;
110 }
111
112 public ImmutableList<Object> args() {
113 return args;
114 }
115
116 @SuppressWarnings("unchecked")
117 public <T> T arg(int i) {
118 return (T) args.get(i);
119 }
120
121 @Override
122 public String toString() {
123 return MoreObjects.toStringHelper(getClass())
124 .add("type", type)
125 .add("args", args)
126 .toString();
127 }
128 }
129}