{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://nttcom.github.io/pola/schemas/cli/segment.json",
    "title": "Segment of an explicit segment list",
    "type": "object",
    "properties": {
        "sid": {
            "anyOf": [
                {
                    "title": "SR-MPLS label",
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 1048575
                },
                {
                    "$comment": "20-bit MPLS label range (0-1048575).",
                    "title": "SR-MPLS label, quoted",
                    "type": "string",
                    "pattern": "^0*(?:[0-9]{1,6}|10(?:[0-3][0-9]{4}|4(?:[0-7][0-9]{3}|8(?:[0-4][0-9]{2}|5(?:[0-6][0-9]|7[0-5])))))$"
                },
                {
                    "title": "SRv6 SID, IPv6 only",
                    "description": "IPv4-mapped IPv6 addresses (e.g. ::ffff:192.0.2.1) are not allowed.",
                    "type": "string",
                    "format": "ipv6"
                }
            ]
        },
        "sidAbsent": {
            "type": "boolean",
            "description": "Omit the SID and let the PCC resolve it from the NAI (localAddr/remoteAddr)."
        },
        "behavior": {
            "type": "integer",
            "minimum": 0,
            "maximum": 65535,
            "description": "SRv6 endpoint behavior."
        },
        "dataPlane": {
            "enum": ["sr-mpls", "srv6"],
            "description": "Data plane of a SID-absent segment."
        },
        "localAddr": {
            "$ref": "#/$defs/ipAddr"
        },
        "remoteAddr": {
            "$ref": "#/$defs/ipAddr"
        },
        "sidStructure": {
            "type": "string",
            "pattern": "^[0-9]+,[0-9]+,[0-9]+,[0-9]+$",
            "description": "<locator-block>,<locator-node>,<function>,<argument>"
        },
        "localInterfaceId": {
            "type": "integer",
            "minimum": 0,
            "maximum": 4294967295,
            "description": "Local interface ID for link-local IPv6 NAI scoping (RFC 8664/9603 §4.3.1)."
        },
        "remoteInterfaceId": {
            "type": "integer",
            "minimum": 0,
            "maximum": 4294967295,
            "description": "Remote interface ID for link-local IPv6 NAI scoping (RFC 8664/9603 §4.3.1)."
        }
    },
    "if": {
        "not": {
            "properties": { "sidAbsent": { "const": true } },
            "required": ["sidAbsent"]
        }
    },
    "then": {
        "required": ["sid"]
    },
    "else": {
        "required": ["localAddr", "dataPlane"],
        "not": { "required": ["sid"] }
    },
    "additionalProperties": false,
    "$defs": {
        "ipAddr": {
            "$comment": "anyOf, not oneOf: `format` is an annotation by default, so both branches would match and oneOf would always fail",
            "type": "string",
            "anyOf": [
                { "format": "ipv4" },
                { "format": "ipv6" }
            ]
        }
    }
}
