@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);
  }
  @Override
  public TaskEntity updatePortGroups(String id, List<String> portGroups) throws ExternalException {
    NetworkService.State network = getById(id);
    checkPortGroupsExist(portGroups);
    checkPortGroupsNotAddedToAnySubnet(getPortGroupDelta(network, portGroups));

    NetworkService.State networkStateUpdate = new NetworkService.State();
    networkStateUpdate.portGroups = portGroups;
    patchNetworkService(id, networkStateUpdate);

    TaskEntity task =
        taskBackend.createCompletedTask(convertToEntity(network), Operation.SET_PORT_GROUPS);
    return task;
  }
  @Override
  public TaskEntity prepareNetworkDelete(String id) throws ExternalException {
    NetworkEntity network = convertToEntity(getById(id));
    if (SubnetState.PENDING_DELETE.equals(network.getState())) {
      throw new InvalidNetworkStateException(
          String.format(
              "Invalid operation to delete network %s in state PENDING_DELETE", network.getId()));
    }

    NetworkService.State networkState = new NetworkService.State();
    networkState.state = SubnetState.PENDING_DELETE;
    networkState.deleteRequestTime = System.currentTimeMillis();
    this.patchNetworkService(id, networkState);
    this.tombstone(network);

    return taskBackend.createCompletedTask(network, Operation.DELETE_NETWORK);
  }
  @Override
  public TaskEntity createNetwork(SubnetCreateSpec network) throws ExternalException {
    checkPortGroupsExist(network.getPortGroups());
    checkPortGroupsNotAddedToAnySubnet(network.getPortGroups());

    NetworkService.State state = new NetworkService.State();
    state.name = network.getName();
    state.description = network.getDescription();
    state.portGroups = network.getPortGroups();
    state.state = SubnetState.READY;

    com.vmware.xenon.common.Operation result =
        xenonClient.post(NetworkServiceFactory.SELF_LINK, state);

    NetworkService.State createdState = result.getBody(NetworkService.State.class);

    String id = ServiceUtils.getIDFromDocumentSelfLink(createdState.documentSelfLink);

    NetworkEntity networkEntity = new NetworkEntity();
    networkEntity.setId(id);
    networkEntity.setDescription(createdState.description);
    networkEntity.setPortGroups(getPortGroupsJSONString(createdState.portGroups));
    return taskBackend.createCompletedTask(networkEntity, Operation.CREATE_NETWORK);
  }