> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tposs/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/tposs/latest/configuration/hooks.md).

# Hooks configuration

Hooks allow TPOSS to send notifications to a pre-configured endpoint when specific events occur. You can enable or disable each such hook individually by setting the **hooks.{HOOK}.enabled** configuration parameter to `true` or `false`.

The following hooks are supported:

| Hook | Description |
| --- | --- |
| `hooks.balance_volume_threshold` | Hook is triggered when the configured consumption threshold for a volume is reached. |
| `hooks.charging_rules_changed` | Hook is triggered when the subscriber's charging rules change. |
| `hooks.mccmnc_changed` | Hook is triggered when the Mobile Country Code (MCC) or Mobile Network Code (MNC) changes. |
| `hooks.network_attachment.first` | Hook is triggered upon the first attachment to the network. |
| `hooks.visited_country_changed` | Hook is triggered when a visited country changes. |

## Events

All the hooks described above are triggered by a different event:

- [Balance threshold event](#balance-threshold-event)
- [Charging rules change event](#charging-rules-change-event)
- [MCC/MNC change event](#mccmnc-change-event)
- [First network attachment event](#first-network-attachment-event)
- [Visited country change event](#visited-country-change-event)

When an event occurs, TPOSS sends an HTTP call to the pre-configured HTTP endpoint. The endpoint is specified by the **hooks.{HOOK}.url** configuration parameter.

If the outgoing HTTP request fails, TPOSS may try to resend it. You can control this behaviour using the **hooks.{HOOK}.attempts** configuration parameter of each hook. To avoid the storm of requests, the **hooks.{HOOK}.delay** configuration option exists. It defines the delay (in milliseconds) between retries to send HTTP requests.

### Balance threshold event

The `balance threshold` event is triggered when a subscriber's balance reaches a specified threshold. The threshold is defined by the **hooks.balance_volume_threshold.level** parameter, which can be set to a value between `0.0` and `1.0`. This represents the percentage of the balance consumed before the event is triggered. You can specify multiple threshold values separated by a comma.

See the configuration example:

```yaml
hooks:
  balance_volume_threshold:
    # Enables the balance volume threshold notifications.
    enabled: true
    # Name of the event that triggered the hook
    event: tposs/charging/balance/updated
    # Level of used balance when a notification is sent
    level: 0.8
    # Number of attempts for resending failed balance volume threshold notifications
    attempts: 3
    # Delay between attempts in milliseconds
    delay: 300
    # Endpoint for sending notifications
    url: http://localhost:8080/test/consumer
```

The content of the outgoing HTTP notification is as follows:

```json
{
  "volumeThreshold":80.0,
  "usedOctets":800,
  "totalOctets":1000,
  "event":"tposs/charging/balance/updated",
  "date":"2017-03-14T10:04:25",
  "balanceId":"a7bc0e7c-be04-4140-8008-c6ab2ef31dee"
}
```

### Charging rules change event

The `charging rules change` event is triggered when there is a change of a service session for the given subscriber. By default, TPOSS sends a notification to all subscribers. TPOSS may also send a notification only for subscribers with the specified mobile country codes. To achieve this, use the **hooks.charging_rules_changed.mcc_whitelist** configuration option. It contains a list of Mobile Country Codes (MCCs) separated by a comma for which the `Charging Rules Change` event is triggered.

See the configuration example:

```yaml
hooks:
  charging_rules_changed:
    # Enables the charging rules change notifications.
    enabled: true
    # Name of the event that triggered the hook
    event: tposs/charging_rules/changed
    # Number of attempts for resending failed charging rules change notifications
    attempts: 3
    # Delay between attempts in milliseconds
    delay: 300
    # Endpoint for sending notifications
    url: http://localhost:8080/test/consumer
    # MCCs for which the event is triggered
    mcc_whitelist: "262"
```

The content of the outgoing HTTP notification is as follows:

```json
{
  "event": "tposs/charging_rules/changed",
  "subscriber_name": "112233@example.net",
  "date": "2017-03-10T09:22:00Z",
  "imsi": "262019876543210",
  "currentChargingRules": ["Online"],
  "previousChargingRules": ["Portal"],
  "id": "Session-Id",
  "success": true
}
```

### MCC/MNC change event

The `MCC/MNC change` event is triggered when a subscriber’s MCC or MNC changes during the handling of RADIUS Accounting requests. TPOSS checks the value of the `3GPP-SGSN-MCC-MNC` RADIUS AVP when handling each RADIUS Accounting-Interim-Request, and compares it with the MCC/MNC in the current network sessions. If they are different, TPOSS sends a notification.

See the configuration example:

```yaml
hooks:
  mccmnc_changed:
    # Enables the MCC/MNC change notifications.
    enabled: true
    # Name of the event that triggered the hook
    event: tposs/location/mccmnc/updated
    # Number of attempts for resending failed MCC/MNC notifications
    attempts: 3
    # Delay between attempts in milliseconds
    delay: 300
    # Endpoint for sending notifications
    url: http://localhost:8080/test/consumer
```

The content of the outgoing HTTP notification is as follows:

```json
{
  "event": "tposs/location/mccmnc/changed",
  "subscriberId": "112233@cennso.net",
  "date": "2017-03-10T09:22:00Z",
  "currentMCCMNC": "26201",
  "previousMCCMNC": "26203"
}
```

### First network attachment event

The `first network attachment` event is triggered when TPOSS receives a RADIUS Access-Request and the IMSI from the request is attached to the network for the first time.

See the configuration example:

```yaml
hooks:
  network_attachment:
    first:
      # Enables the first network attachment notifications.
      enabled: true
      # Name of the event that triggered the hook
      event: tposs/network/attachment/first
      # Endpoint for sending notifications
      url: http://localhost:8080/test/consumer
      # Number of attempts for resending failed first network attachment notifications
      attempts: 3
      # Delay between attempts in milliseconds
      delay: 300
```

The content of the outgoing HTTP notification is as follows:

```json
{
  "event": "tposs/network/attachment/first",
  "imsi": "310170845466094",
  "imeisv": "3520990017614887",
  "imei": "352099001761481",
  "sgsnMCCMNC": "26201",
  "date": "2017-03-10T09:22:00Z"
}
```

All fields in the outgoing HTTP notification are mandatory, except for `imeisv` and `imei`. If the network session was created without an IMEISV (based on a RADIUS Access-Request or Accounting-Request without the `3GPP-IMEISV` AVP), the values of these attributes are skipped in the notification payload.

### Visited country change event

The `visited country change` event is triggered when there is a change of subscriber's visited country during the handling of RADIUS Accounting Requests.

See the configuration example:

```yaml
hooks:
  visited_country_changed:
    # Enables the visited country changed notification.
    enabled: true
    # Name of the event that triggered the hook
    event: tposs/location/visitedCountry/updated
    # Number of attempts for resending failed visited country changed notifications
    attempts: 3
    # Delay between attempts in milliseconds
    delay: 300
    # Endpoint for sending notifications
    url: http://localhost:8080/test/consumer
```

The content of the outgoing HTTP notification is as follows:

```json
{
  "event": "tposs/location/visitedCountry/changed",
  "subscriberId": "112233@cennso.net",
  "date": "2017-03-10T09:22:00Z",
  "currentCountry": "DE",
  "previousCountry": "FR"
}
```
