def _add_to_chain(pid, sid, obsoletes_pid, obsoleted_by_pid):
    _assert_sid_is_in_chain(sid, obsoletes_pid)
    _assert_sid_is_in_chain(sid, obsoleted_by_pid)
    obsoletes_chain_model = _get_chain_by_pid(obsoletes_pid)
    obsoleted_by_chain_model = _get_chain_by_pid(obsoleted_by_pid)
    sid_chain_model = _get_chain_by_sid(sid) if sid else None
    chain_model = obsoletes_chain_model or obsoleted_by_chain_model or sid_chain_model
    if not chain_model:
        return False
    if obsoletes_chain_model and obsoletes_chain_model != chain_model:
        _merge_chains(chain_model, obsoletes_chain_model)
    if obsoleted_by_chain_model and obsoleted_by_chain_model != chain_model:
        _merge_chains(chain_model, obsoleted_by_chain_model)
    _add_pid_to_chain(chain_model, pid)
    _set_chain_sid(chain_model, sid)
    return True