Jonathan Hart
Committed by Ray Milkey

Implement copyData as copy constructor on IntentData.

Cleaned up javadocs. 

Change-Id: I90e6350244991d4f30180fe501fec9e6fd180d43
......@@ -23,13 +23,15 @@ import org.onosproject.store.Timestamp;
import java.util.List;
import java.util.Objects;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A wrapper class that contains an intents, its state, and other metadata for
* internal use.
*/
public class IntentData { //FIXME need to make this "immutable"
// manager should be able to mutate a local copy while processing
private Intent intent;
private final Intent intent;
private IntentState state;
private Timestamp version;
......@@ -37,34 +39,77 @@ public class IntentData { //FIXME need to make this "immutable"
private List<Intent> installables;
/**
* Creates a new intent data object.
*
* @param intent intent this metadata references
* @param state intent state
* @param version version of the intent for this key
*/
public IntentData(Intent intent, IntentState state, Timestamp version) {
this.intent = intent;
this.state = state;
this.version = version;
}
/**
* Copy constructor.
*
* @param intentData intent data to copy
*/
public IntentData(IntentData intentData) {
checkNotNull(intentData);
intent = intentData.intent;
state = intentData.state;
version = intentData.version;
origin = intentData.origin;
installables = intentData.installables;
}
// kryo constructor
protected IntentData() {
intent = null;
}
/**
* Returns the intent this metadata references.
*
* @return intent
*/
public Intent intent() {
return intent;
}
/**
* Returns the state of the intent.
*
* @return intent state
*/
public IntentState state() {
return state;
}
/**
* Returns the intent key.
*
* @return intent key
*/
public Key key() {
return intent.key();
}
/**
* Returns the version of the intent for this key.
*
* @return intent version
*/
public Timestamp version() {
return version;
}
/**
* Sets the origin, which is the node that created the instance.
* Sets the origin, which is the node that created the intent.
*
* @param origin origin instance
*/
......@@ -72,10 +117,20 @@ public class IntentData { //FIXME need to make this "immutable"
this.origin = origin;
}
/**
* Returns the origin node that created this intent.
*
* @return origin node ID
*/
public NodeId origin() {
return origin;
}
/**
* Updates the state of the intent to the given new state.
*
* @param newState new state of the intent
*/
public void setState(IntentState newState) {
this.state = newState;
}
......@@ -94,10 +149,20 @@ public class IntentData { //FIXME need to make this "immutable"
this.version = version;
}
/**
* Sets the intent installables to the given list of intents.
*
* @param installables list of installables for this intent
*/
public void setInstallables(List<Intent> installables) {
this.installables = ImmutableList.copyOf(installables);
}
/**
* Returns the installables associated with this intent.
*
* @return list of installable intents
*/
public List<Intent> installables() {
return installables;
}
......@@ -120,12 +185,14 @@ public class IntentData { //FIXME need to make this "immutable"
&& Objects.equals(this.version, other.version);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("key", key())
.add("state", state())
.add("version", version())
.add("intent", intent())
.add("origin", origin())
.add("installables", installables())
.toString();
}
......
......@@ -51,6 +51,7 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.intent.IntentState.*;
import static org.slf4j.LoggerFactory.getLogger;
......@@ -149,20 +150,6 @@ public class GossipIntentStore
return null;
}
private IntentData copyData(IntentData original) {
if (original == null) {
return null;
}
IntentData result =
new IntentData(original.intent(), original.state(), original.version());
if (original.installables() != null) {
result.setInstallables(original.installables());
}
result.setOrigin(original.origin());
return result;
}
/**
* Determines whether an intent data update is allowed. The update must
* either have a higher version than the current data, or the state
......@@ -239,6 +226,8 @@ public class GossipIntentStore
@Override
public void write(IntentData newData) {
checkNotNull(newData);
IntentData currentData = currentMap.get(newData.key());
if (isUpdateAcceptable(currentData, newData)) {
// Only the master is modifying the current state. Therefore assume
......@@ -246,13 +235,11 @@ public class GossipIntentStore
if (newData.state() == PURGE_REQ) {
currentMap.remove(newData.key(), newData);
} else {
currentMap.put(newData.key(), copyData(newData));
currentMap.put(newData.key(), new IntentData(newData));
}
// if current.put succeeded
pendingMap.remove(newData.key(), newData);
} else {
log.debug("not writing update: current {}, new {}", currentData, newData);
}
}
......@@ -307,16 +294,22 @@ public class GossipIntentStore
@Override
public IntentData getIntentData(Key key) {
return copyData(currentMap.get(key));
IntentData current = currentMap.get(key);
if (current == null) {
return null;
}
return new IntentData(current);
}
@Override
public void addPending(IntentData data) {
checkNotNull(data);
if (data.version() == null) {
data.setVersion(new WallClockTimestamp());
}
data.setOrigin(clusterService.getLocalNode().id());
pendingMap.put(data.key(), copyData(data));
pendingMap.put(data.key(), new IntentData(data));
}
@Override
......@@ -361,8 +354,7 @@ public class GossipIntentStore
// some work.
if (isMaster(event.value().intent().key())) {
if (delegate != null) {
log.debug("processing {}", event.key());
delegate.process(copyData(event.value()));
delegate.process(new IntentData(event.value()));
}
}
......
......@@ -38,6 +38,9 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.intent.IntentState.*;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Simple single-instance implementation of the intent store.
*/
@Component(immediate = true)
@Service
public class SimpleIntentStore
......@@ -49,19 +52,6 @@ public class SimpleIntentStore
private final Map<Key, IntentData> current = Maps.newConcurrentMap();
private final Map<Key, IntentData> pending = Maps.newConcurrentMap();
private IntentData copyData(IntentData original) {
if (original == null) {
return null;
}
IntentData result =
new IntentData(original.intent(), original.state(), original.version());
if (original.installables() != null) {
result.setInstallables(original.installables());
}
return result;
}
@Activate
public void activate() {
log.info("Started");
......@@ -99,7 +89,6 @@ public class SimpleIntentStore
return null;
}
/**
* Determines whether an intent data update is allowed. The update must
* either have a higher version than the current data, or the state
......@@ -174,6 +163,8 @@ public class SimpleIntentStore
@Override
public void write(IntentData newData) {
checkNotNull(newData);
synchronized (this) {
// TODO this could be refactored/cleaned up
IntentData currentData = current.get(newData.key());
......@@ -183,7 +174,7 @@ public class SimpleIntentStore
if (pendingData.state() == PURGE_REQ) {
current.remove(newData.key(), newData);
} else {
current.put(newData.key(), copyData(newData));
current.put(newData.key(), new IntentData(newData));
}
if (pendingData != null
......@@ -219,7 +210,11 @@ public class SimpleIntentStore
@Override
public IntentData getIntentData(Key key) {
return copyData(current.get(key));
IntentData currentData = current.get(key);
if (currentData == null) {
return null;
}
return new IntentData(currentData);
}
@Override
......