blob: 92161f4f54fc3e98c2e0dbac546f53ba761cb3f9 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -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 Vachuska83e090e2014-10-22 14:25:35 -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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorb876bf12014-10-02 14:59:37 -070016package org.onlab.onos.net.intent;
17
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070018import org.onlab.onos.core.ApplicationId;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070019import org.onlab.onos.net.NetworkResource;
Thomas Vachuska83e090e2014-10-22 14:25:35 -070020import org.onlab.onos.net.flow.BatchOperationTarget;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070021
22import java.util.Collection;
23import java.util.Objects;
24
25import static com.google.common.base.Preconditions.checkNotNull;
26
Brian O'Connorb876bf12014-10-02 14:59:37 -070027/**
28 * Abstraction of an application level intent.
toma1d16b62014-10-02 23:45:11 -070029 * <p/>
Brian O'Connorb876bf12014-10-02 14:59:37 -070030 * Make sure that an Intent should be immutable when a new type is defined.
31 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070032public abstract class Intent implements BatchOperationTarget {
33
34 private final IntentId id;
35 private final ApplicationId appId;
36 private final Collection<NetworkResource> resources;
37
38 /**
39 * Constructor for serializer.
40 */
41 protected Intent() {
42 this.id = null;
43 this.appId = null;
44 this.resources = null;
45 }
46
47 /**
48 * Creates a new intent.
49 *
50 * @param id intent identifier
51 * @param appId application identifier
52 * @param resources required network resources (optional)
53 */
54 protected Intent(IntentId id, ApplicationId appId,
55 Collection<NetworkResource> resources) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070056 this.id = checkNotNull(id, "Intent ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070057 this.appId = checkNotNull(appId, "Application ID cannot be null");
Thomas Vachuskac96058a2014-10-20 23:00:16 -070058 this.resources = resources;
59 }
60
Brian O'Connorb876bf12014-10-02 14:59:37 -070061 /**
62 * Returns the intent identifier.
63 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070064 * @return intent fingerprint
65 */
66 public IntentId id() {
67 return id;
68 }
69
70 /**
71 * Returns the identifier of the application that requested the intent.
72 *
73 * @return application identifier
74 */
75 public ApplicationId appId() {
76 return appId;
77 }
78
79 /**
80 * Returns the collection of resources required for this intent.
81 *
82 * @return collection of resources; may be null
83 */
84 public Collection<NetworkResource> resources() {
85 return resources;
86 }
87
88 /**
89 * Produces an intent identifier backed by hash-like fingerprint for the
90 * specified class of intent and its constituent fields.
91 *
92 * @param fields intent fields
Brian O'Connorb876bf12014-10-02 14:59:37 -070093 * @return intent identifier
94 */
Thomas Vachuskac96058a2014-10-20 23:00:16 -070095 protected static IntentId id(Object... fields) {
Thomas Vachuska46c07ad2014-10-21 16:01:01 -070096 // FIXME: spread the bits across the full long spectrum
Thomas Vachuskac96058a2014-10-20 23:00:16 -070097 return IntentId.valueOf(Objects.hash(fields));
98 }
99
100 /**
101 * Indicates whether or not the intent is installable.
102 *
103 * @return true if installable
104 */
105 public boolean isInstallable() {
106 return false;
107 }
108
109 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700110 public final int hashCode() {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700111 return Objects.hash(id);
112 }
113
114 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700115 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700116 if (this == obj) {
117 return true;
118 }
119 if (obj == null || getClass() != obj.getClass()) {
120 return false;
121 }
122 final Intent other = (Intent) obj;
123 return Objects.equals(this.id, other.id);
124 }
125
Brian O'Connorb876bf12014-10-02 14:59:37 -0700126}