def get_namespace(opts, msp=None, ca=None):
    """Get relevant namespace where MSP or CA should be located.

    Args:
        opts (dict): Nephos options dict.
        msp (str): Name of Membership Service Provider (MSP).
        ca (str): Name of Certificate Authority (CA).

    Returns:
        str: Namespace relating to either an MSP or a CA.
    """
    if msp is not None:
        if "msps" in opts and msp in opts["msps"]:
            msp_values = opts["msps"][msp]
        else:
            raise KeyError('Settings dict does not contain MSP "{}"'.format(msp))
        if "namespace" in msp_values:
            # Specific MSP-based namespace
            return msp_values["namespace"]
    elif ca is not None:
        if "cas" in opts and ca in opts["cas"]:
            ca_values = opts["cas"][ca]
        else:
            raise KeyError('Settings dict does not contain CA "{}"'.format(ca))
        if "namespace" in ca_values:
            # Specific MSP-based namespace
            return ca_values["namespace"]
    # Default case is to return core namespace
    return opts["core"]["namespace"]