Enhanced toString for intents for consistency.
Removed ID block allocator stuff.
Showing
14 changed files
with
30 additions
and
256 deletions
| ... | @@ -18,7 +18,8 @@ public class IntentsListCommand extends AbstractShellCommand { | ... | @@ -18,7 +18,8 @@ public class IntentsListCommand extends AbstractShellCommand { |
| 18 | IntentService service = get(IntentService.class); | 18 | IntentService service = get(IntentService.class); |
| 19 | for (Intent intent : service.getIntents()) { | 19 | for (Intent intent : service.getIntents()) { |
| 20 | IntentState state = service.getIntentState(intent.id()); | 20 | IntentState state = service.getIntentState(intent.id()); |
| 21 | - print("%s %s %s", intent.id(), state, intent); | 21 | + print("id=%s, state=%s, appId=%s, %s", |
| 22 | + intent.id(), state, intent.appId().name(), intent); | ||
| 22 | } | 23 | } |
| 23 | } | 24 | } |
| 24 | 25 | ... | ... |
| ... | @@ -73,7 +73,12 @@ public abstract class ConnectivityIntent extends Intent { | ... | @@ -73,7 +73,12 @@ public abstract class ConnectivityIntent extends Intent { |
| 73 | return treatment; | 73 | return treatment; |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | - | 76 | + /** |
| 77 | + * Produces a collection of network resources from the given links. | ||
| 78 | + * | ||
| 79 | + * @param links collection of links | ||
| 80 | + * @return collection of link resources | ||
| 81 | + */ | ||
| 77 | protected static Collection<NetworkResource> resources(Collection<Link> links) { | 82 | protected static Collection<NetworkResource> resources(Collection<Link> links) { |
| 78 | return ImmutableSet.<NetworkResource>copyOf(links); | 83 | return ImmutableSet.<NetworkResource>copyOf(links); |
| 79 | } | 84 | } | ... | ... |
| ... | @@ -66,6 +66,7 @@ public final class HostToHostIntent extends ConnectivityIntent { | ... | @@ -66,6 +66,7 @@ public final class HostToHostIntent extends ConnectivityIntent { |
| 66 | public String toString() { | 66 | public String toString() { |
| 67 | return MoreObjects.toStringHelper(getClass()) | 67 | return MoreObjects.toStringHelper(getClass()) |
| 68 | .add("id", id()) | 68 | .add("id", id()) |
| 69 | + .add("appId", appId()) | ||
| 69 | .add("selector", selector()) | 70 | .add("selector", selector()) |
| 70 | .add("treatment", treatment()) | 71 | .add("treatment", treatment()) |
| 71 | .add("one", one) | 72 | .add("one", one) | ... | ... |
| 1 | -package org.onlab.onos.net.intent; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * A generalized interface for ID generation | ||
| 5 | - * <p/> | ||
| 6 | - * {@link #getNewId()} generates a globally unique ID instance on | ||
| 7 | - * each invocation. | ||
| 8 | - * | ||
| 9 | - * @param <T> the type of ID | ||
| 10 | - */ | ||
| 11 | -@Deprecated | ||
| 12 | -public interface IdGenerator<T> { | ||
| 13 | - /** | ||
| 14 | - * Returns a globally unique ID instance. | ||
| 15 | - * | ||
| 16 | - * @return globally unique ID instance | ||
| 17 | - */ | ||
| 18 | - T getNewId(); | ||
| 19 | -} |
| ... | @@ -78,8 +78,9 @@ public final class LinkCollectionIntent extends ConnectivityIntent { | ... | @@ -78,8 +78,9 @@ public final class LinkCollectionIntent extends ConnectivityIntent { |
| 78 | public String toString() { | 78 | public String toString() { |
| 79 | return MoreObjects.toStringHelper(getClass()) | 79 | return MoreObjects.toStringHelper(getClass()) |
| 80 | .add("id", id()) | 80 | .add("id", id()) |
| 81 | - .add("match", selector()) | 81 | + .add("appId", appId()) |
| 82 | - .add("action", treatment()) | 82 | + .add("selector", selector()) |
| 83 | + .add("treatment", treatment()) | ||
| 83 | .add("links", links()) | 84 | .add("links", links()) |
| 84 | .add("egress", egressPoint()) | 85 | .add("egress", egressPoint()) |
| 85 | .toString(); | 86 | .toString(); | ... | ... |
| ... | @@ -81,10 +81,11 @@ public final class MultiPointToSinglePointIntent extends ConnectivityIntent { | ... | @@ -81,10 +81,11 @@ public final class MultiPointToSinglePointIntent extends ConnectivityIntent { |
| 81 | public String toString() { | 81 | public String toString() { |
| 82 | return MoreObjects.toStringHelper(getClass()) | 82 | return MoreObjects.toStringHelper(getClass()) |
| 83 | .add("id", id()) | 83 | .add("id", id()) |
| 84 | - .add("match", selector()) | 84 | + .add("appId", appId()) |
| 85 | - .add("action", treatment()) | 85 | + .add("selector", selector()) |
| 86 | - .add("ingressPoints", ingressPoints()) | 86 | + .add("treatment", treatment()) |
| 87 | - .add("egressPoint", egressPoint()) | 87 | + .add("ingress", ingressPoints()) |
| 88 | + .add("egress", egressPoint()) | ||
| 88 | .toString(); | 89 | .toString(); |
| 89 | } | 90 | } |
| 90 | } | 91 | } | ... | ... |
| ... | @@ -56,8 +56,9 @@ public class PathIntent extends ConnectivityIntent { | ... | @@ -56,8 +56,9 @@ public class PathIntent extends ConnectivityIntent { |
| 56 | public String toString() { | 56 | public String toString() { |
| 57 | return MoreObjects.toStringHelper(getClass()) | 57 | return MoreObjects.toStringHelper(getClass()) |
| 58 | .add("id", id()) | 58 | .add("id", id()) |
| 59 | - .add("match", selector()) | 59 | + .add("appId", appId()) |
| 60 | - .add("action", treatment()) | 60 | + .add("selector", selector()) |
| 61 | + .add("treatment", treatment()) | ||
| 61 | .add("path", path) | 62 | .add("path", path) |
| 62 | .toString(); | 63 | .toString(); |
| 63 | } | 64 | } | ... | ... |
| ... | @@ -69,10 +69,11 @@ public class PointToPointIntent extends ConnectivityIntent { | ... | @@ -69,10 +69,11 @@ public class PointToPointIntent extends ConnectivityIntent { |
| 69 | public String toString() { | 69 | public String toString() { |
| 70 | return MoreObjects.toStringHelper(getClass()) | 70 | return MoreObjects.toStringHelper(getClass()) |
| 71 | .add("id", id()) | 71 | .add("id", id()) |
| 72 | - .add("match", selector()) | 72 | + .add("appId", appId()) |
| 73 | - .add("action", treatment()) | 73 | + .add("selector", selector()) |
| 74 | - .add("ingressPoint", ingressPoint) | 74 | + .add("treatment", treatment()) |
| 75 | - .add("egressPoints", egressPoint) | 75 | + .add("ingress", ingressPoint) |
| 76 | + .add("egress", egressPoint) | ||
| 76 | .toString(); | 77 | .toString(); |
| 77 | } | 78 | } |
| 78 | 79 | ... | ... |
| ... | @@ -77,10 +77,11 @@ public class SinglePointToMultiPointIntent extends ConnectivityIntent { | ... | @@ -77,10 +77,11 @@ public class SinglePointToMultiPointIntent extends ConnectivityIntent { |
| 77 | public String toString() { | 77 | public String toString() { |
| 78 | return MoreObjects.toStringHelper(getClass()) | 78 | return MoreObjects.toStringHelper(getClass()) |
| 79 | .add("id", id()) | 79 | .add("id", id()) |
| 80 | - .add("match", selector()) | 80 | + .add("appId", appId()) |
| 81 | - .add("action", treatment()) | 81 | + .add("selector", selector()) |
| 82 | - .add("ingressPoint", ingressPoint) | 82 | + .add("treatment", treatment()) |
| 83 | - .add("egressPort", egressPoints) | 83 | + .add("ingress", ingressPoint) |
| 84 | + .add("egress", egressPoints) | ||
| 84 | .toString(); | 85 | .toString(); |
| 85 | } | 86 | } |
| 86 | 87 | ... | ... |
| 1 | -package org.onlab.onos.net.intent; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * This interface is for generator of IntentId. It is defined only for | ||
| 5 | - * testing purpose to keep type safety on mock creation. | ||
| 6 | - * | ||
| 7 | - * <p> | ||
| 8 | - * {@link #getNewId()} generates a globally unique {@link IntentId} instance | ||
| 9 | - * on each invocation. Application developers should not generate IntentId | ||
| 10 | - * by themselves. Instead use an implementation of this interface. | ||
| 11 | - * </p> | ||
| 12 | - */ | ||
| 13 | -public interface IntentIdGenerator extends IdGenerator<IntentId> { | ||
| 14 | -} |
| 1 | -package org.onlab.onos.net.intent.impl; | ||
| 2 | - | ||
| 3 | -import org.onlab.onos.net.intent.IdGenerator; | ||
| 4 | - | ||
| 5 | -/** | ||
| 6 | - * Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as | ||
| 7 | - * backend. | ||
| 8 | - * | ||
| 9 | - * @param <T> the type of ID | ||
| 10 | - */ | ||
| 11 | -public abstract class AbstractBlockAllocatorBasedIdGenerator<T> implements IdGenerator<T> { | ||
| 12 | - protected final IdBlockAllocator allocator; | ||
| 13 | - protected IdBlock idBlock; | ||
| 14 | - | ||
| 15 | - /** | ||
| 16 | - * Constructs an ID generator which use {@link IdBlockAllocator} as backend. | ||
| 17 | - * | ||
| 18 | - * @param allocator | ||
| 19 | - */ | ||
| 20 | - protected AbstractBlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) { | ||
| 21 | - this.allocator = allocator; | ||
| 22 | - this.idBlock = allocator.allocateUniqueIdBlock(); | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | - @Override | ||
| 26 | - public synchronized T getNewId() { | ||
| 27 | - try { | ||
| 28 | - return convertFrom(idBlock.getNextId()); | ||
| 29 | - } catch (UnavailableIdException e) { | ||
| 30 | - idBlock = allocator.allocateUniqueIdBlock(); | ||
| 31 | - return convertFrom(idBlock.getNextId()); | ||
| 32 | - } | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | - /** | ||
| 36 | - * Returns an ID instance of {@code T} type from the long value. | ||
| 37 | - * | ||
| 38 | - * @param value original long value | ||
| 39 | - * @return ID instance | ||
| 40 | - */ | ||
| 41 | - protected abstract T convertFrom(long value); | ||
| 42 | -} |
| 1 | -package org.onlab.onos.net.intent.impl; | ||
| 2 | - | ||
| 3 | -public class DummyIdBlockAllocator implements IdBlockAllocator { | ||
| 4 | - private long blockTop; | ||
| 5 | - private static final long BLOCK_SIZE = 0x1000000L; | ||
| 6 | - | ||
| 7 | - /** | ||
| 8 | - * Returns a block of IDs which are unique and unused. | ||
| 9 | - * Range of IDs is fixed size and is assigned incrementally as this method | ||
| 10 | - * called. | ||
| 11 | - * | ||
| 12 | - * @return an IdBlock containing a set of unique IDs | ||
| 13 | - */ | ||
| 14 | - @Override | ||
| 15 | - public IdBlock allocateUniqueIdBlock() { | ||
| 16 | - synchronized (this) { | ||
| 17 | - long blockHead = blockTop; | ||
| 18 | - long blockTail = blockTop + BLOCK_SIZE; | ||
| 19 | - | ||
| 20 | - IdBlock block = new IdBlock(blockHead, BLOCK_SIZE); | ||
| 21 | - blockTop = blockTail; | ||
| 22 | - | ||
| 23 | - return block; | ||
| 24 | - } | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - @Override | ||
| 28 | - public IdBlock allocateUniqueIdBlock(long range) { | ||
| 29 | - throw new UnsupportedOperationException("Not supported yet"); | ||
| 30 | - } | ||
| 31 | -} |
| 1 | -package org.onlab.onos.net.intent.impl; | ||
| 2 | - | ||
| 3 | -import static com.google.common.base.Preconditions.checkArgument; | ||
| 4 | - | ||
| 5 | -import java.util.Objects; | ||
| 6 | -import java.util.concurrent.atomic.AtomicLong; | ||
| 7 | - | ||
| 8 | -import com.google.common.base.MoreObjects; | ||
| 9 | - | ||
| 10 | -/** | ||
| 11 | - * A class representing an ID space. | ||
| 12 | - */ | ||
| 13 | -public final class IdBlock { | ||
| 14 | - private final long start; | ||
| 15 | - private final long size; | ||
| 16 | - | ||
| 17 | - private final AtomicLong currentId; | ||
| 18 | - | ||
| 19 | - /** | ||
| 20 | - * Constructs a new ID block with the specified size and initial value. | ||
| 21 | - * | ||
| 22 | - * @param start initial value of the block | ||
| 23 | - * @param size size of the block | ||
| 24 | - * @throws IllegalArgumentException if the size is less than or equal to 0 | ||
| 25 | - */ | ||
| 26 | - public IdBlock(long start, long size) { | ||
| 27 | - checkArgument(size > 0, "size should be more than 0, but %s", size); | ||
| 28 | - | ||
| 29 | - this.start = start; | ||
| 30 | - this.size = size; | ||
| 31 | - | ||
| 32 | - this.currentId = new AtomicLong(start); | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | - // TODO: consider if this method is needed or not | ||
| 36 | - /** | ||
| 37 | - * Returns the initial value. | ||
| 38 | - * | ||
| 39 | - * @return initial value | ||
| 40 | - */ | ||
| 41 | - public long getStart() { | ||
| 42 | - return start; | ||
| 43 | - } | ||
| 44 | - | ||
| 45 | - // TODO: consider if this method is needed or not | ||
| 46 | - /** | ||
| 47 | - * Returns the last value. | ||
| 48 | - * | ||
| 49 | - * @return last value | ||
| 50 | - */ | ||
| 51 | - public long getEnd() { | ||
| 52 | - return start + size - 1; | ||
| 53 | - } | ||
| 54 | - | ||
| 55 | - /** | ||
| 56 | - * Returns the block size. | ||
| 57 | - * | ||
| 58 | - * @return block size | ||
| 59 | - */ | ||
| 60 | - public long getSize() { | ||
| 61 | - return size; | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - /** | ||
| 65 | - * Returns the next ID in the block. | ||
| 66 | - * | ||
| 67 | - * @return next ID | ||
| 68 | - * @throws UnavailableIdException if there is no available ID in the block. | ||
| 69 | - */ | ||
| 70 | - public long getNextId() { | ||
| 71 | - final long id = currentId.getAndIncrement(); | ||
| 72 | - if (id > getEnd()) { | ||
| 73 | - throw new UnavailableIdException(String.format( | ||
| 74 | - "used all IDs in allocated space (size: %d, end: %d, current: %d)", | ||
| 75 | - size, getEnd(), id | ||
| 76 | - )); | ||
| 77 | - } | ||
| 78 | - | ||
| 79 | - return id; | ||
| 80 | - } | ||
| 81 | - | ||
| 82 | - // TODO: Do we really need equals and hashCode? Should it contain currentId? | ||
| 83 | - @Override | ||
| 84 | - public boolean equals(Object o) { | ||
| 85 | - if (this == o) { | ||
| 86 | - return true; | ||
| 87 | - } | ||
| 88 | - if (o == null || getClass() != o.getClass()) { | ||
| 89 | - return false; | ||
| 90 | - } | ||
| 91 | - | ||
| 92 | - IdBlock that = (IdBlock) o; | ||
| 93 | - return Objects.equals(this.start, that.start) | ||
| 94 | - && Objects.equals(this.size, that.size) | ||
| 95 | - && Objects.equals(this.currentId.get(), that.currentId.get()); | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - @Override | ||
| 99 | - public int hashCode() { | ||
| 100 | - return Objects.hash(start, size, currentId); | ||
| 101 | - } | ||
| 102 | - | ||
| 103 | - @Override | ||
| 104 | - public String toString() { | ||
| 105 | - return MoreObjects.toStringHelper(getClass()) | ||
| 106 | - .add("start", start) | ||
| 107 | - .add("size", size) | ||
| 108 | - .add("currentId", currentId) | ||
| 109 | - .toString(); | ||
| 110 | - } | ||
| 111 | -} |
| 1 | -package org.onlab.onos.net.intent.impl; | ||
| 2 | - | ||
| 3 | -/** | ||
| 4 | - * An interface that gives unique ID spaces. | ||
| 5 | - */ | ||
| 6 | -public interface IdBlockAllocator { | ||
| 7 | - /** | ||
| 8 | - * Allocates a unique Id Block. | ||
| 9 | - * | ||
| 10 | - * @return Id Block. | ||
| 11 | - */ | ||
| 12 | - IdBlock allocateUniqueIdBlock(); | ||
| 13 | - | ||
| 14 | - /** | ||
| 15 | - * Allocates next unique id and retrieve a new range of ids if needed. | ||
| 16 | - * | ||
| 17 | - * @param range range to use for the identifier | ||
| 18 | - * @return Id Block. | ||
| 19 | - */ | ||
| 20 | - IdBlock allocateUniqueIdBlock(long range); | ||
| 21 | -} |
-
Please register or login to post a comment