Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.core.ApplicationId; |
| 19 | import org.onosproject.core.IdGenerator; |
| 20 | import org.onosproject.net.NetworkResource; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 26 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 27 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Abstraction of an application level intent. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 30 | * <p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 31 | * Make sure that an Intent should be immutable when a new type is defined. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 32 | * </p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 33 | */ |
Sho SHIMIZU | 5e5d4aa | 2015-01-26 16:12:11 -0800 | [diff] [blame] | 34 | public abstract class Intent { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 35 | |
| 36 | private final IntentId id; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 37 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 38 | private final ApplicationId appId; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 39 | private final String key; |
| 40 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 41 | private final Collection<NetworkResource> resources; |
| 42 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 43 | private static IdGenerator idGenerator; |
| 44 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Constructor for serializer. |
| 47 | */ |
| 48 | protected Intent() { |
| 49 | this.id = null; |
| 50 | this.appId = null; |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 51 | this.key = null; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 52 | this.resources = null; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Creates a new intent. |
| 57 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 58 | * @param appId application identifier |
| 59 | * @param resources required network resources (optional) |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 60 | */ |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 61 | @Deprecated |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 62 | protected Intent(ApplicationId appId, |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 63 | Collection<NetworkResource> resources) { |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 64 | 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'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 77 | checkState(idGenerator != null, "Id generator is not bound."); |
| 78 | this.id = IntentId.valueOf(idGenerator.getNewId()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 79 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 80 | this.key = (key != null) ? key : id.toString(); //FIXME |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 81 | this.resources = checkNotNull(resources); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Returns the intent identifier. |
| 86 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 87 | * @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 Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 112 | * 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 Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 121 | public final int hashCode() { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 122 | return id.hashCode(); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 126 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 127 | 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 SHIMIZU | 2389523 | 2014-11-25 15:59:50 -0800 | [diff] [blame] | 134 | return this.id().equals(other.id()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 137 | /** |
| 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 SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 143 | public static void bindIdGenerator(IdGenerator newIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 144 | 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 SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 154 | public static void unbindIdGenerator(IdGenerator oldIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 155 | if (Objects.equals(idGenerator, oldIdGenerator)) { |
| 156 | idGenerator = null; |
| 157 | } |
| 158 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 159 | } |