Search in sources :

Example 1 with Builder

use of org.opendaylight.netvirt.aclservice.api.utils.AclInterface.Builder in project netvirt by opendaylight.

the class AclInterfaceCacheImpl method updateIfPresent.

@Override
public AclInterface updateIfPresent(String interfaceId, BiFunction<AclInterface, AclInterface.Builder, Boolean> updateFunction) {
    final AtomicBoolean updated = new AtomicBoolean(false);
    AclInterface aclInterface = cache.computeIfPresent(interfaceId, (key, prevAclInterface) -> {
        Builder builder = AclInterface.builder(prevAclInterface);
        updated.set(updateFunction.apply(prevAclInterface, builder));
        return builder.build();
    });
    return updated.get() ? aclInterface : null;
}
Also used : AclInterface(org.opendaylight.netvirt.aclservice.api.utils.AclInterface) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Builder(org.opendaylight.netvirt.aclservice.api.utils.AclInterface.Builder)

Example 2 with Builder

use of org.opendaylight.netvirt.aclservice.api.utils.AclInterface.Builder in project netvirt by opendaylight.

the class AclInterfaceCacheImpl method addOrUpdate.

@Override
public AclInterface addOrUpdate(@Nonnull String interfaceId, BiConsumer<AclInterface, AclInterface.Builder> updateFunction) {
    while (true) {
        // First try to update the existing instance in the cache if one exists.
        AclInterface aclInterface = cache.computeIfPresent(interfaceId, (key, prevAclInterface) -> {
            Builder builder = AclInterface.builder(prevAclInterface);
            updateFunction.accept(prevAclInterface, builder);
            return builder.build();
        });
        if (aclInterface == null) {
            // No existing instance so try to put a new one.
            Builder builder = AclInterface.builder();
            builder.interfaceId(interfaceId);
            updateFunction.accept(null, builder);
            aclInterface = builder.build();
            if (cache.putIfAbsent(interfaceId, aclInterface) == null) {
                // The new instance was added.
                return aclInterface;
            }
        // The new instance wasn't added - some one else beat us to it. Loop back up and try again.
        } else {
            return aclInterface;
        }
    }
}
Also used : AclInterface(org.opendaylight.netvirt.aclservice.api.utils.AclInterface) Builder(org.opendaylight.netvirt.aclservice.api.utils.AclInterface.Builder)

Aggregations

AclInterface (org.opendaylight.netvirt.aclservice.api.utils.AclInterface)2 Builder (org.opendaylight.netvirt.aclservice.api.utils.AclInterface.Builder)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1