Skip to content
Snippets Groups Projects

Draft: Resolve "Implement CAMARA-based NBI connector"

Open Lluis Gifre Renom requested to merge feat/163-implement-camara-based-nbi-connector into develop
Compare and Show latest version
3 files
+ 295
247
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -11,37 +11,35 @@
@@ -11,37 +11,35 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import json
import json
from flask.json import jsonify
from flask.json import jsonify
from flask_restful import Resource, request, Api
from flask_restful import Resource, request
from enum import Enum
from enum import Enum
import uuid
import uuid
 
# Enum for session status
 
class SessionStatus(Enum):
 
ACTIVE = 'ACTIVE'
 
INACTIVE = 'INACTIVE'
 
 
class Rate:
 
def __init__(self, value, unit):
 
self.value = value
 
self.unit = unit
class SessionStatus(Enum):
class Duration:
ACTIVE='ACTIVE'
def __init__(self, value, unit):
INACTIVE='INACTIVE'
self.value = value
self.unit = unit
class Rate():
def __init__(self,value,unit):
class ProfileCreate:
self.value=value
def __init__(self, name, description, status, targetMinUpstreamRate=None, maxUpstreamRate=None, maxUpstreamBurstRate=None,
self.unit=unit
targetMinDownstreamRate=None, maxDownstreamRate=None, maxDownstreamBurstRate=None, minDuration=None, maxDuration=None,
priority=None, packetDelayBudget=None, jitter=None, packetErrorLossRate=None):
class Duration():
def __init__(self,value,unit):
self.value=value
self.unit=unit
class SessionCreate():
def __init__(self,source_ipv4_address,destination_ipv4_address,name,session_profile,targetMinUpstreamRate=None,maxUpstreamRate=None,
maxUpstreamBurstRate=None,targetMinDownstreamRate=None,maxDownstreamRate=None,maxDownstreamBurstRate=None,minDuration=None,
maxDuration=None,priority=None,packetDelayBudget=None,jitter=None,packetErrorLossRate=None,description=None):
self.source_ipv4_address = source_ipv4_address
self.destination_ipv4_address = destination_ipv4_address
self.name = name
self.name = name
self.session_profile = session_profile
self.description = description
 
self.status = status
self.targetMinUpstreamRate = targetMinUpstreamRate
self.targetMinUpstreamRate = targetMinUpstreamRate
self.maxUpstreamRate = maxUpstreamRate
self.maxUpstreamRate = maxUpstreamRate
self.maxUpstreamBurstRate = maxUpstreamBurstRate
self.maxUpstreamBurstRate = maxUpstreamBurstRate
@@ -54,96 +52,199 @@ class SessionCreate():
@@ -54,96 +52,199 @@ class SessionCreate():
self.packetDelayBudget = packetDelayBudget
self.packetDelayBudget = packetDelayBudget
self.jitter = jitter
self.jitter = jitter
self.packetErrorLossRate = packetErrorLossRate
self.packetErrorLossRate = packetErrorLossRate
self.description = description
class Session(SessionCreate):
class Profile(ProfileCreate):
def __init__(self,session_id,status,**kwargs):
def __init__(self, profile_id, **kwargs):
super().__init__(**kwargs)
super().__init__(**kwargs)
self.session_id=session_id
self.profile_id = profile_id
self.status=status
sessions=[]
profiles = []
 
 
class ProfileList(Resource):
 
def post(self):
 
data = request.get_json()
 
profile_id = str(uuid.uuid4())
 
new_profile = Profile(
 
profile_id=profile_id,
 
name=data.get('name'),
 
description=data.get('description'),
 
status=data.get('status', SessionStatus.ACTIVE.value),
 
targetMinUpstreamRate=data.get('targetMinUpstreamRate'),
 
maxUpstreamRate=data.get('maxUpstreamRate'),
 
maxUpstreamBurstRate=data.get('maxUpstreamBurstRate'),
 
targetMinDownstreamRate=data.get('targetMinDownstreamRate'),
 
maxDownstreamRate=data.get('maxDownstreamRate'),
 
maxDownstreamBurstRate=data.get('maxDownstreamBurstRate'),
 
minDuration=data.get('minDuration'),
 
maxDuration=data.get('maxDuration'),
 
priority=data.get('priority'),
 
packetDelayBudget=data.get('packetDelayBudget'),
 
jitter=data.get('jitter'),
 
packetErrorLossRate=data.get('packetErrorLossRate')
 
)
 
profiles.append(new_profile.__dict__)
 
return jsonify(new_profile.__dict__), 201
 
 
class ProfileDetail(Resource):
 
def get(self, profile_id):
 
profile = next((p for p in profiles if p["profile_id"] == profile_id), None)
 
if profile:
 
return jsonify(profile)
 
