Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.net.intent; |
| 17 | |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.onosproject.store.Timestamp; |
| 21 | |
| 22 | import java.util.List; |
| 23 | import java.util.Objects; |
| 24 | |
| 25 | /** |
| 26 | * A wrapper class that contains an intents, its state, and other metadata for |
| 27 | * internal use. |
| 28 | */ |
| 29 | public class IntentData { //FIXME need to make this "immutable" |
| 30 | // manager should be able to mutate a local copy while processing |
| 31 | private Intent intent; |
| 32 | |
| 33 | private IntentState state; |
| 34 | private Timestamp version; |
| 35 | |
| 36 | private List<Intent> installables; |
| 37 | |
| 38 | public IntentData(Intent intent, IntentState state, Timestamp version) { |
| 39 | this.intent = intent; |
| 40 | this.state = state; |
| 41 | this.version = version; |
| 42 | } |
| 43 | |
| 44 | // kryo constructor |
| 45 | protected IntentData() { |
| 46 | } |
| 47 | |
| 48 | public Intent intent() { |
| 49 | return intent; |
| 50 | } |
| 51 | |
| 52 | public IntentState state() { |
| 53 | return state; |
| 54 | } |
| 55 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 56 | public Key key() { |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 57 | return intent.key(); |
| 58 | } |
| 59 | |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 60 | public Timestamp version() { |
| 61 | return version; |
| 62 | } |
| 63 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 64 | public void setState(IntentState newState) { |
| 65 | this.state = newState; |
| 66 | } |
| 67 | |
Brian O'Connor | 2ba63fd | 2015-02-09 22:48:11 -0800 | [diff] [blame] | 68 | /** |
| 69 | * Sets the version for this intent data. |
| 70 | * <p> |
| 71 | * The store should call this method only once when the IntentData is |
| 72 | * first passed into the pending map. Ideally, an IntentData is timestamped |
| 73 | * on the same thread that the called used to submit the intents. |
| 74 | * </p> |
| 75 | * |
| 76 | * @param version the version/timestamp for this intent data |
| 77 | */ |
| 78 | public void setVersion(Timestamp version) { |
| 79 | this.version = version; |
| 80 | } |
| 81 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 82 | public void setInstallables(List<Intent> installables) { |
| 83 | this.installables = ImmutableList.copyOf(installables); |
| 84 | } |
| 85 | |
| 86 | public List<Intent> installables() { |
| 87 | return installables; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int hashCode() { |
| 92 | return Objects.hash(intent, version); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (this == obj) { |
| 98 | return true; |
| 99 | } |
| 100 | if (obj == null || getClass() != obj.getClass()) { |
| 101 | return false; |
| 102 | } |
| 103 | final IntentData other = (IntentData) obj; |
| 104 | return Objects.equals(this.intent, other.intent) |
| 105 | && Objects.equals(this.version, other.version); |
| 106 | } |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 107 | |
| 108 | public String toString() { |
| 109 | return MoreObjects.toStringHelper(getClass()) |
| 110 | .add("key", key()) |
| 111 | .add("state", state()) |
| 112 | .add("version", version()) |
Ray Milkey | 9f74c08 | 2015-02-11 15:40:16 -0800 | [diff] [blame] | 113 | .add("intent", intent()) |
Jonathan Hart | d0ba217 | 2015-02-11 13:54:33 -0800 | [diff] [blame] | 114 | .add("installables", installables()) |
Ray Milkey | 65cb59a | 2015-02-10 15:18:26 -0800 | [diff] [blame] | 115 | .toString(); |
| 116 | } |
| 117 | |
Brian O'Connor | cff0332 | 2015-02-03 15:28:59 -0800 | [diff] [blame] | 118 | } |