Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 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.onosproject.net.intent; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collections; |
| 23 | import java.util.List; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
Sho SHIMIZU | 30f45fb | 2014-12-03 18:13:26 -0800 | [diff] [blame] | 27 | public final class BatchWrite { |
Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 28 | |
| 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 SHIMIZU | 30f45fb | 2014-12-03 18:13:26 -0800 | [diff] [blame] | 39 | 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 SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 50 | 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 SHIMIZU | 86f6198 | 2015-01-20 13:32:57 -0800 | [diff] [blame] | 95 | public static final class Operation { |
Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 96 | 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 SHIMIZU | 86f6198 | 2015-01-20 13:32:57 -0800 | [diff] [blame] | 103 | private Operation(OpType type, List<Object> args) { |
Sho SHIMIZU | 64ae11c | 2014-12-03 15:17:47 -0800 | [diff] [blame] | 104 | 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 | } |