Ayaka Koshibe
Committed by Gerrit Code Review

ConsistentLinkResourceStore to replace HazelcastLinkResourceStore. Also

includes:

 - typo fix (intendId -> intentId)
 - refactored ResourceAllocations command so it doesn't use error handling as
   part of control flow
 - add ability to compare LinkResourceAllocations

Reference: ONOS-1076

Conflicts:
	cli/src/main/java/org/onosproject/cli/net/ResourceAllocationsCommand.java

Change-Id: I6a68012d8d7d359ce7c5dcd31e80a3b9f63d5670
......@@ -21,14 +21,13 @@ import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Link;
import org.onosproject.net.link.LinkService;
import org.onosproject.net.resource.LinkResourceAllocations;
import org.onosproject.net.resource.LinkResourceService;
/**
* Lists allocations by link.
* Lists allocations by link. Lists all allocations if link is unspecified.
*/
@Command(scope = "onos", name = "resource-allocations",
description = "Lists allocations by link")
description = "Lists allocations by link. Lists all allocations if link is unspecified.")
public class ResourceAllocationsCommand extends AbstractShellCommand {
private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s%s";
......@@ -46,27 +45,20 @@ public class ResourceAllocationsCommand extends AbstractShellCommand {
LinkResourceService resourceService = get(LinkResourceService.class);
LinkService linkService = get(LinkService.class);
Iterable<LinkResourceAllocations> itr = null;
try {
ConnectPoint src = ConnectPoint.deviceConnectPoint(srcString);
if (srcString == null || dstString == null) {
print("----- Displaying all resource allocations -----");
resourceService.getAllocations().forEach(alloc -> print("%s", alloc));
return;
}
ConnectPoint src = ConnectPoint.deviceConnectPoint(srcString);
ConnectPoint dst = ConnectPoint.deviceConnectPoint(dstString);
Link link = linkService.getLink(src, dst);
itr = resourceService.getAllocations(link);
for (LinkResourceAllocations allocation : itr) {
print("%s", allocation.getResourceAllocation(link));
}
} catch (Exception e) {
print("----- Displaying all resource allocations -----", e.getMessage());
itr = resourceService.getAllocations();
for (LinkResourceAllocations allocation : itr) {
print("%s", allocation);
}
if (link != null) {
resourceService.getAllocations(link).forEach(alloc -> print("%s", alloc));
} else {
print("No path found for endpoints: %s, %s", src, dst);
}
}
}
......
......@@ -15,6 +15,8 @@
*/
package org.onosproject.net.resource;
import java.util.Objects;
import com.google.common.base.MoreObjects;
/**
......@@ -48,6 +50,23 @@ public class BandwidthResourceRequest implements ResourceRequest {
}
@Override
public int hashCode() {
return Objects.hash(bandwidth);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final BandwidthResourceAllocation other = (BandwidthResourceAllocation) obj;
return Objects.equals(this.bandwidth, other.bandwidth());
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("bandwidth", bandwidth)
......
......@@ -27,6 +27,7 @@ import org.onosproject.net.intent.IntentId;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Map.Entry;
import java.util.Set;
......@@ -56,8 +57,8 @@ public class DefaultLinkResourceAllocations implements LinkResourceAllocations {
}
@Override
public IntentId intendId() {
return request.intendId();
public IntentId intentId() {
return request.intentId();
}
@Override
......@@ -85,6 +86,24 @@ public class DefaultLinkResourceAllocations implements LinkResourceAllocations {
}
@Override
public int hashCode() {
return Objects.hash(request, allocations);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final DefaultLinkResourceAllocations other = (DefaultLinkResourceAllocations) obj;
return Objects.equals(this.request, other.request)
&& Objects.equals(this.allocations, other.allocations);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("allocations", allocations)
......
......@@ -18,12 +18,14 @@ package org.onosproject.net.resource;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.Objects;
import org.onosproject.net.Link;
import org.onosproject.net.intent.Constraint;
import org.onosproject.net.intent.IntentId;
import com.google.common.collect.ImmutableSet;
import org.onosproject.net.intent.constraint.BandwidthConstraint;
import org.onosproject.net.intent.constraint.LambdaConstraint;
......@@ -59,7 +61,7 @@ public final class DefaultLinkResourceRequest implements LinkResourceRequest {
}
@Override
public IntentId intendId() {
public IntentId intentId() {
return intentId;
}
......@@ -150,7 +152,6 @@ public final class DefaultLinkResourceRequest implements LinkResourceRequest {
return this;
}
/**
* Returns link resource request.
*
......@@ -162,4 +163,21 @@ public final class DefaultLinkResourceRequest implements LinkResourceRequest {
}
}
@Override
public int hashCode() {
return Objects.hash(intentId, links);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final DefaultLinkResourceRequest other = (DefaultLinkResourceRequest) obj;
return Objects.equals(this.intentId, other.intentId)
&& Objects.equals(this.links, other.links);
}
}
......
......@@ -32,7 +32,7 @@ public interface LinkResourceRequest extends ResourceRequest {
*
* @return the {@link IntentId} associated with the request
*/
IntentId intendId();
IntentId intentId();
/**
* Returns the set of target links.
......
......@@ -16,7 +16,6 @@
package org.onosproject.store.service;
/**
* Transactional Map data structure.
* <p>
......
......@@ -184,7 +184,7 @@ public class IntentTestsMocks {
}
@Override
public IntentId intendId() {
public IntentId intentId() {
return null;
}
......
......@@ -34,7 +34,6 @@ import com.google.common.collect.Maps;
* Default TransactionContext implementation.
*/
public class DefaultTransactionContext implements TransactionContext {
private static final String TX_NOT_OPEN_ERROR = "Transaction Context is not open";
@SuppressWarnings("rawtypes")
......
......@@ -356,7 +356,6 @@ public class PartitionedDatabase implements Database {
}
Map<Database, Transaction> subTransactions = Maps.newHashMap();
perPartitionUpdates.forEach((k, v) -> subTransactions.put(k, new DefaultTransaction(transaction.id(), v)));
return subTransactions;
}
}
......
......@@ -16,6 +16,7 @@
package org.onosproject.store.consistent.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
......
......@@ -339,7 +339,7 @@ public class HazelcastLinkResourceStore
STxMap<IntentId, LinkResourceAllocations> intentAllocs = getIntentAllocs(tx);
// should this be conditional write?
intentAllocs.put(allocations.intendId(), allocations);
intentAllocs.put(allocations.intentId(), allocations);
for (Link link : allocations.links()) {
allocateLinkResource(tx, link, allocations);
......@@ -349,7 +349,7 @@ public class HazelcastLinkResourceStore
return;
} catch (TransactionException e) {
log.debug("Failed to commit allocations for {}. [retry={}]",
allocations.intendId(), i);
allocations.intentId(), i);
log.trace(" details {} ", allocations, e);
continue;
} catch (Exception e) {
......@@ -438,7 +438,7 @@ public class HazelcastLinkResourceStore
public LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
checkNotNull(allocations);
final IntentId intendId = allocations.intendId();
final IntentId intendId = allocations.intentId();
final Collection<Link> links = allocations.links();
boolean success = false;
......
......@@ -224,7 +224,7 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
@Override
public synchronized void allocateResources(LinkResourceAllocations allocations) {
checkNotNull(allocations);
linkResourceAllocationsMap.put(allocations.intendId(), allocations);
linkResourceAllocationsMap.put(allocations.intentId(), allocations);
for (Link link : allocations.links()) {
subtractFreeResources(link, allocations);
Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link);
......@@ -239,7 +239,7 @@ public class SimpleLinkResourceStore implements LinkResourceStore {
@Override
public synchronized LinkResourceEvent releaseResources(LinkResourceAllocations allocations) {
checkNotNull(allocations);
linkResourceAllocationsMap.remove(allocations.intendId());
linkResourceAllocationsMap.remove(allocations.intentId());
for (Link link : allocations.links()) {
addFreeResources(link, allocations);
Set<LinkResourceAllocations> linkAllocs = allocatedResources.get(link);
......
......@@ -191,7 +191,7 @@ public class SimpleLinkResourceStoreTest {
}
@Override
public IntentId intendId() {
public IntentId intentId() {
return null;
}
......@@ -227,7 +227,7 @@ public class SimpleLinkResourceStoreTest {
}
@Override
public IntentId intendId() {
public IntentId intentId() {
return null;
}
......