Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 18 | |
| 19 | import java.util.Objects; |
| 20 | |
| 21 | import static com.google.common.base.MoreObjects.toStringHelper; |
Sho SHIMIZU | c647b1e | 2014-12-12 15:04:55 -0800 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 23 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Abstraction of an intent-related operation, e.g. add, remove, replace. |
| 26 | */ |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 27 | public final class IntentOperation { |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 28 | |
| 29 | private final Type type; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 30 | private final Intent intent; |
| 31 | |
| 32 | /** |
| 33 | * Operation type. |
| 34 | */ |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 35 | public enum Type { |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 36 | /** |
| 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 Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates an intent operation. |
| 49 | * |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 50 | * @param type operation type |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 51 | * @param intent intent subject |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 52 | */ |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 53 | public IntentOperation(Type type, Intent intent) { |
Sho SHIMIZU | c647b1e | 2014-12-12 15:04:55 -0800 | [diff] [blame] | 54 | this.type = checkNotNull(type); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 55 | 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'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 73 | return intent.id(); |
| 74 | } |
| 75 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Returns the key for this intent. |
| 78 | * |
| 79 | * @return key value |
| 80 | */ |
| 81 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 82 | return intent.key(); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 83 | } |
| 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public int hashCode() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 97 | return Objects.hash(type, intent); |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 98 | } |
| 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 110 | 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 118 | .add("intent", intent) |
| 119 | .toString(); |
| 120 | } |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 121 | } |