@Override
  public TaskEntity setDefault(String networkId) throws ExternalException {
    NetworkService.State currentDefaultNetwork = getDefaultNetwork();

    if (currentDefaultNetwork != null) {
      NetworkService.State currentDefaultNetworkPatch = new NetworkService.State();
      currentDefaultNetworkPatch.isDefault = false;

      try {
        xenonClient.patch(currentDefaultNetwork.documentSelfLink, currentDefaultNetworkPatch);
      } catch (DocumentNotFoundException ex) {
        throw new NetworkNotFoundException(
            "Failed to patch current default network " + currentDefaultNetwork.documentSelfLink);
      }
    }

    NetworkService.State newDefaultNetwork = getById(networkId);
    NetworkService.State newDefaultNetworkPatch = new NetworkService.State();
    newDefaultNetworkPatch.isDefault = true;
    try {
      newDefaultNetwork =
          xenonClient
              .patch(newDefaultNetwork.documentSelfLink, newDefaultNetworkPatch)
              .getBody(NetworkService.State.class);
    } catch (DocumentNotFoundException ex) {
      throw new NetworkNotFoundException(
          "Failed to patch new default network " + newDefaultNetwork.documentSelfLink);
    }

    return taskBackend.createCompletedTask(
        convertToEntity(newDefaultNetwork), Operation.SET_DEFAULT_NETWORK);
  }
 private void patchNetworkService(String id, NetworkService.State networkState)
     throws NetworkNotFoundException {
   try {
     xenonClient.patch(NetworkServiceFactory.SELF_LINK + "/" + id, networkState);
   } catch (DocumentNotFoundException e) {
     throw new NetworkNotFoundException(id);
   }
 }