|
1 | | -### Documentation coming |
| 1 | +# AppNet Element Specification Documentation |
| 2 | + |
| 3 | +An **AppNet element** defines the logic for an application network function (ANF) in a modular, programmable way. Elements process RPC requests and responses using match-action rules and can maintain typed local or shared state. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Element Structure |
| 8 | + |
| 9 | +Each element specification includes the following sections: |
| 10 | + |
| 11 | +### 1. `state` |
| 12 | +Defines local or shared, typed state variables. Maps and other types are supported. |
| 13 | + |
| 14 | +```plaintext |
| 15 | +state: |
| 16 | + firewall_table: Map<string, string> |
| 17 | +```` |
| 18 | +
|
| 19 | +### 2. `init()` |
| 20 | +
|
| 21 | +Optional initialization logic for state variables. |
| 22 | +
|
| 23 | +```appnet |
| 24 | +init(): |
| 25 | + set(firewall_table, 'test', 'DENY') |
| 26 | +``` |
| 27 | + |
| 28 | +### 3. `req(rpc)` |
| 29 | + |
| 30 | +Match-action rules for processing **RPC requests**. Match expressions can be nested, with pattern matching on `Some(...)` and `None` for optional types. |
| 31 | + |
| 32 | +```plaintext |
| 33 | +req(rpc): |
| 34 | + match get(acl, get(rpc, 'username')): |
| 35 | + Some(permission) => |
| 36 | + match permission: |
| 37 | + 'ALLOWED' => |
| 38 | + send(rpc, Down) |
| 39 | + 'DENY' => |
| 40 | + send(err('firewall'), Up) |
| 41 | + None => |
| 42 | + send(rpc, Down) |
| 43 | +``` |
| 44 | + |
| 45 | +### 4. `resp(rpc)` |
| 46 | + |
| 47 | +Logic for processing **RPC responses**. |
| 48 | + |
| 49 | +```plaintext |
| 50 | +resp(rpc): |
| 51 | + send(rpc, Up) |
| 52 | +``` |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Syntax Reference |
| 57 | + |
| 58 | +### Typed State Declaration |
| 59 | + |
| 60 | +```appnet |
| 61 | +state: |
| 62 | + variable_name: Type |
| 63 | +``` |
| 64 | + |
| 65 | +Examples: |
| 66 | + |
| 67 | +* `Map<string, string>` |
| 68 | +* `int`, `float`, `bool` |
| 69 | + |
| 70 | +### Pattern Matching with Option Types |
| 71 | + |
| 72 | +AppNet supports optional value semantics: |
| 73 | + |
| 74 | +```appnet |
| 75 | +match get(map, key): |
| 76 | + Some(value) => ... |
| 77 | + None => ... |
| 78 | +``` |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Common Actions |
| 83 | + |
| 84 | +* `send(rpc, Down)`: Forward the RPC to the next element. |
| 85 | +* `send(rpc, Up)`: Return the RPC to the previous element. |
| 86 | +* `send(err("msg"), Up)`: Send an error upstream. |
| 87 | +* `set(map, key, value)`: Write to a map or state variable. |
| 88 | +* `get(map, key)`: Read a value from state; returns `Some(v)` or `None`. |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Full Example: Application Firewall |
| 93 | + |
| 94 | +```appnet |
| 95 | +state: |
| 96 | + firewall_table: Map<string, string> |
| 97 | +
|
| 98 | +init(): |
| 99 | + set(firewall_table, 'test', 'No') |
| 100 | +
|
| 101 | +req(rpc): |
| 102 | + match get(acl, get(rpc, 'username')): |
| 103 | + Some(permission) => |
| 104 | + match permission: |
| 105 | + 'ALLOWED' => |
| 106 | + send(rpc, Down) |
| 107 | + 'DENY' => |
| 108 | + send(err('firewall'), Up) |
| 109 | + None => |
| 110 | + send(rpc, Down) |
| 111 | +
|
| 112 | +resp(rpc): |
| 113 | + send(rpc, Up) |
| 114 | +``` |
| 115 | + |
| 116 | +This firewall element checks a body field of the RPC against an access control list (`firewall_table`). If the value is `'ALLOWED'`, it allows the request. If `'DENY'`, it blocks the request. If there's no entry (`None`), the request is allowed by default. |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +## Runtime Behavior |
| 121 | + |
| 122 | +* **Down**: sends the request to the next element or the microservice. |
| 123 | +* **Up**: returns a message to the previous element. |
| 124 | + |
| 125 | +--- |
| 126 | + |
0 commit comments