-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.py
260 lines (199 loc) · 9.58 KB
/
router.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""Router
Copyright (c) 2023 - S2N Lab (https://s2n.cnit.it/)
"""
import fastapi
import time
import requests
from threading import Thread
from fastapi import APIRouter
from pydantic import BaseModel
from typing import Union
from models import Free5gck8sBlueCreateModel, MiniFree5gcModel, OnboardModel, \
InstantiateModel, NsInstance, NsLcmOpOcc
from fiveTonicRestApi import FiveTonicRestApi
from utils import create_logger
fiveTonicParametersFile = "fivetonichost.txt"
logger = create_logger("Router")
northRouter = APIRouter(
prefix="",
tags=["NorthRouter"],
responses={404: {"description": "Not found"}}
)
try:
with open(fiveTonicParametersFile, "r") as f:
fiveTonicHost = f.readline().split("\n")[0]
fiveTonicPort = f.readline().split("\n")[0]
f.close()
logger.info("Read from file (\"{}\"): Athonet host-port: {} - {}".format(fiveTonicParametersFile, fiveTonicHost, fiveTonicPort))
except Exception as e:
logger.error("Impossible to read the file \"{}\"".format(fiveTonicParametersFile))
raise ValueError("Impossible to read the file \"{}\"".format(fiveTonicParametersFile))
fiveTonicInterface = FiveTonicRestApi(fiveTonicHost, fiveTonicPort)
class RestAnswer202(BaseModel):
#id: str
description: str = "operation submitted"
status: str = "submitted"
def restCallback(callback, requestedOperation, blueId, sessionId, status):
if callback:
logger.info("Generating callback message")
headers = {
"Content-type": "application/json",
"Accept": "application/json"
}
data = {
"blueprint":
{
"id": blueId,
"type": "Free5GC_K8s"
},
"requested_operation": requestedOperation,
"session_id": blueId,
"status": status
}
r = None
try:
r = requests.post(callback, json=data, params=None, verify=False, stream=True, headers=headers)
return r
except Exception as e:
logger.error("Error - posting callback: ", e)
else:
logger.info("No callback message is specified")
return None
def getSlice(free5gcMessage: Union[Free5gck8sBlueCreateModel, MiniFree5gcModel]) -> NsInstance:
nsList = fiveTonicInterface.nsQueryList()
nsName = "{}{}".format(
free5gcMessage.config.sliceProfiles[0].sliceType, free5gcMessage.config.sliceProfiles[0].sliceId
)
ns = next((item for item in nsList if item.nsInstanceName == nsName), None)
return ns
def getLcmOpOcc(free5gcMessage: Union[Free5gck8sBlueCreateModel, MiniFree5gcModel]) -> NsLcmOpOcc:
nsLcmOpOccList = fiveTonicInterface.nsLcmOpOccQueryList()
slice = getSlice(free5gcMessage)
if not slice or not nsLcmOpOccList:
raise ValueError("Slice not exist")
return next((item for item in nsLcmOpOccList if item.nsInstanceId == slice.id), None)
def instantiateSlice(free5gcMessage: Union[Free5gck8sBlueCreateModel, MiniFree5gcModel], id, instantiateObject, blueId, requestedOperation):
# check if slice is onboarded and then instantiate it
partialTimer = 0
totalTime = 600 # 5 minutes in seconds
clock = 2 # in seconds
callback = free5gcMessage.callbackURL
try:
ns = getSlice(free5gcMessage)
fiveTonicInterface.nsInstantiate(ns.id, instantiateObject)
while True:
time.sleep(clock)
ns = getSlice(free5gcMessage)
logger.info("NS: {}".format(ns))
if ns and ns.nsState.upper() == "INSTANTIATED":
logger.info("Instantiated")
if callback != "":
restCallback(callback, requestedOperation, blueId, blueId, "ready")
return
else:
logger.info("wait {} seconds...".format(clock))
time.sleep(clock)
partialTimer += clock
if partialTimer == totalTime:
logger.info("Operation timed out")
if callback != "":
restCallback(callback, requestedOperation, blueId, blueId, "failed")
return
except Exception as e:
logger.warn("Impossible to delete the slice: {}".format(e))
if callback != "":
restCallback(callback, requestedOperation, blueId, blueId, "failed")
raise fastapi.HTTPException(status_code=404, detail="Impossible to delete the slice: {}".format(e))
def deleteSlice(free5gcMessage: Union[Free5gck8sBlueCreateModel, MiniFree5gcModel], blueId, requestedOperation):
# check if slice is terminated and then delete it
partialTimer = 0
totalTime = 600 # 5 minutes in seconds
clock = 2 # in seconds
callback = free5gcMessage.callbackURL
try:
while True:
nsLcmOpOcc = getLcmOpOcc(free5gcMessage)
if not nsLcmOpOcc:
logger.warn("the status of the slice is empty")
raise ValueError("the status of the slice is empty")
logger.info("nsLcmOpOcc: {}".format(nsLcmOpOcc))
if nsLcmOpOcc.operationState.upper() == "PROCESSING" \
or nsLcmOpOcc.operationState.upper() == "ROLLING_BACK":
logger.info("Slice in state: {} . Waiting...".format(nsLcmOpOcc.operationState))
time.sleep(clock)
partialTimer += clock
if partialTimer == totalTime:
logger.info("Operation timed out (1)")
if callback != "":
restCallback(callback, requestedOperation, blueId, blueId, "failed")
return
else:
while True:
ns = getSlice(free5gcMessage)
logger.info("NS: {}".format(ns))
if ns and ns.nsState.upper() == "NOT_INSTANTIATED":
logger.info("Removing of slice")
fiveTonicInterface.nsDelete(ns.id)
if callback != "":
restCallback(callback, requestedOperation, blueId, blueId, "ready")
return
else:
logger.info("wait {} seconds...".format(clock))
time.sleep(clock)
partialTimer += clock
if partialTimer == totalTime:
logger.info("Operation timed out (2)")
if callback != "":
restCallback(callback, requestedOperation, blueId, blueId, "failed")
return
except Exception as e:
logger.warn("Impossible to delete the slice: {}".format(e))
if callback != "":
restCallback(callback, requestedOperation, blueId, blueId, "failed")
raise fastapi.HTTPException(status_code=404, detail="Impossible to delete the slice: {}".format(e))
@northRouter.post("/nfvcl/v1/api/blue/Free5GC_K8s/{blue_id}/add_slice", response_model=RestAnswer202)
async def addSlice(free5gcMessage: Union[Free5gck8sBlueCreateModel, MiniFree5gcModel], blue_id: str):
onBoardObject = OnboardModel.fromFree5GcModel(free5gcMessage)
instantiateObject = InstantiateModel.fromFree5GcModel(free5gcMessage)
logger.info("OnboardModel instance: {}".format(onBoardObject))
logger.info("InstantiateModel instance: {}".format(instantiateObject))
try:
# onboarding
ns = fiveTonicInterface.nsOnboard(onBoardObject)
#if ns.nsState == "NOT_INSTANTIATED":
# logger.warn("slice not instantiated")
# raise ValueError("slice not instantiated")
#instantiate
thread = Thread(target=instantiateSlice, args=(free5gcMessage, ns.id, instantiateObject, blue_id, "add_slice",))
thread.start()
return RestAnswer202()
except Exception as e:
logger.warn("Impossible to create the slice: {}".format(e))
raise fastapi.HTTPException(status_code=404, detail="Impossible to create the slice: {}".format(e))
@northRouter.delete("/nfvcl/v1/api/blue/Free5GC_K8s/{blue_id}/del_slice", response_model=RestAnswer202)
async def delSlice(free5gcMessage: Union[Free5gck8sBlueCreateModel, MiniFree5gcModel], blue_id: str):
try:
# get the slice from 5Tonic
ns = getSlice(free5gcMessage)
if not ns:
logger.warn("Slice not found")
raise ValueError("Slice not found")
# terminate the slice (asynchronouns API)
fiveTonicInterface.nsTerminate(ns.id)
# launch a thread to check when the slice will be terminated and then delete it
thread = Thread(target=deleteSlice, args=(free5gcMessage, blue_id, "del_slice"))
thread.start()
return RestAnswer202()
except Exception as e:
logger.warn("Impossible to delete the slice: {}".format(e))
raise fastapi.HTTPException(status_code=404, detail="Impossible to delete the slice: {}".format(e))
@northRouter.post("/nfvcl/v1/api/blue/Free5GC_K8s/{blue_id}/check_slice", response_model=RestAnswer202)
async def checkSlice(free5gcMessage: Union[Free5gck8sBlueCreateModel, MiniFree5gcModel], blue_id: str):
try:
nsLcmOpOcc = getLcmOpOcc(free5gcMessage)
if not nsLcmOpOcc:
raise ValueError("The nsLcmOpOcc doesn't exist")
return RestAnswer202(description=nsLcmOpOcc.operationState, status=nsLcmOpOcc.operationState)
except Exception as e:
logger.warn("Impossible to check the slice: {}".format(e))
raise fastapi.HTTPException(status_code=404, detail="Impossible to check the slice: {}".format(e))