blob: f23a44878e3efeb18c79e2b8231a1a5df238182b [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) {
40 this.appId = checkNotNull(appId, "Application ID cannot be null");
41 this.id = checkNotNull(id, "Fingerprint cannot be null");
42 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) {
80 return IntentId.valueOf(Objects.hash(fields));
81 }
82
83 /**
84 * Indicates whether or not the intent is installable.
85 *
86 * @return true if installable
87 */
88 public boolean isInstallable() {
89 return false;
90 }
91
92 @Override
93 public int hashCode() {
94 return Objects.hash(id);
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (this == obj) {
100 return true;
101 }
102 if (obj == null || getClass() != obj.getClass()) {
103 return false;
104 }
105 final Intent other = (Intent) obj;
106 return Objects.equals(this.id, other.id);
107 }
108
Brian O'Connorb876bf12014-10-02 14:59:37 -0700109}