blob: 4c11f5786c7423ac65c81689e59d5f3054cb3731 [file] [log] [blame]
Brian O'Connorb876bf12014-10-02 14:59:37 -07001package org.onlab.onos.net.intent;
2
Thomas Vachuskac96058a2014-10-20 23:00:16 -07003import org.onlab.onos.ApplicationId;
4import org.onlab.onos.net.NetworkResource;
5
6import java.util.Collection;
7import java.util.Objects;
8
9import static com.google.common.base.Preconditions.checkNotNull;
10
Brian O'Connorb876bf12014-10-02 14:59:37 -070011/**
12 * Abstraction of an application level intent.
toma1d16b62014-10-02 23:45:11 -070013 * <p/>
Brian O'Connorb876bf12014-10-02 14:59:37 -070014 * Make sure that an Intent should be immutable when a new type is defined.
15 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070016public 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 Vachuska46c07ad2014-10-21 16:01:01 -070040 this.id = checkNotNull(id, "Intent ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070041 this.appId = checkNotNull(appId, "Application ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070042 this.resources = resources;
43 }
44
Brian O'Connorb876bf12014-10-02 14:59:37 -070045 /**
46 * Returns the intent identifier.
47 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070048 * @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'Connorb876bf12014-10-02 14:59:37 -070077 * @return intent identifier
78 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070079 protected static IntentId id(Object... fields) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070080 // FIXME: spread the bits across the full long spectrum
Thomas Vachuskac96058a2014-10-20 23:00:16 -070081 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 Vachuska46c07ad2014-10-21 16:01:01 -070094 public final int hashCode() {
Thomas Vachuskac96058a2014-10-20 23:00:16 -070095 return Objects.hash(id);
96 }
97
98 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070099 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700100 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'Connorb876bf12014-10-02 14:59:37 -0700110}