blob: b668fcf905f1ee49e047bcecfdccbbfe9ab9cccc [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) {
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070045 this.operations = operations;
Brian O'Connor72a034c2014-11-26 18:24:23 -080046 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070047 }
48
49 /**
50 * List of operations that need to be executed as a unit.
51 *
52 * @return list of intent operations
53 */
54 public List<IntentOperation> operations() {
55 return operations;
56 }
57
Brian O'Connor72a034c2014-11-26 18:24:23 -080058 public ApplicationId appId() {
59 return appId;
60 }
61
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070062 /**
63 * Returns a builder for intent operation batches.
64 *
65 * @return intent operations builder
Brian O'Connor72a034c2014-11-26 18:24:23 -080066 * @param applicationId application id
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070067 */
Brian O'Connor72a034c2014-11-26 18:24:23 -080068 public static Builder builder(ApplicationId applicationId) {
69 return new Builder(applicationId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070070 }
71
Brian O'Connorfa81eae2014-10-30 13:20:05 -070072 @Override
73 public int hashCode() {
74 return Objects.hash(operations);
75 }
76
77 @Override
78 public boolean equals(Object obj) {
79 if (this == obj) {
80 return true;
81 }
82 if (obj == null || getClass() != obj.getClass()) {
83 return false;
84 }
85 final IntentOperations other = (IntentOperations) obj;
86 return Objects.equals(this.operations, other.operations);
87 }
88
89 @Override
90 public String toString() {
91 return toStringHelper(this)
92 .add("operations", operations)
93 .toString();
94 }
95
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070096 /**
97 * Builder for batches of intent operations.
98 */
99 public static final class Builder {
100
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700101 private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
Brian O'Connor72a034c2014-11-26 18:24:23 -0800102 private final ApplicationId appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700103
104 // Public construction is forbidden.
Brian O'Connor72a034c2014-11-26 18:24:23 -0800105 private Builder(ApplicationId appId) {
106 this.appId = appId;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700107 }
108
109 /**
110 * Adds an intent submit operation.
111 *
112 * @param intent intent to be submitted
113 * @return self
114 */
115 public Builder addSubmitOperation(Intent intent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700116 checkNotNull(intent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700117 builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
118 return this;
119 }
120
121 /**
122 * Adds an intent submit operation.
123 *
124 * @param oldIntentId intent to be replaced
125 * @param newIntent replacement intent
126 * @return self
127 */
128 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700129 checkNotNull(oldIntentId, "Intent ID cannot be null");
130 checkNotNull(newIntent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700131 builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
132 return this;
133 }
134
135 /**
136 * Adds an intent submit operation.
137 *
138 * @param intentId identifier of the intent to be withdrawn
139 * @return self
140 */
141 public Builder addWithdrawOperation(IntentId intentId) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700142 checkNotNull(intentId, "Intent ID cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700143 builder.add(new IntentOperation(WITHDRAW, intentId, null));
144 return this;
145 }
146
147 /**
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700148 * Adds an intent update operation.
149 *
150 * @param intentId identifier of the intent to be updated
151 * @return self
152 */
153 public Builder addUpdateOperation(IntentId intentId) {
154 checkNotNull(intentId, "Intent ID cannot be null");
155 builder.add(new IntentOperation(UPDATE, intentId, null));
156 return this;
157 }
158
159 /**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700160 * Builds a batch of intent operations.
161 *
162 * @return immutable batch of intent operations
163 */
164 public IntentOperations build() {
Brian O'Connor72a034c2014-11-26 18:24:23 -0800165 return new IntentOperations(builder.build(), appId);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700166 }
167
168 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700169}