Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 19 | package org.onlab.onos.net.intent; |
| 20 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame^] | 21 | import org.onlab.onos.core.ApplicationId; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 22 | import org.onlab.onos.net.NetworkResource; |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 23 | import org.onlab.onos.net.flow.BatchOperationTarget; |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.Collection; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Abstraction of an application level intent. |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 32 | * <p/> |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 33 | * Make sure that an Intent should be immutable when a new type is defined. |
| 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 | |
| 41 | /** |
| 42 | * Constructor for serializer. |
| 43 | */ |
| 44 | protected Intent() { |
| 45 | this.id = null; |
| 46 | this.appId = null; |
| 47 | this.resources = null; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates a new intent. |
| 52 | * |
| 53 | * @param id intent identifier |
| 54 | * @param appId application identifier |
| 55 | * @param resources required network resources (optional) |
| 56 | */ |
| 57 | protected Intent(IntentId id, ApplicationId appId, |
| 58 | Collection<NetworkResource> resources) { |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 59 | this.id = checkNotNull(id, "Intent ID cannot be null"); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 60 | this.appId = checkNotNull(appId, "Application ID cannot be null"); |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 61 | this.resources = resources; |
| 62 | } |
| 63 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 64 | /** |
| 65 | * Returns the intent identifier. |
| 66 | * |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 67 | * @return intent fingerprint |
| 68 | */ |
| 69 | public IntentId id() { |
| 70 | return id; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the identifier of the application that requested the intent. |
| 75 | * |
| 76 | * @return application identifier |
| 77 | */ |
| 78 | public ApplicationId appId() { |
| 79 | return appId; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the collection of resources required for this intent. |
| 84 | * |
| 85 | * @return collection of resources; may be null |
| 86 | */ |
| 87 | public Collection<NetworkResource> resources() { |
| 88 | return resources; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Produces an intent identifier backed by hash-like fingerprint for the |
| 93 | * specified class of intent and its constituent fields. |
| 94 | * |
| 95 | * @param fields intent fields |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 96 | * @return intent identifier |
| 97 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 98 | protected static IntentId id(Object... fields) { |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 99 | // FIXME: spread the bits across the full long spectrum |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 100 | return IntentId.valueOf(Objects.hash(fields)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Indicates whether or not the intent is installable. |
| 105 | * |
| 106 | * @return true if installable |
| 107 | */ |
| 108 | public boolean isInstallable() { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 113 | public final int hashCode() { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 114 | return Objects.hash(id); |
| 115 | } |
| 116 | |
| 117 | @Override |
Thomas Vachuska | 46c07ad | 2014-10-21 16:01:01 -0700 | [diff] [blame] | 118 | public final boolean equals(Object obj) { |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 119 | if (this == obj) { |
| 120 | return true; |
| 121 | } |
| 122 | if (obj == null || getClass() != obj.getClass()) { |
| 123 | return false; |
| 124 | } |
| 125 | final Intent other = (Intent) obj; |
| 126 | return Objects.equals(this.id, other.id); |
| 127 | } |
| 128 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 129 | } |