blob: d1d4c3d7ce5b67cd59f6034a6d1037ff753270ca [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 */
Brian O'Connorcff03322015-02-03 15:28:59 -080034@Deprecated //DELETEME
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070035public final class IntentOperations {
Brian O'Connorb876bf12014-10-02 14:59:37 -070036
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070037 private final List<IntentOperation> operations;
Brian O'Connor72a034c2014-11-26 18:24:23 -080038 private final ApplicationId appId;
Brian O'Connorb876bf12014-10-02 14:59:37 -070039
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070040 /**
41 * Creates a batch of intent operations using the supplied list.
42 *
43 * @param operations list of intent operations
44 */
Brian O'Connor72a034c2014-11-26 18:24:23 -080045 private IntentOperations(List<IntentOperation> operations, ApplicationId appId) {
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080046 checkNotNull(operations);
47 checkNotNull(appId);
Sho SHIMIZU351e2f62014-12-03 14:20:06 -080048 // TODO: consider check whether operations are not empty because empty batch is meaningless
49 // but it affects the existing code to add this checking
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080050
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070051 this.operations = operations;
Brian O'Connor72a034c2014-11-26 18:24:23 -080052 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070053 }
54
55 /**
56 * List of operations that need to be executed as a unit.
57 *
58 * @return list of intent operations
59 */
60 public List<IntentOperation> operations() {
61 return operations;
62 }
63
Brian O'Connor72a034c2014-11-26 18:24:23 -080064 public ApplicationId appId() {
65 return appId;
66 }
67
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070068 /**
69 * Returns a builder for intent operation batches.
70 *
71 * @return intent operations builder
Brian O'Connor72a034c2014-11-26 18:24:23 -080072 * @param applicationId application id
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070073 */
Brian O'Connor72a034c2014-11-26 18:24:23 -080074 public static Builder builder(ApplicationId applicationId) {
75 return new Builder(applicationId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070076 }
77
Brian O'Connorfa81eae2014-10-30 13:20:05 -070078 @Override
79 public int hashCode() {
80 return Objects.hash(operations);
81 }
82
83 @Override
84 public boolean equals(Object obj) {
85 if (this == obj) {
86 return true;
87 }
88 if (obj == null || getClass() != obj.getClass()) {
89 return false;
90 }
91 final IntentOperations other = (IntentOperations) obj;
92 return Objects.equals(this.operations, other.operations);
93 }
94
95 @Override
96 public String toString() {
97 return toStringHelper(this)
98 .add("operations", operations)
99 .toString();
100 }
101
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700102 /**
103 * Builder for batches of intent operations.
104 */
105 public static final class Builder {
106
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700107 private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
Brian O'Connor72a034c2014-11-26 18:24:23 -0800108 private final ApplicationId appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700109
110 // Public construction is forbidden.
Brian O'Connor72a034c2014-11-26 18:24:23 -0800111 private Builder(ApplicationId appId) {
112 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700113 }
114
115 /**
116 * Adds an intent submit operation.
117 *
118 * @param intent intent to be submitted
119 * @return self
120 */
121 public Builder addSubmitOperation(Intent intent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700122 checkNotNull(intent, "Intent cannot be null");
Brian O'Connorcff03322015-02-03 15:28:59 -0800123 builder.add(new IntentOperation(SUBMIT, intent));
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700124 return this;
125 }
126
127 /**
128 * Adds an intent submit operation.
129 *
130 * @param oldIntentId intent to be replaced
131 * @param newIntent replacement intent
132 * @return self
133 */
134 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700135 checkNotNull(oldIntentId, "Intent ID cannot be null");
136 checkNotNull(newIntent, "Intent cannot be null");
Brian O'Connorcff03322015-02-03 15:28:59 -0800137 builder.add(new IntentOperation(REPLACE, newIntent)); //FIXME
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700138 return this;
139 }
140
141 /**
142 * Adds an intent submit operation.
143 *
144 * @param intentId identifier of the intent to be withdrawn
145 * @return self
146 */
147 public Builder addWithdrawOperation(IntentId intentId) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700148 checkNotNull(intentId, "Intent ID cannot be null");
Brian O'Connorcff03322015-02-03 15:28:59 -0800149 builder.add(new IntentOperation(WITHDRAW, null)); //FIXME
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700150 return this;
151 }
152
153 /**
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700154 * Adds an intent update operation.
155 *
156 * @param intentId identifier of the intent to be updated
157 * @return self
158 */
159 public Builder addUpdateOperation(IntentId intentId) {
160 checkNotNull(intentId, "Intent ID cannot be null");
Brian O'Connorcff03322015-02-03 15:28:59 -0800161 builder.add(new IntentOperation(UPDATE, null)); //FIXME
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700162 return this;
163 }
164
165 /**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700166 * Builds a batch of intent operations.
167 *
168 * @return immutable batch of intent operations
169 */
170 public IntentOperations build() {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800171 return new IntentOperations(builder.build(), appId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700172 }
173
174 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700175}