return {"message": "Profile not found"}, 404
 
 
def put(self, profile_id):
 
data = request.get_json()
 
profile = next((p for p in profiles if p["profile_id"] == profile_id), None)
 
if profile:
 
for key, value in data.items():
 
if key in profile:
 
profile[key] = value
 
return jsonify(profile)
 
return {"message": "Profile not found"}, 404
 
 
class Profile_delete_by_name(Resource):
 
def delete(self, name):
 
global profiles
 
profile = next((p for p in profiles if p["name"] == name), None)
 
if profile:
 
profiles = [p for p in profiles if p["name"] != name]
 
return {"message": "Profile deleted successfully"}, 200
 
return {"message": "Profile not found"}, 404
 
 
class AllProfiles(Resource):
 
def get(self):
 
return jsonify(profiles)
 
 
class PortRange:
 
def __init__(self, from_port, to_port):
 
self.from_port = from_port
 
self.to_port = to_port
 
 
class PortDetails:
 
def __init__(self, ranges=None, ports=None):
 
self.ranges = ranges or []
 
self.ports = ports or []
 
 
class Device:
 
def __init__(self, phoneNumber, networkAccessIdentifier, publicAddress, publicPort, ipv6Address):
 
self.phoneNumber = phoneNumber
 
self.networkAccessIdentifier = networkAccessIdentifier
 
self.publicAddress = publicAddress
 
self.publicPort = publicPort
 
self.ipv6Address = ipv6Address
 
 
class ApplicationServer:
 
def __init__(self, ipv4Address, ipv6Address):
 
self.ipv4Address = ipv4Address
 
self.ipv6Address = ipv6Address
 
 
class Webhook:
 
def __init__(self, notificationUrl, notificationAuthToken):
 
self.notificationUrl = notificationUrl
 
self.notificationAuthToken = notificationAuthToken
 
 
class SessionCreate:
 
def __init__(self, device, applicationServer, devicePorts, applicationServerPorts, qosProfile, webhook, duration):
 
self.device = device
 
self.applicationServer = applicationServer
 
self.devicePorts = devicePorts
 
self.applicationServerPorts = applicationServerPorts
 
self.qosProfile = qosProfile
 
self.webhook = webhook
 
