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