{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://nttcom.github.io/pola/schemas/cli/policy.json",
    "title": "pola sr-policy add input file",
    "type": "object",
    "properties": {
        "asn": {
            "type": "integer",
            "minimum": 1
        },
        "srPolicy": {
            "type": "object",
            "properties": {
                "pcepSessionAddr": {
                    "$ref": "#/$defs/ipAddr"
                },
                "headend": {
                    "$ref": "#/$defs/ipAddr"
                },
                "endpoint": {
                    "$ref": "#/$defs/ipAddr"
                },
                "headendRouterID": {
                    "type": "string"
                },
                "endpointRouterID": {
                    "type": "string"
                },
                "endpointFamily": {
                    "type": "string",
                    "enum": ["ipv4", "ipv6"]
                },
                "name": {
                    "type": "string"
                },
                "color": {
                    "type": "integer",
                    "minimum": 1
                },
                "candidatePath": {
                    "type": "object",
                    "properties": {
                        "preference": {
                            "description": "Defaults to 100 when omitted.",
                            "type": "integer",
                            "minimum": 0,
                            "maximum": 4294967295
                        },
                        "dynamic": {
                            "type": "object",
                            "properties": {
                                "metric": {
                                    "type": "string",
                                    "enum": ["igp", "te", "delay", "min-delay", "hopcount"]
                                },
                                "algorithm": {
                                    "description": "IGP algorithm (RFC 9350 §4). 0 = default SPF; 128-255 = Flex-Algo.",
                                    "type": "integer",
                                    "minimum": 0,
                                    "maximum": 255
                                },
                                "dataPlane": {
                                    "type": "string",
                                    "enum": ["sr-mpls", "srv6"]
                                },
                                "underlayFamily": {
                                    "type": "string",
                                    "enum": ["ipv4", "ipv6"]
                                },
                                "waypoints": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "routerID": {
                                                "type": "string"
                                            },
                                            "sid": {
                                                "type": "string"
                                            }
                                        },
                                        "required": ["routerID"],
                                        "additionalProperties": false
                                    }
                                }
                            },
                            "additionalProperties": false,
                            "oneOf": [
                                {
                                    "title": "Default SPF algorithm: metric is required",
                                    "properties": { "algorithm": { "const": 0 } },
                                    "required": ["metric"]
                                },
                                {
                                    "title": "Flexible Algorithm: the metric comes from the FAD",
                                    "properties": { "algorithm": { "minimum": 128, "maximum": 255 } },
                                    "required": ["algorithm"],
                                    "not": { "required": ["metric"] }
                                }
                            ]
                        },
                        "explicit": {
                            "type": "object",
                            "properties": {
                                "segmentList": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "segment.json"
                                    },
                                    "minItems": 1
                                }
                            },
                            "required": ["segmentList"],
                            "additionalProperties": false
                        }
                    },
                    "oneOf": [
                        { "required": ["dynamic"], "not": { "required": ["explicit"] } },
                        { "required": ["explicit"], "not": { "required": ["dynamic"] } }
                    ],
                    "additionalProperties": false
                }
            },
            "required": ["pcepSessionAddr", "color", "candidatePath"],
            "additionalProperties": false,
            "oneOf": [
                {
                    "title": "Endpoints resolved from the TED by router ID",
                    "required": ["headendRouterID", "endpointRouterID"],
                    "not": {
                        "anyOf": [
                            { "required": ["headend"] },
                            { "required": ["endpoint"] }
                        ]
                    }
                },
                {
                    "title": "Endpoints given directly as addresses",
                    "required": ["headend", "endpoint"],
                    "not": {
                        "anyOf": [
                            { "required": ["headendRouterID"] },
                            { "required": ["endpointRouterID"] },
                            { "required": ["endpointFamily"] }
                        ]
                    }
                }
            ]
        }
    },
    "required": [
        "srPolicy"
    ],
    "allOf": [
        {
            "$comment": "The router-ID form resolves endpoints from the TED, which is keyed by ASN",
            "if": {
                "required": ["srPolicy"],
                "properties": {
                    "srPolicy": {
                        "anyOf": [
                            { "required": ["headendRouterID"] },
                            { "required": ["endpointRouterID"] }
                        ]
                    }
                }
            },
            "then": {
                "required": ["asn"]
            }
        },
        {
            "$comment": "A dynamic candidate path is TED-computed, which is keyed by ASN",
            "if": {
                "required": ["srPolicy"],
                "properties": {
                    "srPolicy": {
                        "required": ["candidatePath"],
                        "properties": {
                            "candidatePath": {
                                "required": ["dynamic"]
                            }
                        }
                    }
                }
            },
            "then": {
                "required": ["asn"]
            }
        }
    ],
    "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" }
            ]
        }
    }
}
