blob: 4cca45d85fdf89b95e6137ef4369b7677d1b3753 [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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.core.ApplicationId;
19import org.onosproject.core.IdGenerator;
20import org.onosproject.net.NetworkResource;
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;
Brian O'Connor520c0522014-11-23 23:50:47 -080026import static com.google.common.base.Preconditions.checkState;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070027
Brian O'Connorb876bf12014-10-02 14:59:37 -070028/**
29 * Abstraction of an application level intent.
Thomas Vachuska4b420772014-10-30 16:46:17 -070030 * <p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070031 * Make sure that an Intent should be immutable when a new type is defined.
Thomas Vachuska4b420772014-10-30 16:46:17 -070032 * </p>
Brian O'Connorb876bf12014-10-02 14:59:37 -070033 */
Sho SHIMIZU5e5d4aa2015-01-26 16:12:11 -080034public abstract class Intent {
Thomas Vachuskac96058a2014-10-20 23:00:16 -070035
36 private final IntentId id;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080037
Thomas Vachuskac96058a2014-10-20 23:00:16 -070038 private final ApplicationId appId;
Ray Milkey5b3717e2015-02-05 11:44:08 -080039 private final Key key;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080040
Thomas Vachuskac96058a2014-10-20 23:00:16 -070041 private final Collection<NetworkResource> resources;
42
Brian O'Connor520c0522014-11-23 23:50:47 -080043 private static IdGenerator idGenerator;
44
Thomas Vachuskac96058a2014-10-20 23:00:16 -070045 /**
46 * Constructor for serializer.
47 */
48 protected Intent() {
49 this.id = null;
50 this.appId = null;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080051 this.key = null;
Thomas Vachuskac96058a2014-10-20 23:00:16 -070052 this.resources = null;
53 }
54
55 /**
56 * Creates a new intent.
57 *
Brian O'Connor520c0522014-11-23 23:50:47 -080058 * @param appId application identifier
59 * @param resources required network resources (optional)
Thomas Vachuskac96058a2014-10-20 23:00:16 -070060 */
Brian O'Connor520c0522014-11-23 23:50:47 -080061 protected Intent(ApplicationId appId,
Thomas Vachuskac96058a2014-10-20 23:00:16 -070062 Collection<NetworkResource> resources) {
Brian O'Connorea4d7d12015-01-28 16:37:46 -080063 this(appId, null, resources);
64 }
65
66 /**
67 * Creates a new intent.
68 *
69 * @param appId application identifier
70 * @param key optional key
71 * @param resources required network resources (optional)
72 */
73 protected Intent(ApplicationId appId,
Ray Milkey5b3717e2015-02-05 11:44:08 -080074 Key key,
Brian O'Connorea4d7d12015-01-28 16:37:46 -080075 Collection<NetworkResource> resources) {
Brian O'Connor520c0522014-11-23 23:50:47 -080076 checkState(idGenerator != null, "Id generator is not bound.");
77 this.id = IntentId.valueOf(idGenerator.getNewId());
Thomas Vachuskac96058a2014-10-20 23:00:16 -070078 this.appId = checkNotNull(appId, "Application ID cannot be null");
Ray Milkey5b3717e2015-02-05 11:44:08 -080079 this.key = (key != null) ? key : Key.of(id.fingerprint(), appId);
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080080 this.resources = checkNotNull(resources);
Thomas Vachuskac96058a2014-10-20 23:00:16 -070081 }
82
Brian O'Connorb876bf12014-10-02 14:59:37 -070083 /**
84 * Returns the intent identifier.
85 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070086 * @return intent fingerprint
87 */
88 public IntentId id() {
89 return id;
90 }
91
92 /**
93 * Returns the identifier of the application that requested the intent.
94 *
95 * @return application identifier
96 */
97 public ApplicationId appId() {
98 return appId;
99 }
100
101 /**
102 * Returns the collection of resources required for this intent.
103 *
104 * @return collection of resources; may be null
105 */
106 public Collection<NetworkResource> resources() {
107 return resources;
108 }
109
110 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700111 * Indicates whether or not the intent is installable.
112 *
113 * @return true if installable
114 */
115 public boolean isInstallable() {
116 return false;
117 }
118
119 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700120 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800121 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700122 }
123
124 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700125 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700126 if (this == obj) {
127 return true;
128 }
129 if (obj == null || getClass() != obj.getClass()) {
130 return false;
131 }
132 final Intent other = (Intent) obj;
Sho SHIMIZU23895232014-11-25 15:59:50 -0800133 return this.id().equals(other.id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700134 }
135
Brian O'Connor520c0522014-11-23 23:50:47 -0800136 /**
137 * Binds an id generator for unique intent id generation.
138 *
139 * Note: A generator cannot be bound if there is already a generator bound.
140 * @param newIdGenerator id generator
141 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800142 public static void bindIdGenerator(IdGenerator newIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800143 checkState(idGenerator == null, "Id generator is already bound.");
144 idGenerator = checkNotNull(newIdGenerator);
145 }
146
147 /**
148 * Unbinds an id generator.
149 *
150 * Note: The caller must provide the old id generator to succeed.
151 * @param oldIdGenerator the current id generator
152 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800153 public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800154 if (Objects.equals(idGenerator, oldIdGenerator)) {
155 idGenerator = null;
156 }
157 }
Brian O'Connorcff03322015-02-03 15:28:59 -0800158
Ray Milkey5b3717e2015-02-05 11:44:08 -0800159 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -0800160 return key;
161 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700162}