From 91d834f7d0cd29455d4bca9bf4dfdcfa3487d701 Mon Sep 17 00:00:00 2001
From: gifrerenom <lluis.gifre@cttc.es>
Date: Fri, 25 Oct 2024 15:22:13 +0000
Subject: [PATCH] Pre-merge code cleanup

---
 .../tools/context_queries/OpticalConfig.py    | 12 --------
 src/context/service/database/OpticalConfig.py |  5 ++--
 src/service/service/tools/object_uuid.py      | 29 +++++++++++++++----
 src/webui/service/opticalconfig/routes.py     | 18 ++++++++++--
 4 files changed, 42 insertions(+), 22 deletions(-)

diff --git a/src/common/tools/context_queries/OpticalConfig.py b/src/common/tools/context_queries/OpticalConfig.py
index 92dd7136b..bd697599c 100644
--- a/src/common/tools/context_queries/OpticalConfig.py
+++ b/src/common/tools/context_queries/OpticalConfig.py
@@ -45,18 +45,6 @@ def get_uuid_random() -> str:
     # Generate random UUID. No need to use namespace since "namespace + random = random".
     return str(uuid4())
 
-def channel_get_uuid(
-    channel_name :str , allow_random : bool = False
-) -> str:
-    if len(channel_name) > 0:
-        return get_uuid_from_string(channel_name)
-    if allow_random: return get_uuid_random()
-
-    raise InvalidArgumentsException([
-        ('channel uuid', channel_name),
-       
-    ], extra_details=['Channel name is required to produce a channel UUID'])
-    
 def device_get_uuid (device_name) : 
     if (len(device_name)> 0):
         return  get_uuid_from_string(device_name)
diff --git a/src/context/service/database/OpticalConfig.py b/src/context/service/database/OpticalConfig.py
index a5791fc0e..ad38751ee 100644
--- a/src/context/service/database/OpticalConfig.py
+++ b/src/context/service/database/OpticalConfig.py
@@ -23,7 +23,7 @@ from common.proto.context_pb2 import OpticalConfig, OpticalConfigId, Empty, Even
 from .models.OpticalConfig.OpticalConfigModel import OpticalConfigModel
 from .models.OpticalConfig.TransponderModel import  TransponderTypeModel, OpticalChannelModel
 from .models.OpticalConfig.RoadmModel import RoadmTypeModel, ChannelModel
-from context.service.database.uuids.OpticalConfig import (
+from .uuids.OpticalConfig import (
     channel_get_uuid, opticalconfig_get_uuid, transponder_get_uuid, roadm_get_uuid
 )
 from .Events import notify_event_opticalconfig
@@ -38,7 +38,6 @@ def get_opticalconfig(db_engine : Engine):
             optical_config = OpticalConfig()
             optical_config.config = json.dumps(obj.dump())
             ids_obj = obj.dump_id()
-
             optical_config.opticalconfig_id.opticalconfig_uuid = ids_obj["opticalconfig_uuid"]
             optical_config.device_id.device_uuid.uuid=ids_obj["device_uuid"]
             optical_configs.append(optical_config)
@@ -57,7 +56,7 @@ def set_opticalconfig(db_engine : Engine, request : OpticalConfig):
     OpticalConfig_data = []
     config_type=None
     #is_transpondre=False
-    opticalconfig_uuid =opticalconfig_get_uuid(device_id) 
+    opticalconfig_uuid = opticalconfig_get_uuid(device_id) 
 
     if request.config:
         config = json.loads(request.config)
diff --git a/src/service/service/tools/object_uuid.py b/src/service/service/tools/object_uuid.py
index a5d905d10..e9454048a 100644
--- a/src/service/service/tools/object_uuid.py
+++ b/src/service/service/tools/object_uuid.py
@@ -1,10 +1,30 @@
+# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
 
 from common.method_wrappers.ServiceExceptions import InvalidArgumentsException
 from typing import Optional, Union
-from uuid import UUID, uuid4, uuid5
+from uuid import UUID, uuid5
 from common.proto.context_pb2 import DeviceId
 
-
+# Generate a UUIDv5-like from the SHA-1 of "TFS" and no namespace to be used as the NAMESPACE for all
+# the context UUIDs generated. For efficiency purposes, the UUID is hardcoded; however, it is produced
+# using the following code:
+#    from hashlib import sha1
+#    from uuid import UUID
+#    hash = sha1(bytes('TFS', 'utf-8')).digest()
+#    NAMESPACE_TFS = UUID(bytes=hash[:16], version=5)
 NAMESPACE_TFS = UUID('200e3a1f-2223-534f-a100-758e29c37f40')
 
 def get_uuid_from_string(str_uuid_or_name : Union[str, UUID], prefix_for_name : Optional[str] = None) -> str:
@@ -22,10 +42,9 @@ def get_uuid_from_string(str_uuid_or_name : Union[str, UUID], prefix_for_name :
             str_uuid_or_name = '{:s}/{:s}'.format(prefix_for_name, str_uuid_or_name)
         return str(uuid5(NAMESPACE_TFS, str_uuid_or_name))
 
-
-def opticalconfig_get_uuid ( device_id: DeviceId) -> str : 
+def opticalconfig_get_uuid(device_id: DeviceId) -> str: 
     device_uuid = device_id.device_uuid.uuid
-    if (len(device_uuid)>0):
+    if len(device_uuid) > 0:
         return get_uuid_from_string(f"{device_uuid}_opticalconfig")
 
     raise InvalidArgumentsException([
diff --git a/src/webui/service/opticalconfig/routes.py b/src/webui/service/opticalconfig/routes.py
index d7016baca..97eec6eae 100644
--- a/src/webui/service/opticalconfig/routes.py
+++ b/src/webui/service/opticalconfig/routes.py
@@ -1,3 +1,17 @@
+# Copyright 2022-2024 ETSI OSG/SDG TeraFlowSDN (TFS) (https://tfs.etsi.org/)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
 import base64, json, logging #, re
 from flask import request, redirect, render_template, Blueprint, flash, session, url_for, current_app ,make_response
 from common.proto.context_pb2 import ( Empty
@@ -7,8 +21,8 @@ from context.client.ContextClient import ContextClient
 from device.client.DeviceClient import DeviceClient
 from service.client.ServiceClient import ServiceClient
 from slice.client.SliceClient import SliceClient
-from .forms import UpdateDeviceForm ,AddTrancseiver ,UpdateInterfaceForm ,UpdateStatusForm
-from common.tools.context_queries.OpticalConfig import opticalconfig_get_uuid , device_get_uuid
+from .forms import UpdateDeviceForm, AddTrancseiver, UpdateStatusForm
+from common.tools.context_queries.OpticalConfig import opticalconfig_get_uuid
 
 
 opticalconfig = Blueprint('opticalconfig', __name__,url_prefix="/opticalconfig")
-- 
GitLab