blob: a71b20eb7aa6a77924e3e59294c55af5f53616dd [file] [log] [blame]
Brian O'Connorcff03322015-02-03 15:28:59 -08001/*
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 */
16package org.onosproject.net.intent;
17
Ray Milkey65cb59a2015-02-10 15:18:26 -080018import com.google.common.base.MoreObjects;
Brian O'Connorcff03322015-02-03 15:28:59 -080019import com.google.common.collect.ImmutableList;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080020import org.onosproject.cluster.NodeId;
Brian O'Connorcff03322015-02-03 15:28:59 -080021import org.onosproject.store.Timestamp;
22
23import java.util.List;
24import java.util.Objects;
25
Jonathan Hart0d18df32015-03-21 08:42:59 -070026import static com.google.common.base.Preconditions.checkNotNull;
27
Brian O'Connorcff03322015-02-03 15:28:59 -080028/**
29 * A wrapper class that contains an intents, its state, and other metadata for
30 * internal use.
31 */
32public class IntentData { //FIXME need to make this "immutable"
33 // manager should be able to mutate a local copy while processing
Jonathan Hart0d18df32015-03-21 08:42:59 -070034 private final Intent intent;
Brian O'Connorcff03322015-02-03 15:28:59 -080035
36 private IntentState state;
37 private Timestamp version;
Brian O'Connor5eb77c82015-03-02 18:09:39 -080038 private NodeId origin;
Brian O'Connorcff03322015-02-03 15:28:59 -080039
40 private List<Intent> installables;
41
Jonathan Hart0d18df32015-03-21 08:42:59 -070042 /**
43 * Creates a new intent data object.
44 *
45 * @param intent intent this metadata references
46 * @param state intent state
47 * @param version version of the intent for this key
48 */
Brian O'Connorcff03322015-02-03 15:28:59 -080049 public IntentData(Intent intent, IntentState state, Timestamp version) {
50 this.intent = intent;
51 this.state = state;
52 this.version = version;
53 }
54
Jonathan Hart0d18df32015-03-21 08:42:59 -070055 /**
56 * Copy constructor.
57 *
58 * @param intentData intent data to copy
59 */
60 public IntentData(IntentData intentData) {
61 checkNotNull(intentData);
62
63 intent = intentData.intent;
64 state = intentData.state;
65 version = intentData.version;
66 origin = intentData.origin;
67 installables = intentData.installables;
Brian O'Connorcff03322015-02-03 15:28:59 -080068 }
69
Jonathan Hart0d18df32015-03-21 08:42:59 -070070 // kryo constructor
71 protected IntentData() {
72 intent = null;
73 }
74
75 /**
76 * Returns the intent this metadata references.
77 *
78 * @return intent
79 */
Brian O'Connorcff03322015-02-03 15:28:59 -080080 public Intent intent() {
81 return intent;
82 }
83
Jonathan Hart0d18df32015-03-21 08:42:59 -070084 /**
85 * Returns the state of the intent.
86 *
87 * @return intent state
88 */
Brian O'Connorcff03322015-02-03 15:28:59 -080089 public IntentState state() {
90 return state;
91 }
92
Jonathan Hart0d18df32015-03-21 08:42:59 -070093 /**
94 * Returns the intent key.
95 *
96 * @return intent key
97 */
Ray Milkey5b3717e2015-02-05 11:44:08 -080098 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -080099 return intent.key();
100 }
101
Jonathan Hart0d18df32015-03-21 08:42:59 -0700102 /**
103 * Returns the version of the intent for this key.
104 *
105 * @return intent version
106 */
Brian O'Connor2ba63fd2015-02-09 22:48:11 -0800107 public Timestamp version() {
108 return version;
109 }
110
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800111 /**
Jonathan Hart0d18df32015-03-21 08:42:59 -0700112 * Sets the origin, which is the node that created the intent.
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800113 *
114 * @param origin origin instance
115 */
116 public void setOrigin(NodeId origin) {
117 this.origin = origin;
118 }
119
Jonathan Hart0d18df32015-03-21 08:42:59 -0700120 /**
121 * Returns the origin node that created this intent.
122 *
123 * @return origin node ID
124 */
Brian O'Connor5eb77c82015-03-02 18:09:39 -0800125 public NodeId origin() {
126 return origin;
127 }
128
Jonathan Hart0d18df32015-03-21 08:42:59 -0700129 /**
130 * Updates the state of the intent to the given new state.
131 *
132 * @param newState new state of the intent
133 */
Brian O'Connorcff03322015-02-03 15:28:59 -0800134 public void setState(IntentState newState) {
135 this.state = newState;
136 }
137
Brian O'Connor2ba63fd2015-02-09 22:48:11 -0800138 /**
139 * Sets the version for this intent data.
140 * <p>
141 * The store should call this method only once when the IntentData is
142 * first passed into the pending map. Ideally, an IntentData is timestamped
143 * on the same thread that the called used to submit the intents.
144 * </p>
145 *
146 * @param version the version/timestamp for this intent data
147 */
148 public void setVersion(Timestamp version) {
149 this.version = version;
150 }
151
Jonathan Hart0d18df32015-03-21 08:42:59 -0700152 /**
153 * Sets the intent installables to the given list of intents.
154 *
155 * @param installables list of installables for this intent
156 */
Brian O'Connorcff03322015-02-03 15:28:59 -0800157 public void setInstallables(List<Intent> installables) {
158 this.installables = ImmutableList.copyOf(installables);
159 }
160
Jonathan Hart0d18df32015-03-21 08:42:59 -0700161 /**
162 * Returns the installables associated with this intent.
163 *
164 * @return list of installable intents
165 */
Brian O'Connorcff03322015-02-03 15:28:59 -0800166 public List<Intent> installables() {
167 return installables;
168 }
169
170 @Override
171 public int hashCode() {
172 return Objects.hash(intent, version);
173 }
174
175 @Override
176 public boolean equals(Object obj) {
177 if (this == obj) {
178 return true;
179 }
180 if (obj == null || getClass() != obj.getClass()) {
181 return false;
182 }
183 final IntentData other = (IntentData) obj;
184 return Objects.equals(this.intent, other.intent)
185 && Objects.equals(this.version, other.version);
186 }
Ray Milkey65cb59a2015-02-10 15:18:26 -0800187
Jonathan Hart0d18df32015-03-21 08:42:59 -0700188 @Override
Ray Milkey65cb59a2015-02-10 15:18:26 -0800189 public String toString() {
190 return MoreObjects.toStringHelper(getClass())
191 .add("key", key())
192 .add("state", state())
193 .add("version", version())
Ray Milkey9f74c082015-02-11 15:40:16 -0800194 .add("intent", intent())
Jonathan Hart0d18df32015-03-21 08:42:59 -0700195 .add("origin", origin())
Jonathan Hartd0ba2172015-02-11 13:54:33 -0800196 .add("installables", installables())
Ray Milkey65cb59a2015-02-10 15:18:26 -0800197 .toString();
198 }
199
Brian O'Connorcff03322015-02-03 15:28:59 -0800200}