From e51a1aa6f482e680b08365a650be6d353504cc34 Mon Sep 17 00:00:00 2001
From: rahhal <mrahhal@cttc.es>
Date: Thu, 13 Feb 2025 12:10:01 +0000
Subject: [PATCH] QOS CAMARA- NBI: - Correction After Shayan's Review

---
 my_deploy.sh                                  |  2 +-
 .../nbi_plugins/camara_qod/Resources.py       | 56 +++++++++----------
 src/nbi/tests/test_camara_qod_profile.py      |  5 +-
 src/nbi/tests/test_camara_qos_service.py      | 33 ++++++-----
 4 files changed, 45 insertions(+), 51 deletions(-)

diff --git a/my_deploy.sh b/my_deploy.sh
index 225480682..7984f77d0 100644
--- a/my_deploy.sh
+++ b/my_deploy.sh
@@ -134,7 +134,7 @@ export CRDB_PASSWORD="tfs123"
 export CRDB_DEPLOY_MODE="single"
 
 # Disable flag for dropping database, if it exists.
-export CRDB_DROP_DATABASE_IF_EXISTS=""
+export CRDB_DROP_DATABASE_IF_EXISTS="YES"
 
 # Disable flag for re-deploying CockroachDB from scratch.
 export CRDB_REDEPLOY=""
diff --git a/src/nbi/service/rest_server/nbi_plugins/camara_qod/Resources.py b/src/nbi/service/rest_server/nbi_plugins/camara_qod/Resources.py
index cd89c02fb..78c297e57 100644
--- a/src/nbi/service/rest_server/nbi_plugins/camara_qod/Resources.py
+++ b/src/nbi/service/rest_server/nbi_plugins/camara_qod/Resources.py
@@ -48,15 +48,14 @@ class ProfileList(_Resource):
         # JSON TO GRPC to store the data in the grpc server
         try:
             qos_profile = create_qos_profile_from_json(request_data)
-        except Exception as e:
-            LOGGER.error(e) # track if there is an error
-            raise e  
+        except:
+            LOGGER.exception("Error happened while creating QoS profile from json")
+            return {"message": "Failed to create QoS profile"}, 500  
         # Send to gRPC server using CreateQosProfile 
         try:
             qos_profile_created = self.qos_profile_client.CreateQoSProfile(qos_profile) 
-        except Exception as e:
-            LOGGER.error(e)
-            raise e
+        except:
+            LOGGER.exception("error happened while creating QoS profile")
         #gRPC message back to JSON using the helper function 
         qos_profile_data = grpc_message_to_qos_table_data(qos_profile_created)
         LOGGER.info(f'qos_profile_data{qos_profile_data}')       
@@ -92,8 +91,8 @@ class ProfileDetail(_Resource):
                 return {"error": f"QoSProfile {qos_profile_id} not found"}, 404
             LOGGER.error(f"gRPC error while fetching QoSProfile: {exc}")
             return {"error": "Internal Server Error"}, 500
-        except Exception as e:
-            LOGGER.error(f"Error while fetching QoSProfile: {e}")
+        except:
+            LOGGER.exception(f"Error while fetching QoSProfile")
             return {"error": "Internal Server Error"}, 500
 
     
@@ -114,8 +113,8 @@ class ProfileDetail(_Resource):
                 return {"error": f"QoSProfile {qos_profile_id} not found"}, 404
             LOGGER.error(f"gRPC error while updating QoSProfile: {exc}")
             return {"error": "Internal Server Error"}, 500
-        except Exception as e:
-            LOGGER.error(f"Error in PUT /profiles/{qos_profile_id}: {e}")
+        except:
+            LOGGER.exception(f"Error in PUT /profiles/{qos_profile_id}")
             return {"error": "Internal Server Error"}, 500
         
     def delete(self, qos_profile_id):
@@ -130,15 +129,15 @@ class ProfileDetail(_Resource):
                 return {"error": f"QoSProfile {qos_profile_id} not found"}, 404
             LOGGER.error(f"gRPC error while deleting QoSProfile: {exc}")
             return {"error": "Internal Server Error"}, 500
