Thomas Vachuska

Enhanced toString for intents for consistency.

Removed ID block allocator stuff.
......@@ -18,7 +18,8 @@ public class IntentsListCommand extends AbstractShellCommand {
IntentService service = get(IntentService.class);
for (Intent intent : service.getIntents()) {
IntentState state = service.getIntentState(intent.id());
print("%s %s %s", intent.id(), state, intent);
print("id=%s, state=%s, appId=%s, %s",
intent.id(), state, intent.appId().name(), intent);
}
}
......
......@@ -73,7 +73,12 @@ public abstract class ConnectivityIntent extends Intent {
return treatment;
}
/**
* Produces a collection of network resources from the given links.
*
* @param links collection of links
* @return collection of link resources
*/
protected static Collection<NetworkResource> resources(Collection<Link> links) {
return ImmutableSet.<NetworkResource>copyOf(links);
}
......
......@@ -66,6 +66,7 @@ public final class HostToHostIntent extends ConnectivityIntent {
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("appId", appId())
.add("selector", selector())
.add("treatment", treatment())
.add("one", one)
......
package org.onlab.onos.net.intent;
/**
* A generalized interface for ID generation
* <p/>
* {@link #getNewId()} generates a globally unique ID instance on
* each invocation.
*
* @param <T> the type of ID
*/
@Deprecated
public interface IdGenerator<T> {
/**
* Returns a globally unique ID instance.
*
* @return globally unique ID instance
*/
T getNewId();
}
......@@ -78,8 +78,9 @@ public final class LinkCollectionIntent extends ConnectivityIntent {
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("match", selector())
.add("action", treatment())
.add("appId", appId())
.add("selector", selector())
.add("treatment", treatment())
.add("links", links())
.add("egress", egressPoint())
.toString();
......
......@@ -81,10 +81,11 @@ public final class MultiPointToSinglePointIntent extends ConnectivityIntent {
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("match", selector())
.add("action", treatment())
.add("ingressPoints", ingressPoints())
.add("egressPoint", egressPoint())
.add("appId", appId())
.add("selector", selector())
.add("treatment", treatment())
.add("ingress", ingressPoints())
.add("egress", egressPoint())
.toString();
}
}
......
......@@ -56,8 +56,9 @@ public class PathIntent extends ConnectivityIntent {
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("match", selector())
.add("action", treatment())
.add("appId", appId())
.add("selector", selector())
.add("treatment", treatment())
.add("path", path)
.toString();
}
......
......@@ -69,10 +69,11 @@ public class PointToPointIntent extends ConnectivityIntent {
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("match", selector())
.add("action", treatment())
.add("ingressPoint", ingressPoint)
.add("egressPoints", egressPoint)
.add("appId", appId())
.add("selector", selector())
.add("treatment", treatment())
.add("ingress", ingressPoint)
.add("egress", egressPoint)
.toString();
}
......
......@@ -77,10 +77,11 @@ public class SinglePointToMultiPointIntent extends ConnectivityIntent {
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("id", id())
.add("match", selector())
.add("action", treatment())
.add("ingressPoint", ingressPoint)
.add("egressPort", egressPoints)
.add("appId", appId())
.add("selector", selector())
.add("treatment", treatment())
.add("ingress", ingressPoint)
.add("egress", egressPoints)
.toString();
}
......
package org.onlab.onos.net.intent;
/**
* This interface is for generator of IntentId. It is defined only for
* testing purpose to keep type safety on mock creation.
*
* <p>
* {@link #getNewId()} generates a globally unique {@link IntentId} instance
* on each invocation. Application developers should not generate IntentId
* by themselves. Instead use an implementation of this interface.
* </p>
*/
public interface IntentIdGenerator extends IdGenerator<IntentId> {
}
package org.onlab.onos.net.intent.impl;
import org.onlab.onos.net.intent.IdGenerator;
/**
* Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as
* backend.
*
* @param <T> the type of ID
*/
public abstract class AbstractBlockAllocatorBasedIdGenerator<T> implements IdGenerator<T> {
protected final IdBlockAllocator allocator;
protected IdBlock idBlock;
/**
* Constructs an ID generator which use {@link IdBlockAllocator} as backend.
*
* @param allocator
*/
protected AbstractBlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
this.allocator = allocator;
this.idBlock = allocator.allocateUniqueIdBlock();
}
@Override
public synchronized T getNewId() {
try {
return convertFrom(idBlock.getNextId());
} catch (UnavailableIdException e) {
idBlock = allocator.allocateUniqueIdBlock();
return convertFrom(idBlock.getNextId());
}
}
/**
* Returns an ID instance of {@code T} type from the long value.
*
* @param value original long value
* @return ID instance
*/
protected abstract T convertFrom(long value);
}
package org.onlab.onos.net.intent.impl;
public class DummyIdBlockAllocator implements IdBlockAllocator {
private long blockTop;
private static final long BLOCK_SIZE = 0x1000000L;
/**
* Returns a block of IDs which are unique and unused.
* Range of IDs is fixed size and is assigned incrementally as this method
* called.
*
* @return an IdBlock containing a set of unique IDs
*/
@Override
public IdBlock allocateUniqueIdBlock() {
synchronized (this) {
long blockHead = blockTop;
long blockTail = blockTop + BLOCK_SIZE;
IdBlock block = new IdBlock(blockHead, BLOCK_SIZE);
blockTop = blockTail;
return block;
}
}
@Override
public IdBlock allocateUniqueIdBlock(long range) {
throw new UnsupportedOperationException("Not supported yet");
}
}
package org.onlab.onos.net.intent.impl;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import com.google.common.base.MoreObjects;
/**
* A class representing an ID space.
*/
public final class IdBlock {
private final long start;
private final long size;
private final AtomicLong currentId;
/**
* Constructs a new ID block with the specified size and initial value.
*
* @param start initial value of the block
* @param size size of the block
* @throws IllegalArgumentException if the size is less than or equal to 0
*/
public IdBlock(long start, long size) {
checkArgument(size > 0, "size should be more than 0, but %s", size);
this.start = start;
this.size = size;
this.currentId = new AtomicLong(start);
}
// TODO: consider if this method is needed or not
/**
* Returns the initial value.
*
* @return initial value
*/
public long getStart() {
return start;
}
// TODO: consider if this method is needed or not
/**
* Returns the last value.
*
* @return last value
*/
public long getEnd() {
return start + size - 1;
}
/**
* Returns the block size.
*
* @return block size
*/
public long getSize() {
return size;
}
/**
* Returns the next ID in the block.
*
* @return next ID
* @throws UnavailableIdException if there is no available ID in the block.
*/
public long getNextId() {
final long id = currentId.getAndIncrement();
if (id > getEnd()) {
throw new UnavailableIdException(String.format(
"used all IDs in allocated space (size: %d, end: %d, current: %d)",
size, getEnd(), id
));
}
return id;
}
// TODO: Do we really need equals and hashCode? Should it contain currentId?
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
IdBlock that = (IdBlock) o;
return Objects.equals(this.start, that.start)
&& Objects.equals(this.size, that.size)
&& Objects.equals(this.currentId.get(), that.currentId.get());
}
@Override
public int hashCode() {
return Objects.hash(start, size, currentId);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("start", start)
.add("size", size)
.add("currentId", currentId)
.toString();
}
}
package org.onlab.onos.net.intent.impl;
/**
* An interface that gives unique ID spaces.
*/
public interface IdBlockAllocator {
/**
* Allocates a unique Id Block.
*
* @return Id Block.
*/
IdBlock allocateUniqueIdBlock();
/**
* Allocates next unique id and retrieve a new range of ids if needed.
*
* @param range range to use for the identifier
* @return Id Block.
*/
IdBlock allocateUniqueIdBlock(long range);
}