import com.google.common.util.concurrent.ListenableFuture;
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 org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.device.Xid;
import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
+import org.opendaylight.yangtools.yang.common.RpcError;
import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
@Override
- protected OfHeader buildRequest(Xid xid, RoleRequestInputBuilder input) {
+ protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) throws ServiceException {
input.setXid(xid.getValue());
return input.build();
}
- public Future<BigInteger> getGenerationIdFromDevice(Short version) throws RoleChangeException {
- final NodeId nodeId = deviceContext.getPrimaryConnectionContext().getNodeId();
- LOG.info("getGenerationIdFromDevice called for device:{}", nodeId.getValue());
+ public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
+ 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();
- ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture = handleServiceCall(roleRequestInputBuilder);
+ final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture = handleServiceCall(roleRequestInputBuilder);
Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
@Override
- public void onSuccess(RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
+ public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
if (roleRequestOutputRpcResult.isSuccessful()) {
- RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
+ final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
if (roleRequestOutput != null) {
LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
finalFuture.set(roleRequestOutput.getGenerationId());
} else {
LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
- finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:" + nodeId.getValue()));
+ finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:" + getDeviceInfo().getNodeId().getValue()));
}
} else {
}
@Override
- public void onFailure(Throwable throwable) {
+ public void onFailure(final Throwable throwable) {
LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
finalFuture.setException(new ExecutionException(throwable));
}
}
- public Future<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:{}",
- deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole);
- RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
+ getDeviceInfo().getNodeId(), ofpRole);
+ final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
roleRequestInputBuilder.setVersion(version);
roleRequestInputBuilder.setGenerationId(generationId);
- ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
+ final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
- final SettableFuture<SetRoleOutput> finalFuture = SettableFuture.create();
+ final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
@Override
- public void onSuccess(RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
+ public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
- deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole);
- RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
- SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
- setRoleOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
- finalFuture.set(setRoleOutputBuilder.build());
+ 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());
+
+ } else if (rpcErrors != null) {
+ LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
+ for (RpcError rpcError : rpcErrors) {
+ LOG.warn("RpcError on submitRoleChange for {}: {}",
+ deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
+ }
+
+ finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
+ }
}
@Override
- public void onFailure(Throwable throwable) {
+ public void onFailure(final Throwable throwable) {
LOG.error("submitRoleChange onFailure for device:{}, role:{}",
- deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole, throwable);
- finalFuture.set(null);
+ getDeviceInfo().getNodeId(), ofpRole, throwable);
+ finalFuture.setException(throwable);
}
});
return finalFuture;
}
- private static ControllerRole toOFJavaRole(OfpRole role) {
+ private static ControllerRole toOFJavaRole(final OfpRole role) {
ControllerRole ofJavaRole = null;
switch (role) {
case BECOMEMASTER: