blob: 9e31ca345c5cc95b99901cbcc4efdd9aed7df96d [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 com.google.common.collect.ImmutableList;
19
20import java.util.List;
21
Thomas Vachuska83e090e2014-10-22 14:25:35 -070022import static com.google.common.base.Preconditions.checkNotNull;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070023import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE;
24import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT;
25import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW;
26
Brian O'Connorb876bf12014-10-02 14:59:37 -070027/**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070028 * Batch of intent submit/withdraw/replace operations.
Brian O'Connorb876bf12014-10-02 14:59:37 -070029 */
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070030public final class IntentOperations {
Brian O'Connorb876bf12014-10-02 14:59:37 -070031
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070032 private final List<IntentOperation> operations;
Brian O'Connorb876bf12014-10-02 14:59:37 -070033
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070034 /**
35 * Creates a batch of intent operations using the supplied list.
36 *
37 * @param operations list of intent operations
38 */
39 private IntentOperations(List<IntentOperation> operations) {
40 this.operations = operations;
41 }
42
43 /**
44 * List of operations that need to be executed as a unit.
45 *
46 * @return list of intent operations
47 */
48 public List<IntentOperation> operations() {
49 return operations;
50 }
51
52 /**
53 * Returns a builder for intent operation batches.
54 *
55 * @return intent operations builder
56 */
57 public static Builder builder() {
58 return new Builder();
59 }
60
61 /**
62 * Builder for batches of intent operations.
63 */
64 public static final class Builder {
65
Thomas Vachuska83e090e2014-10-22 14:25:35 -070066 private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070067
68 // Public construction is forbidden.
69 private Builder() {
70 }
71
72 /**
73 * Adds an intent submit operation.
74 *
75 * @param intent intent to be submitted
76 * @return self
77 */
78 public Builder addSubmitOperation(Intent intent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -070079 checkNotNull(intent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070080 builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
81 return this;
82 }
83
84 /**
85 * Adds an intent submit operation.
86 *
87 * @param oldIntentId intent to be replaced
88 * @param newIntent replacement intent
89 * @return self
90 */
91 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -070092 checkNotNull(oldIntentId, "Intent ID cannot be null");
93 checkNotNull(newIntent, "Intent cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070094 builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
95 return this;
96 }
97
98 /**
99 * Adds an intent submit operation.
100 *
101 * @param intentId identifier of the intent to be withdrawn
102 * @return self
103 */
104 public Builder addWithdrawOperation(IntentId intentId) {
Thomas Vachuska83e090e2014-10-22 14:25:35 -0700105 checkNotNull(intentId, "Intent ID cannot be null");
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700106 builder.add(new IntentOperation(WITHDRAW, intentId, null));
107 return this;
108 }
109
110 /**
111 * Builds a batch of intent operations.
112 *
113 * @return immutable batch of intent operations
114 */
115 public IntentOperations build() {
116 return new IntentOperations(builder.build());
117 }
118
119 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700120}