Commit 314bdf9b authored by Mark Canterbury's avatar Mark Canterbury
Browse files

Using xmltodict to generate json

parent e810e266
Loading
Loading
Loading
Loading
Loading
+92 −0
Original line number Diff line number Diff line
{
    "HI1Message": {
        "@xmlns": "http://uri.etsi.org/03120/common/2019/10/Core",
        "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
        "@xmlns:common": "http://uri.etsi.org/03120/common/2016/02/Common",
        "@xmlns:task": "http://uri.etsi.org/03120/common/2020/09/Task",
        "@xmlns:auth": "http://uri.etsi.org/03120/common/2020/09/Authorisation",
        "Header": {
            "SenderIdentifier": {
                "CountryCode": "XX",
                "UniqueIdentifier": "ACTOR01"
            },
            "ReceiverIdentifier": {
                "CountryCode": "XX",
                "UniqueIdentifier": "ACTOR02"
            },
            "TransactionIdentifier": "c02358b2-76cf-4ba4-a8eb-f6436ccaea2e",
            "Timestamp": "2015-09-01T12:00:00.000000Z",
            "Version": {
                "ETSIVersion": "V1.13.1",
                "NationalProfileOwner": "XX",
                "NationalProfileVersion": "v1.0"
            }
        },
        "Payload": {
            "RequestPayload": {
                "ActionRequests": {
                    "ActionRequest": [
                        {
                            "ActionIdentifier": "0",
                            "CREATE": {
                                "HI1Object": {
                                    "@xsi:type": "auth:AuthorisationObject",
                                    "ObjectIdentifier": "7dbbc880-8750-4d3c-abe7-ea4a17646045",
                                    "CountryCode": "XX",
                                    "OwnerIdentifier": "ACTOR01",
                                    "auth:AuthorisationReference": "W000001",
                                    "auth:AuthorisationTimespan": {
                                        "auth:StartTime": "2015-09-01T12:00:00Z",
                                        "auth:EndTime": "2015-12-01T12:00:00Z"
                                    }
                                }
                            }
                        },
                        {
                            "ActionIdentifier": "1",
                            "CREATE": {
                                "HI1Object": {
                                    "@xsi:type": "task:LITaskObject",
                                    "ObjectIdentifier": "2b36a78b-b628-416d-bd22-404e68a0cd36",
                                    "CountryCode": "XX",
                                    "OwnerIdentifier": "ACTOR01",
                                    "AssociatedObjects": {
                                        "AssociatedObject": "7dbbc880-8750-4d3c-abe7-ea4a17646045"
                                    },
                                    "task:Reference": "LIID1",
                                    "task:TargetIdentifier": {
                                        "task:TargetIdentifierValues": {
                                            "task:TargetIdentifierValue": {
                                                "task:FormatType": {
                                                    "task:FormatOwner": "ETSI",
                                                    "task:FormatName": "InternationalE164"
                                                },
                                                "task:Value": "442079460223"
                                            }
                                        }
                                    },
                                    "task:DeliveryType": {
                                        "common:Owner": "ETSI",
                                        "common:Name": "TaskDeliveryType",
                                        "common:Value": "IRIandCC"
                                    },
                                    "task:DeliveryDetails": {
                                        "task:DeliveryDestination": {
                                            "task:DeliveryAddress": {
                                                "task:IPv4Address": "192.0.2.0"
                                            }
                                        }
                                    },
                                    "task:CSPID": {
                                        "CountryCode": "XX",
                                        "UniqueIdentifier": "RECVER01"
                                    }
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
}
 No newline at end of file
+94 −0
Original line number Diff line number Diff line
import sys
from jsonschema import validate, RefResolver, Draft202012Validator
import json
from pathlib import Path
import logging
import argparse



# filename = sys.argv[1]

# def load_json (path):    
#     with open(path) as f:
#         s = json.load(f)
#     return s

# schema_store = {}

# json_instance = load_json(filename)
# print (json_instance)

# etsi_schema = load_json('response.schema.json')
# ext_schema = load_json('extended.schema.json')
# ext_ent_schema = load_json("extended_entities.schema.json")
# schema_store = {
#     etsi_schema['$id'] : etsi_schema,
#     ext_schema['$id'] : ext_schema,
#     ext_ent_schema['$id'] : ext_ent_schema
# }

# resolver = RefResolver(None, referrer=None, store=schema_store)

# print (etsi_schema)

# v = Draft202012Validator(ext_schema, resolver=resolver)
# v.validate(json_instance)

# validate(json_instance, ext_schema)
# print ("OK")

def handle_uri(u):
    print(u)

def load_json(path : str):
    with open(path) as f:
        return json.load(f)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument('-s','--schemadir', action="append", help="Directory containing supporting schema files to use for validation")
    parser.add_argument('-v', '--verbose', action="count", help="Verbose logging (can be specified multiple times)")
    parser.add_argument('schema', help="Primary schema to validate against")    
    parser.add_argument('filename', help="JSON instance document to validate")

    args = parser.parse_args()

    match args.verbose:
        case v if v and v >= 2:
            logging.basicConfig(level=logging.DEBUG)
        case 1:
            logging.basicConfig(level=logging.INFO)
        case _:
            logging.basicConfig(level=logging.WARNING)

    logging.debug(f"Arguments: {args}")

    instance_doc = load_json(args.filename)
    main_schema = load_json(args.schema)    
    schema_dict = { main_schema['$id'] : main_schema }

    if args.schemadir:
        schema_paths = []    
        for d in args.schemadir:
            schema_paths += [f for f in Path(d).rglob("*.schema.json")]
        logging.info(f"Schema files loaded: {schema_paths}")

        schemas_json = [json.load(p.open()) for p in schema_paths]
        schema_dict = schema_dict | { s['$id'] : s for s in schemas_json }

    logging.info(f"Schema IDs loaded: {[k for k in schema_dict.keys()]}")

    logging.debug (f"Instance doc: {instance_doc}")
    logging.debug (f"Main schema: {main_schema}")

    resolver = RefResolver(None, 
                           referrer=None, 
                           store=schema_dict)
    
    v = Draft202012Validator(main_schema, resolver=resolver)

    v.validate(instance_doc)

    logging.info("Done")
 No newline at end of file
+12 −0
Original line number Diff line number Diff line
import xmltodict
from pprint import pprint
import json

with open('../examples/request1.xml') as f:
    s = f.read()

d = xmltodict.parse(s)
pprint (d)

with open('../examples/request.json', 'w') as f2:
    f2.write(json.dumps(d))
 No newline at end of file
+387 −0
Original line number Diff line number Diff line
{ 
    "$id"         : "http://etsi.org/temp/280",
    "$schema"     : "https://json-schema.org/draft/2020-12/schema",
    "title"       : "ETSI TS 103 280 (temp)",
    "description" : "A stubbed version of TS 103 280 definitions (need a CR to 280 for this)",
    "$defs": {
        "ShortString": {
          "type": "string",
          "maxLength": 255
        },
        "LongString": {
          "type": "string",
          "maxLength": 65535
        },
        "LIID": {
          "type": "string",
          "pattern": "^([!-~]{1,25})|([0-9a-f]{26,50})$"
        },
        "UTCDateTime": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$"
        },
        "UTCMicrosecondDateTime": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{6}Z$"
        },
        "QualifiedDateTime": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(Z|[+-][0-9]{2}:[0-9]{2})$"
        },
        "QualifiedMicrosecondDateTime": {
          "type": "string",
          "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{6}(Z|[+-][0-9]{2}:[0-9]{2})$"
        },
        "InternationalE164": {
          "type": "string",
          "pattern": "^[0-9]{1,15}$"
        },
        "IMSI": {
          "type": "string",
          "pattern": "^[0-9]{6,15}$"
        },
        "IMEI": {
          "type": "string",
          "pattern": "^[0-9]{14}$"
        },
        "IMEICheckDigit": {
          "type": "string",
          "pattern": "^[0-9]{15}$"
        },
        "IMEISV": {
          "type": "string",
          "pattern": "^[0-9]{16}$"
        },
        "IPv4Address": {
          "type": "string",
          "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])$"
        },
        "IPv4CIDR": {
          "type": "string",
          "pattern": "^((25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9]?[0-9])/([1-2]?[0-9]|3[0-2])$"
        },
        "IPv6Address": {
          "type": "string",
          "pattern": "^([0-9a-f]{4}:){7}([0-9a-f]{4})$"
        },
        "IPv6CIDR": {
          "type": "string",
          "pattern": "^([0-9a-f]{4}:){7}([0-9a-f]{4})/(([1-9][0-9]?)|(1[0-1][0-9])|(12[0-8]))$"
        },
        "TCPPort": {
          "type": "integer",
          "exclusiveMinimum": 1,
          "maximum": 65535
        },
        "UDPPort": {
          "type": "integer",
          "minimum": 0,
          "maximum": 65535
        },
        "MACAddress": {
          "type": "string",
          "pattern": "^([a-f0-9]{2}:){5}[a-f0-9]{2}$"
        },
        "EmailAddress": {
          "allOf": [
            {
              "$ref": "#/$defs/ShortString"
            },
            {
              "type": "string",
              "pattern": "^[a-zA-Z0-9\\.!#$%&'\\*\\+\\\\/=\\?\\^_`\\{\\|\\}~\\-]+@[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
            }
          ]
        },
        "UUID": {
          "type": "string",
          "pattern": "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"
        },
        "ISOCountryCode": {
          "type": "string",
          "pattern": "^[A-Z]{2}$"
        },
        "SIPURI": {
          "type": "string",
          "pattern": "^sips?:[a-zA-Z0-9!#$&-;=?-\\[\\]_~%]+$"
        },
        "TELURI": {
          "type": "string",
          "pattern": "^tel:[a-zA-Z0-9!#$&-;=?-\\[\\]_~%]+$"
        },
        "WGS84LatitudeDecimal": {
          "type": "string",
          "pattern": "^[NS][0-9]{2}\\.[0-9]{6}$"
        },
        "WGS84LongitudeDecimal": {
          "type": "string",
          "pattern": "^[EW][0-9]{3}\\.[0-9]{6}$"
        },
        "WGS84LatitudeAngular": {
          "type": "string",
          "pattern": "^[NS][0-9]{6}\\.[0-9]{2}$"
        },
        "WGS84LongitudeAngular": {
          "type": "string",
          "pattern": "^[EW][0-9]{7}\\.[0-9]{2}$"
        },
        "SUPIIMSI": {
          "$ref": "#/$defs/IMSI"
        },
        "SUPINAI": {
          "$ref": "#/$defs/NAI"
        },
        "SUCI": {
          "type": "string",
          "pattern": "^([a-fA-F0-9]{2})*$"
        },
        "PEIIMEI": {
          "$ref": "#/$defs/IMEI"
        },
        "PEIIMEICheckDigit": {
          "$ref": "#/$defs/IMEICheckDigit"
        },
        "PEIIMEISV": {
          "$ref": "#/$defs/IMEISV"
        },
        "GPSIMSISDN": {
          "type": "string",
          "pattern": "^[0-9]{1,15}$"
        },
        "GPSINAI": {
          "$ref": "#/$defs/NAI"
        },
        "NAI": {
          "type": "string"
        },
        "LDID": {
          "type": "string",
          "pattern": "^([A-Z]{2}-.+-.+)$"
        },
        "InternationalizedEmailAddress": {
          "allOf": [
            {
              "$ref": "#/$defs/ShortString"
            },
            {
              "type": "string",
              "pattern": "^.+@.+$"
            }
          ]
        },
        "EUI64": {
          "type": "string",
          "pattern": "^([a-f0-9]{2}:){7}[a-f0-9]{2}$"
        },
        "CGI": {
          "type": "string",
          "pattern": "^[0-9]{3}-[0-9]{2,3}-[a-f0-9]{4}-[a-f0-9]{4}$"
        },
        "ECGI": {
          "type": "string",
          "pattern": "^[0-9]{3}-[0-9]{2,3}-[a-f0-9]{7}$"
        },
        "NCGI": {
          "type": "string",
          "pattern": "^[0-9]{3}-[0-9]{2,3}-[a-f0-9]{9}$"
        },
        "ICCID": {
          "type": "string",
          "pattern": "^[0-9]{19,20}$"
        },
        "IPAddress": {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "IPv4Address": {
                  "$ref": "#/$defs/IPv4Address"
                }
              },
              "required": [
                "IPv4Address"
              ]
            },
            {
              "type": "object",
              "properties": {
                "IPv6Address": {
                  "$ref": "#/$defs/IPv6Address"
                }
              },
              "required": [
                "IPv6Address"
              ]
            }
          ]
        },
        "IPCIDR": {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "IPv4CIDR": {
                  "$ref": "#/$defs/IPv4CIDR"
                }
              },
              "required": [
                "IPv4CIDR"
              ]
            },
            {
              "type": "object",
              "properties": {
                "IPv6CIDR": {
                  "$ref": "#/$defs/IPv6CIDR"
                }
              },
              "required": [
                "IPv6CIDR"
              ]
            }
          ]
        },
        "TCPPortRange": {
          "type": "object",
          "properties": {
            "start": {
              "$ref": "#/$defs/TCPPort"
            },
            "end": {
              "$ref": "#/$defs/TCPPort"
            }
          },
          "required": [
            "start",
            "end"
          ]
        },
        "UDPPortRange": {
          "type": "object",
          "properties": {
            "start": {
              "$ref": "#/$defs/UDPPort"
            },
            "end": {
              "$ref": "#/$defs/UDPPort"
            }
          },
          "required": [
            "start",
            "end"
          ]
        },
        "Port": {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "TCPPort": {
                  "$ref": "#/$defs/TCPPort"
                }
              },
              "required": [
                "TCPPort"
              ]
            },
            {
              "type": "object",
              "properties": {
                "UDPPort": {
                  "$ref": "#/$defs/UDPPort"
                }
              },
              "required": [
                "UDPPort"
              ]
            }
          ]
        },
        "PortRange": {
          "oneOf": [
            {
              "type": "object",
              "properties": {
                "TCPPortRange": {
                  "$ref": "#/$defs/TCPPortRange"
                }
              },
              "required": [
                "TCPPortRange"
              ]
            },
            {
              "type": "object",
              "properties": {
                "UDPPortRange": {
                  "$ref": "#/$defs/UDPPortRange"
                }
              },
              "required": [
                "UDPPortRange"
              ]
            }
          ]
        },
        "IPAddressPort": {
          "type": "object",
          "properties": {
            "address": {
              "$ref": "#/$defs/IPAddress"
            },
            "port": {
              "$ref": "#/$defs/Port"
            }
          },
          "required": [
            "address",
            "port"
          ]
        },
        "IPAddressPortRange": {
          "type": "object",
          "properties": {
            "address": {
              "$ref": "#/$defs/IPAddress"
            },
            "portRange": {
              "$ref": "#/$defs/PortRange"
            }
          },
          "required": [
            "address",
            "portRange"
          ]
        },
        "WGS84CoordinateDecimal": {
          "type": "object",
          "properties": {
            "latitude": {
              "$ref": "#/$defs/WGS84LatitudeDecimal"
            },
            "longitude": {
              "$ref": "#/$defs/WGS84LongitudeDecimal"
            }
          },
          "required": [
            "latitude",
            "longitude"
          ]
        },
        "WGS84CoordinateAngular": {
          "type": "object",
          "properties": {
            "latitude": {
              "$ref": "#/$defs/WGS84LatitudeAngular"
            },
            "longitude": {
              "$ref": "#/$defs/WGS84LongitudeAngular"
            }
          },
          "required": [
            "latitude",
            "longitude"
          ]
        }
      }
} 
 No newline at end of file