Slice notifications
A slice may take a significant amount of time (minutes) to be set up. So, after creating a slice, activating it, attaching or detaching devices, which was done in previous steps, we will configure a web server to receive slice-status notifications. This will allow you to know when a slice is ready to be configured as desired. Then, slice operations like activation, deactivation and deletion can be done based on its current status notifications.
Managing slice-status notifications
The following SDK will create different handlers that will inform you
when a slice has been built and is AVAILABLE
(but not active yet)
or OPERATING
(active and serving users).
Within the first availability handler,
there will be a condition to handle slices if they are being deleted,
which happens when the slice.deactivate()
is called.
In any other case, an available slice will be activated automatically,
through the slice.activate()
method, and move it on to the OPERATING
stage.
If all is right, through the OPERATING
status handler,
the "Slice is active"
message should be printed out.
Then, simply enable the slice.deactivate()
method, in case you wish to deactivate it.
Keep in mind: Slices need to be deactivated before deletion. It's also not possible to delete an active slice (with
OPERATING
status). So, if you plan to delete one, then call theslice.deactivate()
method. This means the slice will go through a retiring process, before it's permanently deleted.
Slice-status notifications SDK
import time
from fastapi import FastAPI, Header
from pydantic import BaseModel
from typing_extensions import Annotated
from typing import Union
import network_as_code as nac
from network_as_code.models.device import Device, DeviceIpv4Addr
from network_as_code.models.slice import Point, AreaOfService, NetworkIdentifier, SliceInfo
client = nac.NetworkAsCodeClient(...)
device = client.devices.get(...)
slice = client.slices.create(...)
...
# Our web server for receiving notifications
app = FastAPI()
class Notification(BaseModel):
resource: str
action: str
state: str
# We'll keep track of when we are retiring the slice between the notifications
retiring = False
@app.post("/notifications")
def receive_notification(notification: Notification,
authorization: Annotated[Union[str, None], Header]):
if authorization == "Bearer my-token":
slice = client.slices.get(notification.resource)
# Handler for when the slice has been built
if notification.state == "AVAILABLE":
# If we are deactivating the slice, we will receive notification here too
if retiring:
# Deactivated slices can be deleted
slice.delete()
else:
slice.activate()
# Handler for when the slice has been activated
elif notification.state == "OPERATING":
print("Slice is active")
# Activated slices can be deactivated
slice.deactivate()
global retiring
retiring = True
What is a notification URL?
Learn more about the notification URL/auth token and how to create a web server for them.
Note that the snippets above assume you have already created a slice before, which you can learn how to do here. It also implies that you have already created a Network-as-Code client and identified your mobile network device.