Merge "BUG-4117: preparation for FlowCapableNode notification impl."
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / RoleService.java
index 60637f117a2ff6fbbf6b12a1891e8caeb9c39024..c677c53a68247079504cb9a8664125eb798020f6 100644 (file)
@@ -12,6 +12,7 @@ import com.google.common.util.concurrent.Futures;
 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;
@@ -27,7 +28,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 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;
 
@@ -45,14 +48,13 @@ public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder,
     }
 
     @Override
-    protected OfHeader buildRequest(Xid xid, RoleRequestInputBuilder input) {
+    protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) {
         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:{}", getNodeId().getValue());
 
         // send a dummy no-change role request to get the generation-id of the switch
         final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
@@ -61,18 +63,18 @@ public class RoleService extends AbstractSimpleService<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:" + getNodeId().getValue()));
                     }
 
                 } else {
@@ -84,7 +86,7 @@ public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder,
             }
 
             @Override
-            public void onFailure(Throwable throwable) {
+            public void onFailure(final Throwable throwable) {
                 LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
                 finalFuture.setException(new ExecutionException(throwable));
             }
@@ -93,39 +95,51 @@ public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder,
     }
 
 
-    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();
+                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());
+                        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);
+                        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: