import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import java.math.BigInteger;
import java.util.Collection;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
+import javax.annotation.Nonnull;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
import org.opendaylight.openflowplugin.api.openflow.device.Xid;
import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
+import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
+import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/**
- * Created by kramesha on 8/24/15.
- */
public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
private final DeviceContext deviceContext;
- protected RoleService(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final Class<RoleRequestOutput> clazz) {
+ public RoleService(final RequestContextStack requestContextStack,
+ final DeviceContext deviceContext,
+ final Class<RoleRequestOutput> clazz) {
super(requestContextStack, deviceContext, clazz);
this.deviceContext = deviceContext;
}
@Override
- protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) {
+ protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) throws ServiceException {
input.setXid(xid.getValue());
return input.build();
}
public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
- LOG.info("getGenerationIdFromDevice called for device:{}", getNodeId().getValue());
+ LOG.info("getGenerationIdFromDevice called for device: {}", getDeviceInfo().getNodeId().getValue());
// send a dummy no-change role request to get the generation-id of the switch
final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
roleRequestInputBuilder.setGenerationId(BigInteger.ZERO);
final SettableFuture<BigInteger> finalFuture = SettableFuture.create();
- final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture = handleServiceCall(roleRequestInputBuilder);
+ final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture =
+ handleServiceCall(roleRequestInputBuilder);
Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
@Override
- public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
+ public void onSuccess(@Nonnull final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
if (roleRequestOutputRpcResult.isSuccessful()) {
final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
if (roleRequestOutput != null) {
finalFuture.set(roleRequestOutput.getGenerationId());
} else {
LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
- finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:" + getNodeId().getValue()));
+ finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:"
+ + getDeviceInfo().getNodeId().getValue()));
}
} else {
- LOG.error("getGenerationIdFromDevice RPC error " +
+ LOG.error("getGenerationIdFromDevice RPC error {}",
roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
-
+ finalFuture.setException(new RoleChangeException(ErrorUtil
+ .errorsToString(roleRequestOutputRpcResult.getErrors())));
}
-
}
@Override
LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
finalFuture.setException(new ExecutionException(throwable));
}
- });
+ }, MoreExecutors.directExecutor());
return finalFuture;
}
- public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version, final BigInteger generationId) {
+ public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole,
+ final Short version,
+ final BigInteger generationId) {
LOG.info("submitRoleChange called for device:{}, role:{}",
- getNodeId(), ofpRole);
+ getDeviceInfo().getNodeId(), ofpRole);
final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
roleRequestInputBuilder.setVersion(version);
roleRequestInputBuilder.setGenerationId(generationId);
- final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
+ final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
+ handleServiceCall(roleRequestInputBuilder);
final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
@Override
- public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
+ public void onSuccess(@Nonnull final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
- getNodeId(), ofpRole);
+ getDeviceInfo().getNodeId(), ofpRole);
final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
if (roleRequestOutput != null) {
final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
- setRoleOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
- finalFuture.set(RpcResultBuilder.<SetRoleOutput>success().withResult(setRoleOutputBuilder.build()).build());
+ setRoleOutputBuilder
+ .setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
+ finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
+ .withResult(setRoleOutputBuilder.build()).build());
} else if (rpcErrors != null) {
LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
@Override
public void onFailure(final Throwable throwable) {
LOG.error("submitRoleChange onFailure for device:{}, role:{}",
- getNodeId(), ofpRole, throwable);
+ getDeviceInfo().getNodeId(), ofpRole, throwable);
finalFuture.setException(throwable);
}
- });
+ }, MoreExecutors.directExecutor());
return finalFuture;
}
}
return ofJavaRole;
}
-
-
}