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