blob: 327bdfe988bcb449a771be2542efc7a9c0fd82e2 [file] [log] [blame]
Thomas Vachuska1fb982f2014-10-22 14:09:17 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska1fb982f2014-10-22 14:09:17 -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 Vachuska1fb982f2014-10-22 14:09:17 -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 Vachuska1fb982f2014-10-22 14:09:17 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070017
Brian O'Connorfa81eae2014-10-30 13:20:05 -070018
Brian O'Connor9476fa12015-06-25 15:17:17 -040019import com.google.common.annotations.Beta;
20
Brian O'Connorfa81eae2014-10-30 13:20:05 -070021import java.util.Objects;
22
23import static com.google.common.base.MoreObjects.toStringHelper;
Sho SHIMIZUc647b1e2014-12-12 15:04:55 -080024import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070025
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070026/**
27 * Abstraction of an intent-related operation, e.g. add, remove, replace.
28 */
Brian O'Connor9476fa12015-06-25 15:17:17 -040029@Beta
Ray Milkeyc8f481f2014-11-18 15:37:12 -080030public final class IntentOperation {
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070031
32 private final Type type;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070033 private final Intent intent;
34
35 /**
36 * Operation type.
37 */
Brian O'Connorfa81eae2014-10-30 13:20:05 -070038 public enum Type {
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070039 /**
40 * Indicates that an intent should be added.
41 */
42 SUBMIT,
43
44 /**
45 * Indicates that an intent should be removed.
46 */
47 WITHDRAW,
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070048 }
49
50 /**
51 * Creates an intent operation.
52 *
Brian O'Connorfa81eae2014-10-30 13:20:05 -070053 * @param type operation type
Brian O'Connorfa81eae2014-10-30 13:20:05 -070054 * @param intent intent subject
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070055 */
Brian O'Connorcff03322015-02-03 15:28:59 -080056 public IntentOperation(Type type, Intent intent) {
Sho SHIMIZUc647b1e2014-12-12 15:04:55 -080057 this.type = checkNotNull(type);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070058 this.intent = intent;
59 }
60
61 /**
62 * Returns the type of the operation.
63 *
64 * @return operation type
65 */
66 public Type type() {
67 return type;
68 }
69
70 /**
71 * Returns the identifier of the intent to which this operation applies.
72 *
73 * @return intent identifier
74 */
75 public IntentId intentId() {
Brian O'Connorcff03322015-02-03 15:28:59 -080076 return intent.id();
77 }
78
Ray Milkey5b3717e2015-02-05 11:44:08 -080079 /**
80 * Returns the key for this intent.
81 *
82 * @return key value
83 */
84 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -080085 return intent.key();
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070086 }
87
88 /**
89 * Returns the intent to which this operation applied. For remove,
90 * this can be null.
91 *
92 * @return intent that is the subject of the operation; null for remove
93 */
94 public Intent intent() {
95 return intent;
96 }
97
Brian O'Connorfa81eae2014-10-30 13:20:05 -070098 @Override
99 public int hashCode() {
Brian O'Connorcff03322015-02-03 15:28:59 -0800100 return Objects.hash(type, intent);
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700101 }
102
103 @Override
104 public boolean equals(Object obj) {
105 if (this == obj) {
106 return true;
107 }
108 if (obj == null || getClass() != obj.getClass()) {
109 return false;
110 }
111 final IntentOperation other = (IntentOperation) obj;
112 return Objects.equals(this.type, other.type) &&
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700113 Objects.equals(this.intent, other.intent);
114 }
115
116
117 @Override
118 public String toString() {
119 return toStringHelper(this)
120 .add("type", type)
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700121 .add("intent", intent)
122 .toString();
123 }
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700124}