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; |
| 21 | import org.onosproject.net.flow.BatchOperationTarget; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 22 | |
| 23 | import java.util.Collection; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 28 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Abstraction of an application level intent. |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 31 | * <p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 32 | * 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] | 33 | * </p> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 35 | public abstract class Intent implements BatchOperationTarget { |
| 36 | |
| 37 | private final IntentId id; |
| 38 | private final ApplicationId appId; |
| 39 | private final Collection<NetworkResource> resources; |
| 40 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 41 | private static IdGenerator idGenerator; |
| 42 | |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Constructor for serializer. |
| 45 | */ |
| 46 | protected Intent() { |
| 47 | this.id = null; |
| 48 | this.appId = null; |
| 49 | this.resources = null; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Creates a new intent. |
| 54 | * |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 55 | * @param appId application identifier |
| 56 | * @param resources required network resources (optional) |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 57 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 58 | protected Intent(ApplicationId appId, |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 59 | Collection<NetworkResource> resources) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 60 | checkState(idGenerator != null, "Id generator is not bound."); |
| 61 | this.id = IntentId.valueOf(idGenerator.getNewId()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 62 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 63 | this.resources = resources; |
| 64 | } |
| 65 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Returns the intent identifier. |
| 68 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 69 | * @return intent fingerprint |
| 70 | */ |
| 71 | public IntentId id() { |
| 72 | return id; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the identifier of the application that requested the intent. |
| 77 | * |
| 78 | * @return application identifier |
| 79 | */ |
| 80 | public ApplicationId appId() { |
| 81 | return appId; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Returns the collection of resources required for this intent. |
| 86 | * |
| 87 | * @return collection of resources; may be null |
| 88 | */ |
| 89 | public Collection<NetworkResource> resources() { |
| 90 | return resources; |
| 91 | } |
| 92 | |
| 93 | /** |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 94 | * Indicates whether or not the intent is installable. |
| 95 | * |
| 96 | * @return true if installable |
| 97 | */ |
| 98 | public boolean isInstallable() { |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 103 | public final int hashCode() { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 104 | return id.hashCode(); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 108 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 109 | if (this == obj) { |
| 110 | return true; |
| 111 | } |
| 112 | if (obj == null || getClass() != obj.getClass()) { |
| 113 | return false; |
| 114 | } |
| 115 | final Intent other = (Intent) obj; |
Sho SHIMIZU | 2389523 | 2014-11-25 15:59:50 -0800 | [diff] [blame] | 116 | return this.id().equals(other.id()); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Binds an id generator for unique intent id generation. |
| 121 | * |
| 122 | * Note: A generator cannot be bound if there is already a generator bound. |
| 123 | * @param newIdGenerator id generator |
| 124 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 125 | public static void bindIdGenerator(IdGenerator newIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 126 | checkState(idGenerator == null, "Id generator is already bound."); |
| 127 | idGenerator = checkNotNull(newIdGenerator); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Unbinds an id generator. |
| 132 | * |
| 133 | * Note: The caller must provide the old id generator to succeed. |
| 134 | * @param oldIdGenerator the current id generator |
| 135 | */ |
Sho SHIMIZU | f57a739 | 2014-11-25 16:11:32 -0800 | [diff] [blame] | 136 | public static void unbindIdGenerator(IdGenerator oldIdGenerator) { |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 137 | if (Objects.equals(idGenerator, oldIdGenerator)) { |
| 138 | idGenerator = null; |
| 139 | } |
| 140 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 141 | } |