-        except Exception as e:
-            LOGGER.error(f"Error in DELETE /profiles/{qos_profile_id}: {e}")
+        except:
+            LOGGER.exception(f"Error in DELETE /profiles/{qos_profile_id}")
             return {"error": "Internal Server Error"}, 500
         
 ###SESSION##########################################################
 class QodInfo(_Resource):
     def post(self):
         if not request.is_json:
-            return (jsonify({'error': 'Unsupported Media Type', 'message': 'JSON payload is required'}), 415)
+            return jsonify({'error': 'Unsupported Media Type', 'message': 'JSON payload is required'}), 415
         request_data: Dict = request.get_json()
         qos_profile_id = request_data.get('qos_profile_id')
         qos_session_id = request_data.get('qos_session_id')
@@ -146,30 +145,33 @@ class QodInfo(_Resource):
         LOGGER.info(f'qos_profile_id:{qos_profile_id}')
         if not qos_profile_id:
             return jsonify({'error': 'qos_profile_id is required'}), 400
-        if  qos_session_id:
+        if qos_session_id:
             return jsonify({'error': 'qos_session_id is not allowed in creation'}), 400
-        service = QOD_2_service(self.client, request_data,qos_profile_id,duration)
+        service = QOD_2_service(self.client, request_data, qos_profile_id, duration)
         stripped_service = copy.deepcopy(service)
         stripped_service.ClearField('service_endpoint_ids')
         stripped_service.ClearField('service_constraints')
         stripped_service.ClearField('service_config')
         try:
-            response = format_grpc_to_json(self.service_client.CreateService(stripped_service))
-            response = format_grpc_to_json(self.service_client.UpdateService(service))
-        except Exception as e: # pylint: disable=broad-except
+            create_response = format_grpc_to_json(self.service_client.CreateService(stripped_service))
+            update_response = format_grpc_to_json(self.service_client.UpdateService(service))
+            response = {
+                "create_response": create_response,
+                "update_response": update_response
+            }
+        except Exception as e:
             LOGGER.error(f"Unexpected error: {str(e)}", exc_info=True)
             return jsonify({'error': 'Internal Server Error', 'message': 'An unexpected error occurred'}), 500
-        LOGGER.error(f"error related to response: {response}")
         return response
-    
+    #didnt work return jsonify(response)
     def get(self):
         service_list = self.client.ListServices(grpc_context_id(DEFAULT_CONTEXT_NAME)) #return context id as json
         qod_info = [service_2_qod(service) for service in service_list.services] #iterating over service list 
         LOGGER.info(f"error related to qod_info: {qod_info}")
-        return jsonify(qod_info), 200
-           
-class QodInfoID(_Resource):
+        return qod_info
+    #didnt work return jsonify(qod_info)
 
+class QodInfoID(_Resource):
     def get(self, sessionId: str):
         try:
             service = self.client.GetService(grpc_service_id(DEFAULT_CONTEXT_NAME, sessionId))
@@ -178,7 +180,6 @@ class QodInfoID(_Resource):
             if exc.code()==grpc.StatusCode.NOT_FOUND:
                 LOGGER.warning(f"Qod Session not found: {sessionId}")
                 return {"error": f"Qod Session {sessionId} not found"}, 404
-
     def put(self, sessionId: str):
         try:
             request_data: Dict = request.get_json()
@@ -199,21 +200,18 @@ class QodInfoID(_Resource):
             updated_service = self.service_client.UpdateService(service)
             qod_response = service_2_qod(updated_service)
             return qod_response, 200
-
         except KeyError as e:
             LOGGER.error(f"Missing required key: {e}")
             return {"error": f"Missing required key: {str(e)}"}, 400
-
         except grpc._channel._InactiveRpcError as exc:
             if exc.code() == grpc.StatusCode.NOT_FOUND:
                 LOGGER.warning(f"Qod Session not found: {sessionId}")
                 return {"error": f"Qod Session {sessionId} not found"}, 404
             LOGGER.error(f"gRPC error while updating Qod Session: {exc}")
             return {"error": "Internal Server Error"}, 500
