blob: 1bfa0dc8b163b4937605e59f607d8f5ccea43dc6 [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
26/**
27 * A wrapper class that contains an intents, its state, and other metadata for
28 * internal use.
29 */
30public 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'Connor5eb77c82015-03-02 18:09:39 -080036 private NodeId origin;
Brian O'Connorcff03322015-02-03 15:28:59 -080037
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 Milkey5b3717e2015-02-05 11:44:08 -080058 public Key key() {
Brian O'Connorcff03322015-02-03 15:28:59 -080059 return intent.key();
60 }
61
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080062 public Timestamp version() {
63 return version;
64 }
65
Brian O'Connor5eb77c82015-03-02 18:09:39 -080066 /**
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'Connorcff03322015-02-03 15:28:59 -080079 public void setState(IntentState newState) {
80 this.state = newState;
81 }
82
Brian O'Connor2ba63fd2015-02-09 22:48:11 -080083 /**
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'Connorcff03322015-02-03 15:28:59 -080097 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 Milkey65cb59a2015-02-10 15:18:26 -0800122
123 public String toString() {
124 return MoreObjects.toStringHelper(getClass())
125 .add("key", key())
126 .add("state", state())
127 .add("version", version())
Ray Milkey9f74c082015-02-11 15:40:16 -0800128 .add("intent", intent())
Jonathan Hartd0ba2172015-02-11 13:54:33 -0800129 .add("installables", installables())
Ray Milkey65cb59a2015-02-10 15:18:26 -0800130 .toString();
131 }
132
Brian O'Connorcff03322015-02-03 15:28:59 -0800133}