blob: a9b3d44a32de6ba5202a7b54060b41d58709c913 [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);
47
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070048 this.operations = operations;
Brian O'Connor72a034c2014-11-26 18:24:23 -080049 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070050 }
51
52 /**
53 * List of operations that need to be executed as a unit.
54 *
55 * @return list of intent operations
56 */
57 public List<IntentOperation> operations() {
58 return operations;
59 }
60
Brian O'Connor72a034c2014-11-26 18:24:23 -080061 public ApplicationId appId() {
62 return appId;
63 }
64
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070065 /**
66 * Returns a builder for intent operation batches.
67 *
68 * @return intent operations builder
Brian O'Connor72a034c2014-11-26 18:24:23 -080069 * @param applicationId application id
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070070 */
Brian O'Connor72a034c2014-11-26 18:24:23 -080071 public static Builder builder(ApplicationId applicationId) {
72 return new Builder(applicationId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070073 }
74
Brian O'Connorfa81eae2014-10-30 13:20:05 -070075 @Override
76 public int hashCode() {
77 return Objects.hash(operations);
78 }
79
80 @Override
81 public boolean equals(Object obj) {
82 if (this == obj) {
83 return true;
84 }
85 if (obj == null || getClass() != obj.getClass()) {
86 return false;
87 }
88 final IntentOperations other = (IntentOperations) obj;
89 return Objects.equals(this.operations, other.operations);
90 }
91
92 @Override
93 public String toString() {
94 return toStringHelper(this)
95 .add("operations", operations)
96 .toString();
97 }
98
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070099 /**
100 * Builder for batches of intent operations.
101 */
102 public static final class Builder {
103
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700104 private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
Brian O'Connor72a034c2014-11-26 18:24:23 -0800105 private final ApplicationId appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700106
107 // Public construction is forbidden.
Brian O'Connor72a034c2014-11-26 18:24:23 -0800108 private Builder(ApplicationId appId) {
109 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700110 }
111
112 /**
113 * Adds an intent submit operation.
114 *
115 * @param intent intent to be submitted
116 * @return self
117 */
118 public Builder addSubmitOperation(Intent intent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700119 checkNotNull(intent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700120 builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
121 return this;
122 }
123
124 /**
125 * Adds an intent submit operation.
126 *
127 * @param oldIntentId intent to be replaced
128 * @param newIntent replacement intent
129 * @return self
130 */
131 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700132 checkNotNull(oldIntentId, "Intent ID cannot be null");
133 checkNotNull(newIntent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700134 builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
135 return this;
136 }
137
138 /**
139 * Adds an intent submit operation.
140 *
141 * @param intentId identifier of the intent to be withdrawn
142 * @return self
143 */
144 public Builder addWithdrawOperation(IntentId intentId) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700145 checkNotNull(intentId, "Intent ID cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700146 builder.add(new IntentOperation(WITHDRAW, intentId, null));
147 return this;
148 }
149
150 /**
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700151 * Adds an intent update operation.
152 *
153 * @param intentId identifier of the intent to be updated
154 * @return self
155 */
156 public Builder addUpdateOperation(IntentId intentId) {
157 checkNotNull(intentId, "Intent ID cannot be null");
158 builder.add(new IntentOperation(UPDATE, intentId, null));
159 return this;
160 }
161
162 /**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700163 * Builds a batch of intent operations.
164 *
165 * @return immutable batch of intent operations
166 */
167 public IntentOperations build() {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800168 return new IntentOperations(builder.build(), appId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700169 }
170
171 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700172}