blob: e2c8f91b2790b1ab2868da3b9b98a28931ad69c6 [file] [log] [blame]
Thomas Vachuska1fb982f2014-10-22 14:09:17 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
19import java.util.Objects;
20
21import static com.google.common.base.MoreObjects.toStringHelper;
Sho SHIMIZUc647b1e2014-12-12 15:04:55 -080022import static com.google.common.base.Preconditions.checkNotNull;
Brian O'Connorfa81eae2014-10-30 13:20:05 -070023
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070024/**
25 * Abstraction of an intent-related operation, e.g. add, remove, replace.
26 */
Ray Milkeyc8f481f2014-11-18 15:37:12 -080027public final class IntentOperation {
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070028
29 private final Type type;
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070030 private final Intent intent;
31
32 /**
33 * Operation type.
34 */
Brian O'Connorfa81eae2014-10-30 13:20:05 -070035 public enum Type {
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070036 /**
37 * Indicates that an intent should be added.
38 */
39 SUBMIT,
40
41 /**
42 * Indicates that an intent should be removed.
43 */
44 WITHDRAW,
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070045 }
46
47 /**
48 * Creates an intent operation.
49 *
Brian O'Connorfa81eae2014-10-30 13:20:05 -070050 * @param type operation type
Brian O'Connorfa81eae2014-10-30 13:20:05 -070051 * @param intent intent subject
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070052 */
Brian O'Connorcff03322015-02-03 15:28:59 -080053 public IntentOperation(Type type, Intent intent) {
Sho SHIMIZUc647b1e2014-12-12 15:04:55 -080054 this.type = checkNotNull(type);
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070055 this.intent = intent;
56 }
57
58 /**
59 * Returns the type of the operation.
60 *
61 * @return operation type
62 */
63 public Type type() {
64 return type;
65 }
66
67 /**
68 * Returns the identifier of the intent to which this operation applies.
69 *
70 * @return intent identifier
71 */
72 public IntentId intentId() {
Brian O'Connorcff03322015-02-03 15:28:59 -080073 return intent.id();
74 }
75
Ray Milkey5b3717e2015-02-05 11:44:08 -080076 /**
77 * Returns the key for this intent.
78 *
79 * @return key value
80 */
81 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -080082 return intent.key();
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070083 }
84
85 /**
86 * Returns the intent to which this operation applied. For remove,
87 * this can be null.
88 *
89 * @return intent that is the subject of the operation; null for remove
90 */
91 public Intent intent() {
92 return intent;
93 }
94
Brian O'Connorfa81eae2014-10-30 13:20:05 -070095 @Override
96 public int hashCode() {
Brian O'Connorcff03322015-02-03 15:28:59 -080097 return Objects.hash(type, intent);
Brian O'Connorfa81eae2014-10-30 13:20:05 -070098 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105 if (obj == null || getClass() != obj.getClass()) {
106 return false;
107 }
108 final IntentOperation other = (IntentOperation) obj;
109 return Objects.equals(this.type, other.type) &&
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700110 Objects.equals(this.intent, other.intent);
111 }
112
113
114 @Override
115 public String toString() {
116 return toStringHelper(this)
117 .add("type", type)
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700118 .add("intent", intent)
119 .toString();
120 }
Thomas Vachuska1fb982f2014-10-22 14:09:17 -0700121}