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 | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent; |
| 17 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 18 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 19 | import org.onlab.onos.net.NetworkResource; |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 20 | import org.onlab.onos.net.flow.BatchOperationTarget; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 21 | |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 22 | import java.util.Arrays; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 23 | import java.util.Collection; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.Preconditions.checkNotNull; |
| 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 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 34 | public abstract class Intent implements BatchOperationTarget { |
| 35 | |
| 36 | private final IntentId id; |
| 37 | private final ApplicationId appId; |
| 38 | private final Collection<NetworkResource> resources; |
| 39 | |
| 40 | /** |
| 41 | * Constructor for serializer. |
| 42 | */ |
| 43 | protected Intent() { |
| 44 | this.id = null; |
| 45 | this.appId = null; |
| 46 | this.resources = null; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Creates a new intent. |
| 51 | * |
| 52 | * @param id intent identifier |
| 53 | * @param appId application identifier |
| 54 | * @param resources required network resources (optional) |
| 55 | */ |
| 56 | protected Intent(IntentId id, ApplicationId appId, |
| 57 | Collection<NetworkResource> resources) { |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 58 | this.id = checkNotNull(id, "Intent ID cannot be null"); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 59 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 60 | this.resources = resources; |
| 61 | } |
| 62 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Returns the intent identifier. |
| 65 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 66 | * @return intent fingerprint |
| 67 | */ |
| 68 | public IntentId id() { |
| 69 | return id; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the identifier of the application that requested the intent. |
| 74 | * |
| 75 | * @return application identifier |
| 76 | */ |
| 77 | public ApplicationId appId() { |
| 78 | return appId; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the collection of resources required for this intent. |
| 83 | * |
| 84 | * @return collection of resources; may be null |
| 85 | */ |
| 86 | public Collection<NetworkResource> resources() { |
| 87 | return resources; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Produces an intent identifier backed by hash-like fingerprint for the |
| 92 | * specified class of intent and its constituent fields. |
| 93 | * |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 94 | * @param intentClass Class of the intent |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 95 | * @param fields intent fields |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 96 | * @return intent identifier |
| 97 | */ |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 98 | protected static IntentId id(Class<?> intentClass, Object... fields) { |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 99 | // FIXME: spread the bits across the full long spectrum |
Brian O'Connor | 41718fc | 2014-10-30 16:57:21 -0700 | [diff] [blame] | 100 | return IntentId.valueOf(Objects.hash(intentClass.getName(), |
| 101 | Arrays.hashCode(fields))); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Indicates whether or not the intent is installable. |
| 106 | * |
| 107 | * @return true if installable |
| 108 | */ |
| 109 | public boolean isInstallable() { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 114 | public final int hashCode() { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 115 | return Objects.hash(id); |
| 116 | } |
| 117 | |
| 118 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 119 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 120 | if (this == obj) { |
| 121 | return true; |
| 122 | } |
| 123 | if (obj == null || getClass() != obj.getClass()) { |
| 124 | return false; |
| 125 | } |
| 126 | final Intent other = (Intent) obj; |
| 127 | return Objects.equals(this.id, other.id); |
| 128 | } |
| 129 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 130 | } |