blob: 176ac851ef57c500d723d29ec63e35d0b50edc04 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070018import java.util.List;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070019import java.util.Objects;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070020
Ray Milkeyc8f481f2014-11-18 15:37:12 -080021import com.google.common.collect.ImmutableList;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.core.ApplicationId;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080023
Brian O'Connorfa81eae2014-10-30 13:20:05 -070024import static com.google.common.base.MoreObjects.toStringHelper;
Thomas Vachuska83e090e2014-10-22 14:25:35 -070025import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import static org.onosproject.net.intent.IntentOperation.Type.REPLACE;
27import static org.onosproject.net.intent.IntentOperation.Type.SUBMIT;
28import static org.onosproject.net.intent.IntentOperation.Type.UPDATE;
29import static org.onosproject.net.intent.IntentOperation.Type.WITHDRAW;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070030
Brian O'Connorb876bf12014-10-02 14:59:37 -070031/**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070032 * Batch of intent submit/withdraw/replace operations.
Brian O'Connorb876bf12014-10-02 14:59:37 -070033 */
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070034public final class IntentOperations {
Brian O'Connorb876bf12014-10-02 14:59:37 -070035
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070036 private final List<IntentOperation> operations;
Brian O'Connor72a034c2014-11-26 18:24:23 -080037 private final ApplicationId appId;
Brian O'Connorb876bf12014-10-02 14:59:37 -070038
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070039 /**
40 * Creates a batch of intent operations using the supplied list.
41 *
42 * @param operations list of intent operations
43 */
Brian O'Connor72a034c2014-11-26 18:24:23 -080044 private IntentOperations(List<IntentOperation> operations, ApplicationId appId) {
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080045 checkNotNull(operations);
46 checkNotNull(appId);
Sho SHIMIZU351e2f62014-12-03 14:20:06 -080047 // TODO: consider check whether operations are not empty because empty batch is meaningless
48 // but it affects the existing code to add this checking
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080049
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070050 this.operations = operations;
Brian O'Connor72a034c2014-11-26 18:24:23 -080051 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070052 }
53
54 /**
55 * List of operations that need to be executed as a unit.
56 *
57 * @return list of intent operations
58 */
59 public List<IntentOperation> operations() {
60 return operations;
61 }
62
Brian O'Connor72a034c2014-11-26 18:24:23 -080063 public ApplicationId appId() {
64 return appId;
65 }
66
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070067 /**
68 * Returns a builder for intent operation batches.
69 *
70 * @return intent operations builder
Brian O'Connor72a034c2014-11-26 18:24:23 -080071 * @param applicationId application id
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070072 */
Brian O'Connor72a034c2014-11-26 18:24:23 -080073 public static Builder builder(ApplicationId applicationId) {
74 return new Builder(applicationId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070075 }
76
Brian O'Connorfa81eae2014-10-30 13:20:05 -070077 @Override
78 public int hashCode() {
79 return Objects.hash(operations);
80 }
81
82 @Override
83 public boolean equals(Object obj) {
84 if (this == obj) {
85 return true;
86 }
87 if (obj == null || getClass() != obj.getClass()) {
88 return false;
89 }
90 final IntentOperations other = (IntentOperations) obj;
91 return Objects.equals(this.operations, other.operations);
92 }
93
94 @Override
95 public String toString() {
96 return toStringHelper(this)
97 .add("operations", operations)
98 .toString();
99 }
100
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700101 /**
102 * Builder for batches of intent operations.
103 */
104 public static final class Builder {
105
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700106 private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
Brian O'Connor72a034c2014-11-26 18:24:23 -0800107 private final ApplicationId appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700108
109 // Public construction is forbidden.
Brian O'Connor72a034c2014-11-26 18:24:23 -0800110 private Builder(ApplicationId appId) {
111 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700112 }
113
114 /**
115 * Adds an intent submit operation.
116 *
117 * @param intent intent to be submitted
118 * @return self
119 */
120 public Builder addSubmitOperation(Intent intent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700121 checkNotNull(intent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700122 builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
123 return this;
124 }
125
126 /**
127 * Adds an intent submit operation.
128 *
129 * @param oldIntentId intent to be replaced
130 * @param newIntent replacement intent
131 * @return self
132 */
133 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700134 checkNotNull(oldIntentId, "Intent ID cannot be null");
135 checkNotNull(newIntent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700136 builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
137 return this;
138 }
139
140 /**
141 * Adds an intent submit operation.
142 *
143 * @param intentId identifier of the intent to be withdrawn
144 * @return self
145 */
146 public Builder addWithdrawOperation(IntentId intentId) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700147 checkNotNull(intentId, "Intent ID cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700148 builder.add(new IntentOperation(WITHDRAW, intentId, null));
149 return this;
150 }
151
152 /**
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700153 * Adds an intent update operation.
154 *
155 * @param intentId identifier of the intent to be updated
156 * @return self
157 */
158 public Builder addUpdateOperation(IntentId intentId) {
159 checkNotNull(intentId, "Intent ID cannot be null");
160 builder.add(new IntentOperation(UPDATE, intentId, null));
161 return this;
162 }
163
164 /**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700165 * Builds a batch of intent operations.
166 *
167 * @return immutable batch of intent operations
168 */
169 public IntentOperations build() {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800170 return new IntentOperations(builder.build(), appId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700171 }
172
173 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700174}