Sho SHIMIZU
Committed by Gerrit Code Review

Generalize the methods in ResourceAdminService

Change-Id: Ib78d2ec441651c3215c422ba4b46d726158de4a9
......@@ -16,8 +16,8 @@
package org.onosproject.net.newresource;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.List;
/**
......@@ -26,50 +26,42 @@ import java.util.List;
@Beta
public interface ResourceAdminService {
/**
* Registers resources as the children of the parent resource path.
* Registers the specified resources.
*
* @param parent parent resource path under which the resource are registered
* @param children resources to be registered as the children of the parent
* @param <T> type of resources
* @param resources resources to be registered
* @return true if registration is successfully done, false otherwise. Registration
* succeeds when each resource is not registered or unallocated.
*/
default <T> boolean registerResources(ResourcePath parent, T... children) {
return registerResources(parent, Arrays.asList(children));
default boolean registerResources(ResourcePath... resources) {
return registerResources(ImmutableList.copyOf(resources));
}
/**
* Registers resources as the children of the parent resource path.
* Registers the specified resources.
*
* @param parent parent resource path under which the resource are registered
* @param children resources to be registered as the children of the parent
* @param <T> type of resources
* @param resources resources to be registered
* @return true if registration is successfully done, false otherwise. Registration
* succeeds when each resource is not registered or unallocated.
*/
<T> boolean registerResources(ResourcePath parent, List<T> children);
boolean registerResources(List<ResourcePath> resources);
/**
* Unregisters resources as the children of the parent resource path.
* Unregisters the specified resources.
*
* @param parent parent resource path under which the resource are unregistered
* @param children resources to be unregistered as the children of the parent
* @param <T> type of resources
* @param resources resources to be unregistered
* @return true if unregistration is successfully done, false otherwise. Unregistration
* succeeds when each resource is not registered or unallocated.
*/
default <T> boolean unregisterResources(ResourcePath parent, T... children) {
return unregisterResources(parent, Arrays.asList(children));
default boolean unregisterResources(ResourcePath... resources) {
return unregisterResources(ImmutableList.copyOf(resources));
}
/**
* Unregisters resources as the children of the parent resource path.
* Unregisters the specified resources.
*
* @param parent parent resource path under which the resource are unregistered
* @param children resources to be unregistered as the children of the parent
* @param <T> type of resources
* @param resources resources to be unregistered
* @return true if unregistration is successfully done, false otherwise. Unregistration
* succeeds when each resource is not registered or unallocated.
*/
<T> boolean unregisterResources(ResourcePath parent, List<T> children);
boolean unregisterResources(List<ResourcePath> resources);
}
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.net.newresource.impl;
import com.google.common.collect.Lists;
import org.onosproject.net.Device;
import org.onosproject.net.Port;
import org.onosproject.net.OchPort;
......@@ -82,39 +83,36 @@ final class ResourceDeviceListener implements DeviceListener {
}
private void registerDeviceResource(Device device) {
executor.submit(() -> adminService.registerResources(ResourcePath.ROOT, device.id()));
executor.submit(() -> adminService.registerResources(ResourcePath.discrete(device.id())));
}
private void unregisterDeviceResource(Device device) {
executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, device.id()));
executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(device.id())));
}
private void registerPortResource(Device device, Port port) {
ResourcePath parent = ResourcePath.discrete(device.id());
executor.submit(() -> registerPortResource(device, port, parent));
}
private void registerPortResource(Device device, Port port, ResourcePath parent) {
adminService.registerResources(parent, port.number());
ResourcePath portPath = ResourcePath.discrete(device.id(), port.number());
switch (port.type()) {
case OCH:
// register ODU TributarySlots against the OCH port
registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
break;
default:
break;
}
executor.submit(() -> {
adminService.registerResources(portPath);
switch (port.type()) {
case OCH:
// register ODU TributarySlots against the OCH port
registerTributarySlotsResources(((OchPort) port).signalType(), portPath);
break;
default:
break;
}
});
}
private void registerTributarySlotsResources(OduSignalType oduSignalType, ResourcePath portPath) {
switch (oduSignalType) {
case ODU2:
adminService.registerResources(portPath, ENTIRE_ODU2_TRIBUTARY_SLOTS);
adminService.registerResources(Lists.transform(ENTIRE_ODU2_TRIBUTARY_SLOTS, portPath::child));
break;
case ODU4:
adminService.registerResources(portPath, ENTIRE_ODU4_TRIBUTARY_SLOTS);
adminService.registerResources(Lists.transform(ENTIRE_ODU4_TRIBUTARY_SLOTS, portPath::child));
break;
default:
break;
......@@ -122,8 +120,8 @@ final class ResourceDeviceListener implements DeviceListener {
}
private void unregisterPortResource(Device device, Port port) {
ResourcePath parent = ResourcePath.discrete(device.id());
executor.submit(() -> adminService.unregisterResources(parent, port.number()));
ResourcePath resource = ResourcePath.discrete(device.id(), port.number());
executor.submit(() -> adminService.unregisterResources(resource));
}
private static List<TributarySlot> getEntireOdu2TributarySlots() {
......
......@@ -15,6 +15,7 @@
*/
package org.onosproject.net.newresource.impl;
import com.google.common.collect.Lists;
import org.onlab.packet.MplsLabel;
import org.onlab.packet.VlanId;
import org.onlab.util.ItemNotFoundException;
......@@ -85,24 +86,24 @@ final class ResourceLinkListener implements LinkListener {
executor.submit(() -> {
// register the link
LinkKey linkKey = LinkKey.linkKey(link);
adminService.registerResources(ResourcePath.ROOT, linkKey);
adminService.registerResources(ResourcePath.discrete(linkKey));
ResourcePath linkPath = ResourcePath.discrete(linkKey);
// register VLAN IDs against the link
if (isEnabled(link, this::isVlanEnabled)) {
adminService.registerResources(linkPath, ENTIRE_VLAN_IDS);
adminService.registerResources(Lists.transform(ENTIRE_VLAN_IDS, linkPath::child));
}
// register MPLS labels against the link
if (isEnabled(link, this::isMplsEnabled)) {
adminService.registerResources(linkPath, ENTIRE_MPLS_LABELS);
adminService.registerResources(Lists.transform(ENTIRE_MPLS_LABELS, linkPath::child));
}
});
}
private void unregisterLinkResource(Link link) {
LinkKey linkKey = LinkKey.linkKey(link);
executor.submit(() -> adminService.unregisterResources(ResourcePath.ROOT, linkKey));
executor.submit(() -> adminService.unregisterResources(ResourcePath.discrete(linkKey)));
}
private boolean isEnabled(Link link, Predicate<ConnectPoint> predicate) {
......
......@@ -17,7 +17,6 @@ package org.onosproject.net.newresource.impl;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -41,7 +40,6 @@ import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
......@@ -164,23 +162,17 @@ public final class ResourceManager extends AbstractListenerManager<ResourceEvent
}
@Override
public <T> boolean registerResources(ResourcePath parent, List<T> children) {
checkNotNull(parent);
checkNotNull(children);
checkArgument(!children.isEmpty());
public boolean registerResources(List<ResourcePath> resources) {
checkNotNull(resources);
List<ResourcePath> resources = Lists.transform(children, parent::child);
return store.register(resources);
}
@Override
public <T> boolean unregisterResources(ResourcePath parent, List<T> children) {
checkNotNull(parent);
checkNotNull(children);
checkArgument(!children.isEmpty());
public boolean unregisterResources(List<ResourcePath> resources) {
checkNotNull(resources);
List<ResourcePath> resources = Lists.transform(children, parent::child);
return store.unregister(resources);
return store.register(resources);
}
private class InternalStoreDelegate implements ResourceStoreDelegate {
......