blob: 32f041428507a7860dbf2269158f6c9fa0166cf9 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070019package org.onlab.onos.net.intent;
20
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070021import com.google.common.collect.ImmutableList;
22
23import java.util.List;
24
Thomas Vachuska83e090e2014-10-22 14:25:35 -070025import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070026import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE;
27import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT;
28import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW;
29
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
64 /**
65 * Builder for batches of intent operations.
66 */
67 public static final class Builder {
68
Thomas Vachuska83e090e2014-10-22 14:25:35 -070069 private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070070
71 // Public construction is forbidden.
72 private Builder() {
73 }
74
75 /**
76 * Adds an intent submit operation.
77 *
78 * @param intent intent to be submitted
79 * @return self
80 */
81 public Builder addSubmitOperation(Intent intent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -070082 checkNotNull(intent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070083 builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
84 return this;
85 }
86
87 /**
88 * Adds an intent submit operation.
89 *
90 * @param oldIntentId intent to be replaced
91 * @param newIntent replacement intent
92 * @return self
93 */
94 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -070095 checkNotNull(oldIntentId, "Intent ID cannot be null");
96 checkNotNull(newIntent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070097 builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
98 return this;
99 }
100
101 /**
102 * Adds an intent submit operation.
103 *
104 * @param intentId identifier of the intent to be withdrawn
105 * @return self
106 */
107 public Builder addWithdrawOperation(IntentId intentId) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700108 checkNotNull(intentId, "Intent ID cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700109 builder.add(new IntentOperation(WITHDRAW, intentId, null));
110 return this;
111 }
112
113 /**
114 * Builds a batch of intent operations.
115 *
116 * @return immutable batch of intent operations
117 */
118 public IntentOperations build() {
119 return new IntentOperations(builder.build());
120 }
121
122 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700123}