Michele Santuari | 38dd82e | 2017-01-04 09:23:49 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Michele Santuari | 38dd82e | 2017-01-04 09:23:49 +0100 | [diff] [blame] | 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.domain; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | import static org.onosproject.net.domain.DomainIntentOperation.Type.ADD; |
| 25 | import static org.onosproject.net.domain.DomainIntentOperation.Type.REMOVE; |
| 26 | |
| 27 | /** |
| 28 | * A batch of domain intent operations that are broken into stages. |
| 29 | */ |
| 30 | public class DomainIntentOperations { |
| 31 | |
| 32 | private final List<DomainIntentOperation> stages; |
| 33 | private final DomainIntentOperationsContext callback; |
| 34 | |
| 35 | private DomainIntentOperations(List<DomainIntentOperation> stages, |
| 36 | DomainIntentOperationsContext cb) { |
| 37 | this.stages = stages; |
| 38 | this.callback = cb; |
| 39 | } |
| 40 | |
| 41 | // kryo-constructor |
| 42 | protected DomainIntentOperations() { |
| 43 | this.stages = null; |
| 44 | this.callback = null; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns a new builder. |
| 49 | * |
| 50 | * @return new builder |
| 51 | */ |
| 52 | public static Builder builder() { |
| 53 | return new Builder(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the domain intent operations as sets of stages that should be |
| 58 | * executed sequentially. |
| 59 | * |
| 60 | * @return domain intent stages |
| 61 | */ |
| 62 | public List<DomainIntentOperation> stages() { |
| 63 | return stages; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns the callback for this batch of operations. |
| 68 | * |
| 69 | * @return callback |
| 70 | */ |
| 71 | public DomainIntentOperationsContext callback() { |
| 72 | return callback; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public String toString() { |
| 77 | return MoreObjects.toStringHelper(this) |
| 78 | .add("stages", stages) |
| 79 | .toString(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * A builder for constructing domain intent operations. |
| 84 | */ |
| 85 | public static final class Builder { |
| 86 | |
| 87 | private final ImmutableList.Builder<DomainIntentOperation> listBuilder = |
| 88 | ImmutableList.builder(); |
| 89 | |
| 90 | // prevent use of the default constructor outside of this file; use the above method |
| 91 | private Builder() { |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Appends a domain intent add to the current stage. |
| 96 | * |
| 97 | * @param intent domain intent |
| 98 | * @return this |
| 99 | */ |
| 100 | public Builder add(DomainIntent intent) { |
| 101 | listBuilder.add(new DomainIntentOperation(intent, ADD)); |
| 102 | return this; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Appends an existing domain intent to the current stage. |
| 107 | * @param domainIntentOperation domain intent operation |
| 108 | * @return this |
| 109 | */ |
| 110 | public Builder operation(DomainIntentOperation domainIntentOperation) { |
| 111 | listBuilder.add(domainIntentOperation); |
| 112 | return this; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Appends a domain intent removal to the current stage. |
| 117 | * |
| 118 | * @param intent domain intent |
| 119 | * @return this |
| 120 | */ |
| 121 | // FIXME this is confusing, consider renaming |
| 122 | public Builder remove(DomainIntent intent) { |
| 123 | listBuilder.add(new DomainIntentOperation(intent, REMOVE)); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Builds the immutable domain intent operations. |
| 129 | * |
| 130 | * @return domain intent operations |
| 131 | */ |
| 132 | public DomainIntentOperations build() { |
| 133 | return build(NullDomainIntentOperationsContext.getInstance()); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Builds the immutable domain intent operations. |
| 138 | * |
| 139 | * @param cb the callback to call when this operation completes |
| 140 | * @return domain intent operations |
| 141 | */ |
| 142 | public DomainIntentOperations build(DomainIntentOperationsContext cb) { |
| 143 | checkNotNull(cb); |
| 144 | return new DomainIntentOperations(listBuilder.build(), cb); |
| 145 | } |
| 146 | } |
| 147 | } |