|
| 1 | +# constraint_set schema |
| 2 | +# |
| 3 | +# { |
| 4 | +# "$schema": "http://json-schema.org/draft-04/schema#", |
| 5 | +# "description": "Describes a Constraint Set", |
| 6 | +# "title": "Constraint Set", |
| 7 | +# "type": "object", |
| 8 | +# "minProperties": 1, |
| 9 | +# "properties": { |
| 10 | +# "urn:x-nmos:cap:meta:label": { |
| 11 | +# "description": "Freeform string label for the Constraint Set", |
| 12 | +# "type": "string" |
| 13 | +# }, |
| 14 | +# "urn:x-nmos:cap:meta:preference": { |
| 15 | +# "description": "This value expresses the relative 'weight' that the Receiver assigns to |
| 16 | +# its preference for the streams satisfied by the associated Constraint Set. |
| 17 | +# The weight is an integer in the range -100 through 100, |
| 18 | +# where -100 is least preferred and 100 is most preferred. |
| 19 | +# When the attribute is omitted, the effective value for the associated Constraint Set is 0.", |
| 20 | +# "type": "integer", |
| 21 | +# "default": 0, |
| 22 | +# "maximum": 100, |
| 23 | +# "minimum": -100 |
| 24 | +# }, |
| 25 | +# "urn:x-nmos:cap:meta:enabled": { |
| 26 | +# "description": "This value indicates whether a Constraint Set is available to use immediately (true) |
| 27 | +# or whether this is an offline capability which can be activated via |
| 28 | +# some unspecified configuration mechanism (false). |
| 29 | +# When the attribute is omitted its value is assumed to be true.", |
| 30 | +# "type": "boolean", |
| 31 | +# "default": true |
| 32 | +# } |
| 33 | +# }, |
| 34 | +# "patternProperties": { |
| 35 | +# "^urn:x-nmos:cap:(?!meta:)": { |
| 36 | +# "$ref": "param_constraint.json" |
| 37 | +# } |
| 38 | +# } |
| 39 | +# } |
| 40 | +# |
| 41 | +# We want to compare that two constraint sets are equal based on the properties of teh schema that allow default |
| 42 | +# values for properties not defined. For example the "preference" property defined to true or undefined is the |
| 43 | +# same such that comparing an object A heving the property set to true and an object B not having the property |
| 44 | +# defined will indicate equality because the schema defines a default value. |
| 45 | +# |
| 46 | +# This function verifies two constraint sets where at most one constraint set is expected and each constraint |
| 47 | +# set must have at most one "sample_rate" paremeter constraint. We then compare the two objects based on their |
| 48 | +# respective schemas: constraint_set and rational. We expect the constraint to be defined using the "enum" |
| 49 | +# keyword with a single array entry. |
| 50 | +# |
| 51 | +# return true if equal, false otherwise |
| 52 | + |
| 53 | + |
| 54 | +def compare_complex_sample_rate_constraint( |
| 55 | + response_constraints, sample_rate_constraints |
| 56 | +): |
| 57 | + |
| 58 | + # NOTE: We already know that both response_constraints, sample_rate_constraints are valid |
| 59 | + # and have been each independently been validated against the schemas. We only check equality. |
| 60 | + |
| 61 | + # Each constraint_sets array must have a single entry |
| 62 | + if len(response_constraints) != 1 or len(sample_rate_constraints) != 1: |
| 63 | + return False |
| 64 | + |
| 65 | + # If the sample_rate property is not defined, objects are not equivalent |
| 66 | + try: |
| 67 | + response_constraints_enum = response_constraints["constraint_sets"][0][ |
| 68 | + "urn:x-nmos:cap:format:sample_rate" |
| 69 | + ]["enum"] |
| 70 | + except Exception: |
| 71 | + return False |
| 72 | + |
| 73 | + # If the sample_rate property is not defined, objects are not equivalent |
| 74 | + try: |
| 75 | + sample_rate_constraints_enum = sample_rate_constraints["constraint_sets"][0][ |
| 76 | + "urn:x-nmos:cap:format:sample_rate" |
| 77 | + ]["enum"] |
| 78 | + except Exception: |
| 79 | + return False |
| 80 | + |
| 81 | + # There must be a single entry in the enum array |
| 82 | + if len(response_constraints_enum) != 1 or len(sample_rate_constraints_enum) != 1: |
| 83 | + return False |
| 84 | + |
| 85 | + try: |
| 86 | + response_numerator = response_constraints_enum[0]["numerator"] |
| 87 | + response_denominator = 1 |
| 88 | + |
| 89 | + if "denominator" in response_constraints_enum[0]: |
| 90 | + response_denominator = response_constraints_enum[0]["denominator"] |
| 91 | + |
| 92 | + sample_rate_numerator = sample_rate_constraints_enum[0]["numerator"] |
| 93 | + sample_rate_denominator = 1 |
| 94 | + |
| 95 | + if "denominator" in sample_rate_constraints_enum[0]: |
| 96 | + sample_rate_denominator = sample_rate_constraints_enum[0]["denominator"] |
| 97 | + |
| 98 | + if ( |
| 99 | + response_numerator != sample_rate_numerator |
| 100 | + or response_denominator != sample_rate_denominator |
| 101 | + ): |
| 102 | + return False |
| 103 | + except Exception: |
| 104 | + return False |
| 105 | + |
| 106 | + # There must be no other patternProperties |
| 107 | + for prop in sample_rate_constraints["constraint_sets"][0]: |
| 108 | + if ( |
| 109 | + prop != "urn:x-nmos:cap:format:sample_rate" |
| 110 | + and prop != "urn:x-nmos:cap:meta:enabled" |
| 111 | + and prop != "urn:x-nmos:cap:meta:preference" |
| 112 | + ): |
| 113 | + return False |
| 114 | + |
| 115 | + for prop in response_constraints["constraint_sets"][0]: |
| 116 | + if ( |
| 117 | + prop != "urn:x-nmos:cap:format:sample_rate" |
| 118 | + and prop != "urn:x-nmos:cap:meta:enabled" |
| 119 | + and prop != "urn:x-nmos:cap:meta:preference" |
| 120 | + ): |
| 121 | + return False |
| 122 | + |
| 123 | + # Check meta:enabled considering default values |
| 124 | + response_enabled = True |
| 125 | + if "urn:x-nmos:cap:meta:enabled" in response_constraints["constraint_sets"][0]: |
| 126 | + response_enabled = response_constraints["constraint_sets"][0][ |
| 127 | + "urn:x-nmos:cap:meta:enabled" |
| 128 | + ] |
| 129 | + |
| 130 | + sample_rate_enabled = True |
| 131 | + if "urn:x-nmos:cap:meta:enabled" in sample_rate_constraints["constraint_sets"][0]: |
| 132 | + sample_rate_enabled = sample_rate_constraints["constraint_sets"][0][ |
| 133 | + "urn:x-nmos:cap:meta:enabled" |
| 134 | + ] |
| 135 | + |
| 136 | + if response_enabled != sample_rate_enabled: |
| 137 | + return False |
| 138 | + |
| 139 | + # Check meta:preference considering default values |
| 140 | + response_preference = 0 |
| 141 | + if "urn:x-nmos:cap:meta:preference" in response_constraints["constraint_sets"][0]: |
| 142 | + response_preference = response_constraints["constraint_sets"][0][ |
| 143 | + "urn:x-nmos:cap:meta:preference" |
| 144 | + ] |
| 145 | + |
| 146 | + sample_rate_preference = 0 |
| 147 | + if ( |
| 148 | + "urn:x-nmos:cap:meta:preference" |
| 149 | + in sample_rate_constraints["constraint_sets"][0] |
| 150 | + ): |
| 151 | + sample_rate_preference = sample_rate_constraints["constraint_sets"][0][ |
| 152 | + "urn:x-nmos:cap:meta:preference" |
| 153 | + ] |
| 154 | + |
| 155 | + if response_preference != sample_rate_preference: |
| 156 | + return False |
| 157 | + |
| 158 | + # If we get here it is because the two objects are equal |
| 159 | + return True |
| 160 | + |
| 161 | + |
| 162 | +def compare_complex_grain_rate_constraint(response_constraints, grain_rate_constraints): |
| 163 | + |
| 164 | + # NOTE: We already know that both response_constraints, grain_rate_constraints are valid |
| 165 | + # and have been each independently been validated against the schemas. We only check equality. |
| 166 | + |
| 167 | + # Each constraint_sets array must have a single entry |
| 168 | + if len(response_constraints) != 1 or len(grain_rate_constraints) != 1: |
| 169 | + return False |
| 170 | + |
| 171 | + # If the grain_rate property is not defined, objects are not equivalent |
| 172 | + try: |
| 173 | + response_constraints_enum = response_constraints["constraint_sets"][0][ |
| 174 | + "urn:x-nmos:cap:format:grain_rate" |
| 175 | + ]["enum"] |
| 176 | + except Exception: |
| 177 | + return False |
| 178 | + |
| 179 | + # If the grain_rate property is not defined, objects are not equivalent |
| 180 | + try: |
| 181 | + grain_rate_constraints_enum = grain_rate_constraints["constraint_sets"][0][ |
| 182 | + "urn:x-nmos:cap:format:grain_rate" |
| 183 | + ]["enum"] |
| 184 | + except Exception: |
| 185 | + return False |
| 186 | + |
| 187 | + # There must be a single entry in the enum array |
| 188 | + if len(response_constraints_enum) != 1 or len(grain_rate_constraints_enum) != 1: |
| 189 | + return False |
| 190 | + |
| 191 | + try: |
| 192 | + response_numerator = response_constraints_enum[0]["numerator"] |
| 193 | + response_denominator = 1 |
| 194 | + |
| 195 | + if "denominator" in response_constraints_enum[0]: |
| 196 | + response_denominator = response_constraints_enum[0]["denominator"] |
| 197 | + |
| 198 | + grain_rate_numerator = grain_rate_constraints_enum[0]["numerator"] |
| 199 | + grain_rate_denominator = 1 |
| 200 | + |
| 201 | + if "denominator" in grain_rate_constraints_enum[0]: |
| 202 | + grain_rate_denominator = grain_rate_constraints_enum[0]["denominator"] |
| 203 | + |
| 204 | + if ( |
| 205 | + response_numerator != grain_rate_numerator |
| 206 | + or response_denominator != grain_rate_denominator |
| 207 | + ): |
| 208 | + return False |
| 209 | + except Exception: |
| 210 | + return False |
| 211 | + |
| 212 | + # There must be no other patternProperties |
| 213 | + for prop in grain_rate_constraints["constraint_sets"][0]: |
| 214 | + if ( |
| 215 | + prop != "urn:x-nmos:cap:format:grain_rate" |
| 216 | + and prop != "urn:x-nmos:cap:meta:enabled" |
| 217 | + and prop != "urn:x-nmos:cap:meta:preference" |
| 218 | + ): |
| 219 | + return False |
| 220 | + |
| 221 | + for prop in response_constraints["constraint_sets"][0]: |
| 222 | + if ( |
| 223 | + prop != "urn:x-nmos:cap:format:grain_rate" |
| 224 | + and prop != "urn:x-nmos:cap:meta:enabled" |
| 225 | + and prop != "urn:x-nmos:cap:meta:preference" |
| 226 | + ): |
| 227 | + return False |
| 228 | + |
| 229 | + # Check meta:enabled considering default values |
| 230 | + response_enabled = True |
| 231 | + if "urn:x-nmos:cap:meta:enabled" in response_constraints["constraint_sets"][0]: |
| 232 | + response_enabled = response_constraints["constraint_sets"][0][ |
| 233 | + "urn:x-nmos:cap:meta:enabled" |
| 234 | + ] |
| 235 | + |
| 236 | + grain_rate_enabled = True |
| 237 | + if "urn:x-nmos:cap:meta:enabled" in grain_rate_constraints["constraint_sets"][0]: |
| 238 | + grain_rate_enabled = grain_rate_constraints["constraint_sets"][0][ |
| 239 | + "urn:x-nmos:cap:meta:enabled" |
| 240 | + ] |
| 241 | + |
| 242 | + if response_enabled != grain_rate_enabled: |
| 243 | + return False |
| 244 | + |
| 245 | + # Check meta:preference considering default values |
| 246 | + response_preference = 0 |
| 247 | + if "urn:x-nmos:cap:meta:preference" in response_constraints["constraint_sets"][0]: |
| 248 | + response_preference = response_constraints["constraint_sets"][0][ |
| 249 | + "urn:x-nmos:cap:meta:preference" |
| 250 | + ] |
| 251 | + |
| 252 | + grain_rate_preference = 0 |
| 253 | + if "urn:x-nmos:cap:meta:preference" in grain_rate_constraints["constraint_sets"][0]: |
| 254 | + grain_rate_preference = grain_rate_constraints["constraint_sets"][0][ |
| 255 | + "urn:x-nmos:cap:meta:preference" |
| 256 | + ] |
| 257 | + |
| 258 | + if response_preference != grain_rate_preference: |
| 259 | + return False |
| 260 | + |
| 261 | + # If we get here it is because the two objects are equal |
| 262 | + return True |
0 commit comments