blob: 19170aed1f837420233058e5273429ad7b12a281 [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;
Brian O'Connorea4d7d12015-01-28 16:37:46 -080039 private final String key;
40
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'Connorea4d7d12015-01-28 16:37:46 -080061 @Deprecated
Brian O'Connor520c0522014-11-23 23:50:47 -080062 protected Intent(ApplicationId appId,
Thomas Vachuskac96058a2014-10-20 23:00:16 -070063 Collection<NetworkResource> resources) {
Brian O'Connorea4d7d12015-01-28 16:37:46 -080064 this(appId, null, resources);
65 }
66
67 /**
68 * Creates a new intent.
69 *
70 * @param appId application identifier
71 * @param key optional key
72 * @param resources required network resources (optional)
73 */
74 protected Intent(ApplicationId appId,
75 String key,
76 Collection<NetworkResource> resources) {
Brian O'Connor520c0522014-11-23 23:50:47 -080077 checkState(idGenerator != null, "Id generator is not bound.");
78 this.id = IntentId.valueOf(idGenerator.getNewId());
Thomas Vachuskac96058a2014-10-20 23:00:16 -070079 this.appId = checkNotNull(appId, "Application ID cannot be null");
Brian O'Connorea4d7d12015-01-28 16:37:46 -080080 this.key = (key != null) ? key : id.toString(); //FIXME
Sho SHIMIZUd7d18002015-01-21 14:37:14 -080081 this.resources = checkNotNull(resources);
Thomas Vachuskac96058a2014-10-20 23:00:16 -070082 }
83
Brian O'Connorb876bf12014-10-02 14:59:37 -070084 /**
85 * Returns the intent identifier.
86 *
Thomas Vachuskac96058a2014-10-20 23:00:16 -070087 * @return intent fingerprint
88 */
89 public IntentId id() {
90 return id;
91 }
92
93 /**
94 * Returns the identifier of the application that requested the intent.
95 *
96 * @return application identifier
97 */
98 public ApplicationId appId() {
99 return appId;
100 }
101
102 /**
103 * Returns the collection of resources required for this intent.
104 *
105 * @return collection of resources; may be null
106 */
107 public Collection<NetworkResource> resources() {
108 return resources;
109 }
110
111 /**
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700112 * Indicates whether or not the intent is installable.
113 *
114 * @return true if installable
115 */
116 public boolean isInstallable() {
117 return false;
118 }
119
120 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700121 public final int hashCode() {
Brian O'Connor520c0522014-11-23 23:50:47 -0800122 return id.hashCode();
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700123 }
124
125 @Override
Thomas Vachuska46c07ad2014-10-21 16:01:01 -0700126 public final boolean equals(Object obj) {
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700127 if (this == obj) {
128 return true;
129 }
130 if (obj == null || getClass() != obj.getClass()) {
131 return false;
132 }
133 final Intent other = (Intent) obj;
Sho SHIMIZU23895232014-11-25 15:59:50 -0800134 return this.id().equals(other.id());
Thomas Vachuskac96058a2014-10-20 23:00:16 -0700135 }
136
Brian O'Connor520c0522014-11-23 23:50:47 -0800137 /**
138 * Binds an id generator for unique intent id generation.
139 *
140 * Note: A generator cannot be bound if there is already a generator bound.
141 * @param newIdGenerator id generator
142 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800143 public static void bindIdGenerator(IdGenerator newIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800144 checkState(idGenerator == null, "Id generator is already bound.");
145 idGenerator = checkNotNull(newIdGenerator);
146 }
147
148 /**
149 * Unbinds an id generator.
150 *
151 * Note: The caller must provide the old id generator to succeed.
152 * @param oldIdGenerator the current id generator
153 */
Sho SHIMIZUf57a7392014-11-25 16:11:32 -0800154 public static void unbindIdGenerator(IdGenerator oldIdGenerator) {
Brian O'Connor520c0522014-11-23 23:50:47 -0800155 if (Objects.equals(idGenerator, oldIdGenerator)) {
156 idGenerator = null;
157 }
158 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700159}