|
| 1 | +# Library Architecture |
| 2 | + |
| 3 | +|  | |
| 4 | +|:---:| |
| 5 | +| **`jdiff` architecture** | |
| 6 | + |
| 7 | +We use the `CheckType` factory class method `create` along with the specified check type to instantiate a concrete object of the specified `CheckType` class. |
| 8 | + |
| 9 | +```python |
| 10 | +from jdiff import CheckType, extract_data_from_json |
| 11 | +check = CheckType.create("exact_match") |
| 12 | +>>> <jdiff.check_types.ExactMatchType at 0x10a618e80> |
| 13 | +``` |
| 14 | + |
| 15 | +- `exact_match` |
| 16 | +- `tolerance` |
| 17 | +- `parameter_match` |
| 18 | +- `regex` |
| 19 | +- `operator` |
| 20 | + |
| 21 | + |
| 22 | +Next, load a JSON object as reference data. as well as a JMESPath expression to extract the values wanted and pass them to `extract_data_from_json` method. Be aware! `jdiff` works with a customized version of JMESPath. More on that [below](#customized-jmespath). |
| 23 | + |
| 24 | +> We'll use data from the tests folder. We assume the current working directory is the root of the repo if you've cloned it. |
| 25 | +
|
| 26 | +```python |
| 27 | +>>> bgp_reference_state = json.load(open("tests/mock/napalm_get_bgp_neighbors/pre.json")) |
| 28 | +>>> bgp_jmspath_exp = "global.$peers$.*.*.ipv4.[accepted_prefixes,received_prefixes,sent_prefixes]" |
| 29 | +>>> bgp_reference_value = extract_data_from_json(bgp_reference_state, bgp_jmspath_exp) |
| 30 | +``` |
| 31 | + |
| 32 | +Once the pre-change values are extracted, we would need to evaluate them against our post-change value. In the case of check-type `exact_match`, our post value would be another JSON object: |
| 33 | + |
| 34 | +```python |
| 35 | +>>> bgp_comparison_state = json.load(open("tests/mock/napalm_get_bgp_neighbors/post.json")) |
| 36 | +>>> bgp_comparison_value = extract_data_from_json(bgp_comparison_state, bgp_jmspath_exp) |
| 37 | +``` |
| 38 | + |
| 39 | +Each check type expects different types of arguments based on how and what they are checking. For example: check type `tolerance` needs a `tolerance` argument, whereas `parameter_match` expects a dictionary. |
| 40 | + |
| 41 | +Now that we have pre and post data, we use the `evaluate` method to compare them, which will return our evaluation result. |
| 42 | + |
| 43 | +```python |
| 44 | +>>> check = CheckType.create(check_type="exact_match") |
| 45 | +>>> results = check.evaluate(bgp_reference_value, bgp_comparison_value) |
| 46 | +>>> results |
| 47 | +({'10.1.0.0': {'accepted_prefixes': {'new_value': 900, 'old_value': 1000}, |
| 48 | + 'received_prefixes': {'new_value': 999, 'old_value': 1000}, |
| 49 | + 'sent_prefixes': {'new_value': 1011, 'old_value': 1000}}}, |
| 50 | + False) |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +## Arguments |
| 55 | + |
| 56 | +Generally, for all of the `CheckTypes`, the arguments will be in the order `intended state`, `actual state`, `options`. For instance, the reference state would be the first argument and comparison state the second argument in the `exact_match` check type. |
| 57 | + |
| 58 | +For regex or parameter matching, your provided regex or dictionary would be the first argument and the collected data would be the second argument. |
| 59 | + |
| 60 | +# Customized JMESPath |
| 61 | + |
| 62 | +Since `jdiff` works with JSON objects as data inputs, JMESPath was the obvious choice for traversing the data and extracting the value(s) to compare. However, JMESPath has a limitation where context is lost for the values it collects, in other words, for each given value that JMESPath returns, we cannot be sure what key it was part of. |
| 63 | + |
| 64 | +Below is the output of `show bgp`. |
| 65 | + |
| 66 | +```python |
| 67 | +>>> data = { |
| 68 | + "result": [ |
| 69 | + { |
| 70 | + "vrfs": { |
| 71 | + "default": { |
| 72 | + "peerList": [ |
| 73 | + { |
| 74 | + "linkType": "external", |
| 75 | + "localAsn": "65130.1100", |
| 76 | + "prefixesSent": 50, |
| 77 | + "receivedUpdates": 0, |
| 78 | + "peerAddress": "7.7.7.7", |
| 79 | + "state": "Idle", |
| 80 | + "updownTime": 1394, |
| 81 | + "asn": "1.2354", |
| 82 | + "routerId": "0.0.0.0" |
| 83 | + }, |
| 84 | + { |
| 85 | + "linkType": "external", |
| 86 | + "localAsn": "65130.1100", |
| 87 | + "receivedUpdates": 0, |
| 88 | + "peerAddress": "10.1.0.0", |
| 89 | + "state": "Connected", |
| 90 | + "updownTime": 1394, |
| 91 | + "asn": "1.2354", |
| 92 | + "routerId": "0.0.0.0" |
| 93 | + } |
| 94 | + ] |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | +} |
| 100 | +``` |
| 101 | +A JMESPath expression to extract `state` is shown below. |
| 102 | + |
| 103 | +```python |
| 104 | +>>> path = result[0].vrfs.default.peerList[*].state |
| 105 | +``` |
| 106 | + |
| 107 | +...which will return |
| 108 | + |
| 109 | +```python |
| 110 | +>>> extract_data_from_json(data, path) |
| 111 | +["Idle", "Connected"] |
| 112 | +``` |
| 113 | + |
| 114 | +How can we understand that `Idle` is relative to peer 7.7.7.7 and `Connected` to peer `10.1.0.0`? |
| 115 | +We could index the output, but that would require some post-processing of the data. For that reason, `jdiff` uses a customized version of JMESPath where it is possible to define a reference key for the value(s) wanted. The reference key must be within `$` sign anchors and defined in a list, together with the value(s): |
| 116 | + |
| 117 | +```python |
| 118 | +>>> path = "result[0].vrfs.default.peerList[*].[$peerAddress$,state]" |
| 119 | +``` |
| 120 | + |
| 121 | +That would give us... |
| 122 | + |
| 123 | +```python |
| 124 | +>>> extract_data_from_json(pre,path) |
| 125 | +[{'7.7.7.7': {'state': 'Idle'}}, {'10.1.0.0': {'state': 'Connected'}}] |
| 126 | +``` |
0 commit comments