Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 3 | import org.onlab.onos.ApplicationId; |
| 4 | import org.onlab.onos.net.NetworkResource; |
| 5 | |
| 6 | import java.util.Collection; |
| 7 | import java.util.Objects; |
| 8 | |
| 9 | import static com.google.common.base.Preconditions.checkNotNull; |
| 10 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 11 | /** |
| 12 | * Abstraction of an application level intent. |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 13 | * <p/> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 14 | * Make sure that an Intent should be immutable when a new type is defined. |
| 15 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 16 | public abstract class Intent implements BatchOperationTarget { |
| 17 | |
| 18 | private final IntentId id; |
| 19 | private final ApplicationId appId; |
| 20 | private final Collection<NetworkResource> resources; |
| 21 | |
| 22 | /** |
| 23 | * Constructor for serializer. |
| 24 | */ |
| 25 | protected Intent() { |
| 26 | this.id = null; |
| 27 | this.appId = null; |
| 28 | this.resources = null; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Creates a new intent. |
| 33 | * |
| 34 | * @param id intent identifier |
| 35 | * @param appId application identifier |
| 36 | * @param resources required network resources (optional) |
| 37 | */ |
| 38 | protected Intent(IntentId id, ApplicationId appId, |
| 39 | Collection<NetworkResource> resources) { |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame^] | 40 | this.id = checkNotNull(id, "Intent ID cannot be null"); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 41 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 42 | this.resources = resources; |
| 43 | } |
| 44 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Returns the intent identifier. |
| 47 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 48 | * @return intent fingerprint |
| 49 | */ |
| 50 | public IntentId id() { |
| 51 | return id; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the identifier of the application that requested the intent. |
| 56 | * |
| 57 | * @return application identifier |
| 58 | */ |
| 59 | public ApplicationId appId() { |
| 60 | return appId; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the collection of resources required for this intent. |
| 65 | * |
| 66 | * @return collection of resources; may be null |
| 67 | */ |
| 68 | public Collection<NetworkResource> resources() { |
| 69 | return resources; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Produces an intent identifier backed by hash-like fingerprint for the |
| 74 | * specified class of intent and its constituent fields. |
| 75 | * |
| 76 | * @param fields intent fields |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 77 | * @return intent identifier |
| 78 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 79 | protected static IntentId id(Object... fields) { |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame^] | 80 | // FIXME: spread the bits across the full long spectrum |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 81 | return IntentId.valueOf(Objects.hash(fields)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Indicates whether or not the intent is installable. |
| 86 | * |
| 87 | * @return true if installable |
| 88 | */ |
| 89 | public boolean isInstallable() { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame^] | 94 | public final int hashCode() { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 95 | return Objects.hash(id); |
| 96 | } |
| 97 | |
| 98 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame^] | 99 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 100 | if (this == obj) { |
| 101 | return true; |
| 102 | } |
| 103 | if (obj == null || getClass() != obj.getClass()) { |
| 104 | return false; |
| 105 | } |
| 106 | final Intent other = (Intent) obj; |
| 107 | return Objects.equals(this.id, other.id); |
| 108 | } |
| 109 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 110 | } |