-        except Exception as e:
-            LOGGER.error(f"Error in PUT /sessions/{sessionId}: {e}")
+        except:
+            LOGGER.exception(f"Error in PUT /sessions/{sessionId}")
             return {"error": "Internal Server Error"}, 500
-    
     def delete(self, sessionId: str):
         self.service_client.DeleteService(grpc_service_id(DEFAULT_CONTEXT_NAME, sessionId))
         return{"Session Deleted"}
diff --git a/src/nbi/tests/test_camara_qod_profile.py b/src/nbi/tests/test_camara_qod_profile.py
index 7acb20374..99dbf1f4d 100644
--- a/src/nbi/tests/test_camara_qod_profile.py
+++ b/src/nbi/tests/test_camara_qod_profile.py
@@ -16,12 +16,10 @@ import logging
 from flask import jsonify
 import requests
 
-
 logging.basicConfig(level=logging.DEBUG)
 LOGGER = logging.getLogger()
-
 def test_create_profile():
-    BASE_URL = 'http://localhost/camara/qod/v0'
+    BASE_URL = 'http://10.1.7.197/camara/qod/v0'
     qos_profile_data={  
                       "name": "QCI_2_voice",
                       "description": "QoS profile for video streaming",
@@ -91,7 +89,6 @@ def test_create_profile():
 
 
 
-
 #def test_update_profile():
 #    qos_profile_id = '0898e7e8-ef15-4522-8e93-623e31c92efa'      
 #    qos_profile_data = {
diff --git a/src/nbi/tests/test_camara_qos_service.py b/src/nbi/tests/test_camara_qos_service.py
index e74e68231..f2cd3034c 100644
--- a/src/nbi/tests/test_camara_qos_service.py
+++ b/src/nbi/tests/test_camara_qos_service.py
@@ -15,28 +15,27 @@
 import logging
 from flask import jsonify
 import requests
-
 logging.basicConfig(level=logging.DEBUG)
 LOGGER = logging.getLogger()
-def test_create_SESSION():
-    BASE_URL = 'http://localhost/camara/qod/v0'
-    service_data={  
-    "device":
-        {"ipv4Address":"84.75.11.12/25" },
-    "applicationServer": {
-        "ipv4Address": "192.168.0.1/26",
-        },
-  "duration":10000000.00,
-  "qos_profile_id": "367d3e3f-96be-4391-af82-e21c042dd9bd",
-     }
-    post_response = requests.post(f'{BASE_URL}/sessions', json=service_data).json()
-    #id=post_response['sessionID']
-    #get_response = requests.get(f'{BASE_URL}/sessions/{id}').json()
-    get_response = requests.get(f'{BASE_URL}/sessions').json()
+BASE_URL = 'http://10.1.7.197/camara/qod/v0'
+#def test_create_SESSION():
+#    service_data={  
+#                    "device":
+#                        {"ipv4Address":"84.75.11.12/25" },
+#                            "applicationServer": {
+#                                    "ipv4Address": "192.168.0.1/26",
+#                                                                    },
+#                    "duration":10000000.00,
+#                    "qos_profile_id": "f46d563f-9f1a-44c8-9649-5fde673236d6",
+#                }      
+#    post_response = requests.post(f'{BASE_URL}/sessions', json=service_data).json()
+#    #id=post_response['sessionID']
+#    #get_response = requests.get(f'{BASE_URL}/sessions/{id}').json()
+#    get_response = requests.get(f'{BASE_URL}/sessions').json()
 
 
 #def test_delete_session_by_id():
-#    session_id = '' 
+#    session_id = '83d4b75a-9b09-40f4-b9a9-f31d591fa319' 
 #    response = requests.delete(f'{BASE_URL}/sessions/{session_id}')
 
 #def test_update_session_by_id():
-- 
GitLab