blob: ee7c2433823135827a248af4b21c84e65b4bc662 [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'Connorb876bf12014-10-02 14:59:37 -070016package org.onlab.onos.net.intent;
17
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;
22
Brian O'Connorfa81eae2014-10-30 13:20:05 -070023import static com.google.common.base.MoreObjects.toStringHelper;
Thomas Vachuska83e090e2014-10-22 14:25:35 -070024import static com.google.common.base.Preconditions.checkNotNull;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080025import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE;
26import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT;
27import static org.onlab.onos.net.intent.IntentOperation.Type.UPDATE;
28import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070029
Brian O'Connorb876bf12014-10-02 14:59:37 -070030/**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070031 * Batch of intent submit/withdraw/replace operations.
Brian O'Connorb876bf12014-10-02 14:59:37 -070032 */
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070033public final class IntentOperations {
Brian O'Connorb876bf12014-10-02 14:59:37 -070034
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070035 private final List<IntentOperation> operations;
Brian O'Connorb876bf12014-10-02 14:59:37 -070036
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070037 /**
38 * Creates a batch of intent operations using the supplied list.
39 *
40 * @param operations list of intent operations
41 */
42 private IntentOperations(List<IntentOperation> operations) {
43 this.operations = operations;
44 }
45
46 /**
47 * List of operations that need to be executed as a unit.
48 *
49 * @return list of intent operations
50 */
51 public List<IntentOperation> operations() {
52 return operations;
53 }
54
55 /**
56 * Returns a builder for intent operation batches.
57 *
58 * @return intent operations builder
59 */
60 public static Builder builder() {
61 return new Builder();
62 }
63
Brian O'Connorfa81eae2014-10-30 13:20:05 -070064
65 @Override
66 public int hashCode() {
67 return Objects.hash(operations);
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj) {
73 return true;
74 }
75 if (obj == null || getClass() != obj.getClass()) {
76 return false;
77 }
78 final IntentOperations other = (IntentOperations) obj;
79 return Objects.equals(this.operations, other.operations);
80 }
81
82 @Override
83 public String toString() {
84 return toStringHelper(this)
85 .add("operations", operations)
86 .toString();
87 }
88
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070089 /**
90 * Builder for batches of intent operations.
91 */
92 public static final class Builder {
93
Thomas Vachuska83e090e2014-10-22 14:25:35 -070094 private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070095
96 // Public construction is forbidden.
97 private Builder() {
98 }
99
100 /**
101 * Adds an intent submit operation.
102 *
103 * @param intent intent to be submitted
104 * @return self
105 */
106 public Builder addSubmitOperation(Intent intent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700107 checkNotNull(intent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700108 builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
109 return this;
110 }
111
112 /**
113 * Adds an intent submit operation.
114 *
115 * @param oldIntentId intent to be replaced
116 * @param newIntent replacement intent
117 * @return self
118 */
119 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700120 checkNotNull(oldIntentId, "Intent ID cannot be null");
121 checkNotNull(newIntent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700122 builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
123 return this;
124 }
125
126 /**
127 * Adds an intent submit operation.
128 *
129 * @param intentId identifier of the intent to be withdrawn
130 * @return self
131 */
132 public Builder addWithdrawOperation(IntentId intentId) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700133 checkNotNull(intentId, "Intent ID cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700134 builder.add(new IntentOperation(WITHDRAW, intentId, null));
135 return this;
136 }
137
138 /**
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700139 * Adds an intent update operation.
140 *
141 * @param intentId identifier of the intent to be updated
142 * @return self
143 */
144 public Builder addUpdateOperation(IntentId intentId) {
145 checkNotNull(intentId, "Intent ID cannot be null");
146 builder.add(new IntentOperation(UPDATE, intentId, null));
147 return this;
148 }
149
150 /**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700151 * Builds a batch of intent operations.
152 *
153 * @return immutable batch of intent operations
154 */
155 public IntentOperations build() {
156 return new IntentOperations(builder.build());
157 }
158
159 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700160}