self.duration = duration
 
 
sessions = []
class SessionList(Resource):
class SessionList(Resource):
def post(self):
def post(self):
data = request.get_json()
data = request.get_json()
session_id=str(uuid.uuid4())
session_id = str(uuid.uuid4())
new_session={
profile_name = data.get('qosProfile')
 
 
profile = next((p for p in profiles if p["name"] == profile_name), None)
 
if not profile:
 
return jsonify({"error": "QoS profile not found"}), 404
 
 
device = Device(
 
phoneNumber=data['device']['phoneNumber'],
 
networkAccessIdentifier=data['device']['networkAccessIdentifier'],
 
publicAddress=data['device']['ipv4Address']['publicAddress'],
 
publicPort=data['device']['ipv4Address']['publicPort'],
 
ipv6Address=data['device']['ipv6Address']
 
)
 
 
applicationServer = ApplicationServer(
 
ipv4Address=data['applicationServer']['ipv4Address'],
 
ipv6Address=data['applicationServer']['ipv6Address']
 
)
 
 
devicePorts = PortDetails(
 
ranges=[PortRange(r['from'], r['to']) for r in data['devicePorts']['ranges']],
 
ports=data['devicePorts']['ports']
 
)
 
 
applicationServerPorts = PortDetails(
 
ranges=[PortRange(r['from'], r['to']) for r in data['applicationServerPorts']['ranges']],
 
ports=data['applicationServerPorts']['ports']
 
)
 
 
webhook = Webhook(
 
notificationUrl=data['webhook']['notificationUrl'],
 
notificationAuthToken=data['webhook']['notificationAuthToken']
 
)
 
 
new_session = SessionCreate(
 
device=device,
 
applicationServer=applicationServer,
 
devicePorts=devicePorts,
 
applicationServerPorts=applicationServerPorts,
 
qosProfile=profile_name,
 
webhook=webhook,
 
duration=data['duration']
 
)
 
 
session_data = {
"session_id": session_id,
"session_id": session_id,
"name": data.get('name'),
"device": {
"source_ipv4_address": data.get('source_ipv4_address'),
"phoneNumber": device.phoneNumber,
"destination_ipv4_address": data.get('destination_ipv4_address'),
"networkAccessIdentifier": device.networkAccessIdentifier,
"session_profile": data.get('session_profile'),
"ipv4Address": {
"targetMinUpstreamRate": data.get('targetMinUpstreamRate'),
"publicAddress": device.publicAddress,
"maxUpstreamRate": data.get('maxUpstreamRate'),
"publicPort": device.publicPort
"maxUpstreamBurstRate": data.get('maxUpstreamBurstRate'),
},
"targetMinDownstreamRate": data.get('targetMinDownstreamRate'),
"ipv6Address": device.ipv6Address
"maxDownstreamRate": data.get('maxDownstreamRate'),
},
"maxDownstreamBurstRate": data.get('maxDownstreamBurstRate'),
"applicationServer": {
"minDuration": data.get('minDuration'),
"ipv4Address": applicationServer.ipv4Address,
"maxDuration": data.get('maxDuration'),
"ipv6Address": applicationServer.ipv6Address
"priority": data.get('priority'),
},
"packetDelayBudget": data.get('packetDelayBudget'),
"devicePorts": {
"jitter": data.get('jitter'),
"ranges": [{"from": r.from_port, "to": r.to_port} for r in devicePorts.ranges],
"packetErrorLossRate": data.get('packetErrorLossRate'),
"ports": devicePorts.ports
"description": data.get('description'),
},
"status":SessionStatus.ACTIVE.value
"applicationServerPorts": {
 
"ranges": [{"from": r.from_port, "to": r.to_port} for r in applicationServerPorts.ranges],
 
"ports": applicationServerPorts.ports
 
},
 
"qosProfile": new_session.qosProfile,
 
"webhook": {
 
"notificationUrl": webhook.notificationUrl,
 
"notificationAuthToken": webhook.notificationAuthToken
 
},
 
"duration": new_session.duration,
 
"status": SessionStatus.ACTIVE.value
}
}
sessions.append(new_session)
return jsonify(new_session)
class AllSessions(Resource):
def get(self):
all_sessions = []
for session in sessions:
session_details = {
"session_id": session["session_id"],
"name": session["name"],
"source_ipv4_address": session["source_ipv4_address"],
"destination_ipv4_address": session["destination_ipv4_address"],
"session_profile": session["session_profile"],
"targetMinUpstreamRate": session["targetMinUpstreamRate"],
"maxUpstreamRate": session["maxUpstreamRate"],
"maxUpstreamBurstRate": session["maxUpstreamBurstRate"],
"targetMinDownstreamRate": session["targetMinDownstreamRate"],
"maxDownstreamRate": session["maxDownstreamRate"],
"maxDownstreamBurstRate": session["maxDownstreamBurstRate"],
"minDuration": session["minDuration"],
"maxDuration": session["maxDuration"],
"priority": session["priority"],
"packetDelayBudget": session["packetDelayBudget"],
"jitter": session["jitter"],
"packetErrorLossRate": session["packetErrorLossRate"],
"description": session["description"],
"status": session["status"]
}
all_sessions.append(session_details)
return jsonify(all_sessions)
class DeleteAllSessions(Resource):
sessions.append(session_data)
def delete(self):
return jsonify(session_data), 201
global sessions
session_count = len(sessions)
class SessionDetail(Resource):
if session_count > 0:
def get(self, session_id):
sessions.clear()
session = next((s for s in sessions if s["session_id"] == session_id), None)
return {"message": f"All {session_count} sessions deleted successfully"}
if session:
else:
return jsonify(session)
return {"message": "No sessions to delete"}, 204
return {"message": "Session not found"}, 404
class Sessionlist2(Resource):
def get(self, session_id):
for session in sessions:
if session["session_id"] == session_id:
return jsonify(session)
abort(404, description="Session not found")
def delete(self, session_id):
global sessions
for session in sessions[:]:
if session["session_id"] == session_id:
sessions.remove(session)
return {"message": "Session deleted successfully"}
abort(404, description="Session not found")
def put(self, session_id):
def put(self, session_id):
data = request.get_json()
data = request.get_json()
session = next((s for s in sessions if s["session_id"] == session_id), None)
session = next((s for s in sessions if s["session_id"] == session_id), None)
@@ -152,5 +253,23 @@ class Sessionlist2(Resource):
@@ -152,5 +253,23 @@ class Sessionlist2(Resource):
if key in session:
if key in session:
session[key] = value
session[key] = value
return jsonify(session)
return jsonify(session)
abort(404, description="Session not found")
return {"message": "Session not found"}, 404
 
 
def delete(self, session_id):
 
global sessions
 
session = next((s for s in sessions if s["session_id"] == session_id), None)
 
if session:
 
sessions = [s for s in sessions if s["session_id"] != session_id]
 
return {"message": "Session deleted successfully"}, 200
 
return {"message": "Session not found"}, 404
 
class AllSessions(Resource):
 
def get(self):
 
return jsonify(sessions)
 
 
class DeleteAllSessions(Resource):
 
def delete(self):
 
global sessions
 
session_count = len(sessions)
 
sessions = []
 
return {"message": f"Deleted {session_count} sessions."}, 200
Loading