diff --git a/Dockerfile b/Dockerfile index b74bdf2..01b5686 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ COPY requirements.txt / RUN pip install --no-cache-dir -r /requirements.txt ADD application_manager/*.py / +ADD application_manager/get-labels.sh / ADD run_application_manager/run_manager.sh / #ADD services/leader_file.txt / uncomment this to test the run_manager diff --git a/application_manager/__pycache__/app_dht.cpython-310.pyc b/application_manager/__pycache__/app_dht.cpython-310.pyc index 2819f75..4d4b41a 100644 Binary files a/application_manager/__pycache__/app_dht.cpython-310.pyc and b/application_manager/__pycache__/app_dht.cpython-310.pyc differ diff --git a/application_manager/__pycache__/app_dht.cpython-311.pyc b/application_manager/__pycache__/app_dht.cpython-311.pyc new file mode 100644 index 0000000..a04e7fb Binary files /dev/null and b/application_manager/__pycache__/app_dht.cpython-311.pyc differ diff --git a/application_manager/__pycache__/catch_topic.cpython-310.pyc b/application_manager/__pycache__/catch_topic.cpython-310.pyc index 4acbbbe..55c1c75 100644 Binary files a/application_manager/__pycache__/catch_topic.cpython-310.pyc and b/application_manager/__pycache__/catch_topic.cpython-310.pyc differ diff --git a/application_manager/__pycache__/security_by_contract.cpython-310.pyc b/application_manager/__pycache__/security_by_contract.cpython-310.pyc new file mode 100644 index 0000000..e886894 Binary files /dev/null and b/application_manager/__pycache__/security_by_contract.cpython-310.pyc differ diff --git a/application_manager/app_dht.py b/application_manager/app_dht.py index a0eb022..9455463 100644 --- a/application_manager/app_dht.py +++ b/application_manager/app_dht.py @@ -5,7 +5,7 @@ client = docker.from_env() -api_url = "http://localhost:3000/" +api_url = "http://sifis-device4.iit.cnr.it:3000/" def publish(ws, topic_uuid, requestor_id, request_id, containers): @@ -36,18 +36,27 @@ def request_list(ws, message, image_name): requestor_id = topic_value["requestor_id"] request_id = topic_value["request_id"] containers = topic_value["containers"] - containers.append(image_name) - publish(ws, topic_uuid, requestor_id, request_id, containers) - print("\n[!] Active Containers: " + str(containers) + "\n") + if image_name not in containers: + containers.append(image_name) + publish(ws, topic_uuid, requestor_id, request_id, containers) + print("\n[!] Active Containers: " + str(containers) + "\n") + result = "OK" + return result + else: + print("The App has been already installed") + print("\n[!] Active Containers: " + str(containers) + "\n") + result = "Already Installed" + return result def update_dht_list(ws, image_name): topic_name = "SIFIS:container_list" try: response = requests.get(api_url + "topic_name/" + topic_name) - message = str(response.json()[0]) + message = str(response.json()[-1]) message = message.replace("'", '"') - request_list(ws, message, image_name) + result = request_list(ws, message, image_name) + return result except IndexError: print("\n[!] Update List, the list is empty") topic_uuid = "Pippo" @@ -58,32 +67,39 @@ def update_dht_list(ws, image_name): def pull_image(ws, image_name, topic_uuid, requestor_id, request_id): + prefix = "ghcr.io/sifis-home/" if image_name == "": raise ValueError("Image name cannot be empty") + if prefix not in image_name: + image_name = prefix + image_name if image_name: try: - client.images.pull(image_name) - update_dht_list(ws, image_name) topic_name = "SIFIS:application_manager_pull_image" - pulling_data = { - "requestor_id": requestor_id, - "request_id": request_id, - "pulled_image": image_name, - "operation": "pull image", - "result": "successfull", - } - requests.post( - api_url - + "topic_name/" - + topic_name - + "/topic_uuid/" - + topic_uuid, - json=pulling_data, - ) + result = update_dht_list(ws, image_name) + if result != "Already Installed": + client.images.pull(image_name) + # update_dht_list(ws, image_name) + pulling_data = { + "requestor_id": requestor_id, + "request_id": request_id, + "pulled_image": image_name, + "operation": "pull image", + "result": "successfull", + } + requests.post( + api_url + + "topic_name/" + + topic_name + + "/topic_uuid/" + + topic_uuid, + json=pulling_data, + ) - print(f"[!] Image {image_name} pulled successfully!") - start_container(image_name, topic_uuid, requestor_id, request_id) - return "\n Pulling and Starting operation completed ..." + print(f"[!] Image {image_name} pulled successfully!") + start_container( + image_name, topic_uuid, requestor_id, request_id + ) + return "\n Pulling and Starting operation completed ..." except docker.errors.APIError as e: pulling_data = { "requestor_id": requestor_id, @@ -114,8 +130,8 @@ def start_container(image_name, topic_uuid, requestor_id, request_id): image_name, detach=True, volumes={ - "/var/run/docker.sock": { - "bind": "/var/run/docker.sock", + "/var/run/sifis.sock": { + "bind": "/var/run/sifis.sock", "mode": "rw", } }, # volume @@ -197,8 +213,9 @@ def remove_image(image_name, topic_uuid, request_id, requestor_id): return "Missing 'image_name' parameter", 400 try: - client.images.remove(image_name, force=True) + print("[!] Removing Image : " + image_name) topic_name = "SIFIS:application_manager_remove_image" + list_containers(topic_uuid, requestor_id, request_id, image_name) removing_info = { "requestor_id": requestor_id, "request_id": request_id, @@ -211,6 +228,7 @@ def remove_image(image_name, topic_uuid, request_id, requestor_id): json=removing_info, ) list_containers(topic_uuid, requestor_id, request_id, image_name) + client.images.remove(image_name, force=True) return f"Image {image_name} removed successfully!" except docker.errors.ImageNotFound as e: return f"Image {image_name} not found: {e}", 404 @@ -221,13 +239,13 @@ def remove_image(image_name, topic_uuid, request_id, requestor_id): def list_containers(topic_uuid, requestor_id, request_id, image_name): topic_name = "SIFIS:container_list" response = requests.get(api_url + "topic_name/" + topic_name) - message = str(response.json()[0]) + message = str(response.json()[-1]) message = message.replace("'", '"') if "value" in str(message): json_message = json.loads(message) topic_value = json_message["value"] containers = topic_value["containers"] - if image_name != None: + if image_name in containers: containers.remove(image_name) _list = { "requestor_id": requestor_id, diff --git a/application_manager/catch_topic.py b/application_manager/catch_topic.py index 34e2a09..3688c77 100644 --- a/application_manager/catch_topic.py +++ b/application_manager/catch_topic.py @@ -4,12 +4,32 @@ import security_by_contract import websocket +REGISTERED = False +registration_id = 1 +session_id = "None" +permit_installation = False -def handle_pull_image(ws, topic_name, topic_uuid): + +def UCS_request(ws, topic_name, topic_uuid): + global REGISTERED, session_id try: image_name = topic_name["image_name"] - security_by_contract.get_labels(image_name) + print("[!] Recovering App LABELS\n") + _, app_id = security_by_contract.get_labels(image_name) + session_id = app_id + print("[!] APP_ID: " + str(session_id)) + return + except Exception as e: + print(e) + +""" +def handle_pull_image(ws, topic_name, topic_uuid): + global REGISTERED, session_id + print("Permit Installation") + print(permit_installation) + try: + image_name = topic_name["image_name"] requestor_id = topic_name["requestor_id"] request_id = topic_name["request_id"] result = app_dht.pull_image( @@ -18,6 +38,33 @@ def handle_pull_image(ws, topic_name, topic_uuid): print(result) except Exception as e: print(e) +""" + + +def handle_pull_image(): + global REGISTERED, session_id, global_pull_image_params + + # Accedi ai parametri globali + params = global_pull_image_params + if params is not None: + ws = params["ws"] + topic_value = params["topic_value"] + topic_uuid = params["topic_uuid"] + try: + image_name = topic_value["image_name"] + requestor_id = topic_value["requestor_id"] + request_id = topic_value["request_id"] + result = app_dht.pull_image( + ws, image_name, topic_uuid, requestor_id, request_id + ) + print(result) + print( + "\n----------------- PULLING OPERATION COMPLETED ---------------------------------\n" + ) + except Exception as e: + print(e) + else: + print("No pull image parameters available.") def handle_start_container(topic_name): @@ -38,6 +85,9 @@ def handle_remove_image(topic_name, topic_uuid): image_name, topic_uuid, request_id, requestor_id ) print(result) + print( + "\n----------------- REMOVING OPERATION COMPLETED ---------------------------------\n" + ) except Exception as e: print(e) @@ -70,13 +120,67 @@ def handle_list_containers(topic_uuid, requestor_id, request_id): topic_uuid, requestor_id, request_id, None ) print(result) + print( + "\n----------------- LISTING OPERATION COMPLETED ---------------------------------\n" + ) + except Exception as e: + print(e) + + +def wait_for_access(json_value): + global registration_id + print("[!] Wait for access") + _id = json_value["command"]["value"]["id"] + try: + if _id == "pep-application_manager": + purpose = json_value["command"]["value"]["message"]["purpose"] + code = json_value["command"]["value"]["message"]["code"] + response_id = json_value["command"]["value"]["message"][ + "message_id" + ] + if ( + purpose == "REGISTER_RESPONSE" + and code == "OK" + and response_id == registration_id + ): + return "OK" + else: + return "REGISTRATION DENIED" except Exception as e: print(e) def on_message(ws, message): + global REGISTERED + global session_id + global permit_installation json_message = json.loads(message) + try: + if "Volatile" in json_message: + json_message = json_message["Volatile"] + json_value = json_message["value"] + purpose = json_value["command"]["value"]["message"]["purpose"] + if purpose == "TRY_RESPONSE": + print("[!] Permit Installation") + handle_pull_image() + + if ( + json_value["command"]["value"]["topic_name"] + == "application_manager_registration" + and REGISTERED == False + ): + access = wait_for_access(json_value) + if access == "OK": + REGISTERED = True + print( + "[!] REGISTRATION OK: Application Manager is registered to UC\n\n" + ) + else: + print("[!] Application Manager is not registered to UC") + except Exception as e: + pass + if "Persistent" in json_message: json_message = json_message["Persistent"] # Handle messages @@ -92,11 +196,29 @@ def on_message(ws, message): ) +global_pull_image_params = None + + +# Funzione per salvare i parametri di handle_pull_image +def save_pull_image_params(params): + global global_pull_image_params + global_pull_image_params = params + + def handle_message(ws, topic_uuid, topic_value, request_id, requestor_id): + global permit_installation if "operation" in topic_value: operation = topic_value["operation"] if operation == "pull_image": - handle_pull_image(ws, topic_value, topic_uuid) + print("[!] Forwarding UCS Request") + UCS_request(ws, topic_value, topic_uuid) + print("[!] Pulling Image Request") + params = { + "ws": ws, + "topic_value": topic_value, + "topic_uuid": topic_uuid, + } + save_pull_image_params(params) elif operation == "remove_image": handle_remove_image(topic_value, topic_uuid) elif operation == "start_container": @@ -118,12 +240,17 @@ def on_close(ws, close_status_code, close_msg): def on_open(ws): + global REGISTERED + global registration_id print("### Connection established ###") + if REGISTERED == False: + registration_id = security_by_contract.register() + print("[!] The registration ID is : " + str(registration_id)) if __name__ == "__main__": ws = websocket.WebSocketApp( - "ws://localhost:3000/ws", + "ws://sifis-device4.iit.cnr.it:3000/ws", on_open=on_open, on_message=on_message, on_error=on_error, diff --git a/application_manager/security_by_contract.py b/application_manager/security_by_contract.py index f1cd1ce..17d8ef3 100644 --- a/application_manager/security_by_contract.py +++ b/application_manager/security_by_contract.py @@ -1,6 +1,45 @@ +import base64 +import datetime import json import os import subprocess +import uuid +from pathlib import Path + +import requests + +REGISTERED = False +websocket_uri = "http://sifis-device4.iit.cnr.it:3000/" + + +def get_json_register(): + ws_req = { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pep-command", + "value": { + "message": { + "purpose": "REGISTER", + "message_id": str(uuid.uuid1()), + "sub_topic_name": "application_manager_registration", + "sub_topic_uuid": "application_manager_registration_uuid", + }, + "id": "pep-application_manager", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + print("\n---------- REGISTRATION ATTEMPT ------------\n") + message_id = ws_req["command"]["value"]["message"]["message_id"] + return ws_req, message_id + + +def register(): + req, id = get_json_register() + requests.post(websocket_uri + "pub", json=req) + REGISTERED = True + return id def extract_manifest_json(output): @@ -50,6 +89,39 @@ def run_cargo_command(json_filename): print("Command output (stderr):", e.stderr) +def xml_to_base64(xml_file_path): + try: + request = Path(xml_file_path).read_text() + print("XACML request used:") + b = base64.b64encode(bytes(request, "utf-8")) # bytes + request64 = b.decode("utf-8") + + return request64 + except Exception as e: + return str(e) + + +def organize_json(request_base64): + ws_req = { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pep-command", + "value": { + "message": { + "purpose": "TRY", + "message_id": str(uuid.uuid1()), + "request": request_base64, + "policy": None, + }, + "id": "pep-application_manager", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + return ws_req + + def get_labels(image_name): # Name of the file to execute script_file = "application_manager/get-labels.sh" @@ -58,32 +130,54 @@ def get_labels(image_name): try: # Execute the shell command with the given image name as an argument - completed_process = subprocess.run( - ["bash", script_file, sifis_prefix + image_name, version], - stdout=subprocess.PIPE, - text=True, - check=True, + manifest_data = _extract_labels( + image_name, script_file, sifis_prefix, version ) - - # Get the output of the execution - output = completed_process.stdout - - # Extract the JSON under the "manifest" label - manifest_json = extract_manifest_json(output) - - # Parse the extracted JSON - manifest_data = json.loads(manifest_json) - - # Print or process the extracted JSON data - print("Extracted Manifest JSON:") - print(json.dumps(manifest_data, indent=2)) json_filename = "manifest_" + image_name + ".json" path = "sifis-xacml/data/" save_manifest_to_file(manifest_data, path + json_filename) run_cargo_command(json_filename) - return manifest_data + formatted_json, message_id = handle_xcml_request(image_name) + requests.post(websocket_uri + "pub", json=formatted_json) + return json_filename, message_id except subprocess.CalledProcessError as e: print("Error during script execution:", e) +def _extract_labels(image_name, script_file, sifis_prefix, version): + completed_process = subprocess.run( + ["bash", script_file, sifis_prefix + image_name, version], + stdout=subprocess.PIPE, + text=True, + check=True, + ) + + # Get the output of the execution + output = completed_process.stdout + + # Extract the JSON under the "manifest" label + manifest_json = extract_manifest_json(output) + + # Parse the extracted JSON + manifest_data = json.loads(manifest_json) + + # Print or process the extracted JSON data + print("Extracted Manifest JSON:") + print(json.dumps(manifest_data, indent=2)) + return manifest_data + + +def handle_xcml_request(image_name): + source_path = "sifis-xacml/manifest_" + file_path = source_path + image_name + "/request_1.xml" + base64_content = xml_to_base64(file_path) + organized_json = organize_json(base64_content) + print(json.dumps(organized_json, indent=2)) + message_id = organized_json["command"]["value"]["message"]["message_id"] + return organized_json, message_id + + +""" +register() get_labels("3pa-lamp-amd64") +""" diff --git a/application_manager/test-ucs.py b/application_manager/test-ucs.py new file mode 100644 index 0000000..9e455ed --- /dev/null +++ b/application_manager/test-ucs.py @@ -0,0 +1,532 @@ +import base64 +import datetime +import json +import uuid +from pathlib import Path + +import websocket + +websocket_uri = "ws://sifis-device4.iit.cnr.it:3000/ws" +session_id = "None" + + +def on_message(ws, message): + if "ucs-command" in message: + print("\nReceived message from the ucs:") + parsed = json.loads(message) + print(json.dumps(parsed, indent=2)) + print("\n--------------------------------\n") + + if ( + parsed["Volatile"]["value"]["command"]["value"]["message"][ + "purpose" + ] + == "TRY_RESPONSE" + ): + global session_id + session_id = parsed["Volatile"]["value"]["command"]["value"][ + "message" + ]["session_id"] + + enter_command() + + +def on_error(ws, error): + print(error) + + +def on_close(ws, close_status_code, close_msg): + print("### Connection closed ###") + + +def on_open(ws): + print("### Connection established ###") + print("[ " + websocket_uri + " ]") + enter_command() + + +def enter_command(): + print("") + print("LIST OF COMMANDS:") + print("") + print( + " PEP commands: PAP commands: PIP commands:" + ) + print( + " 1 : register 5 : add policy 9: add pip time" + ) + print( + " 2 : try access 6 : list policies 10: add pip reader" + ) + print( + " 3 : start access 7 : get policy 11: add pip websocket lamps" + ) + print(" 4 : end access 8 : delete policy") + print("") + + command = input("Enter command number> ") + print("") + send_command(command) + + +def send_command(command): + if command == "1": + register() + elif command == "2": + try_access() + elif command == "3": + start_access() + elif command == "4": + end_access() + elif command == "5": + add_policy() + elif command == "6": + list_policies() + elif command == "7": + get_policy() + elif command == "8": + delete_policy() + elif command == "9": + add_pip_time() + elif command == "10": + add_pip_reader() + elif command == "11": + add_pip_websocket() + elif command == "12": + unrecognized_command() + elif command == "q": + exit("") + else: + print("command not found") + enter_command() + + +def print_and_send(json_out): + print("Message sent:") + print(json.dumps(json_out, indent=2)) + ws.send(json.dumps(json_out)) + + +## REGISTER +def register(): + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pep-command", + "value": { + "message": { + "purpose": "REGISTER", + "message_id": str(uuid.uuid1()), + "sub_topic_name": "topic-name-the-pep-is-subscribed-to", + "sub_topic_uuid": "topic-uuid-the-pep-is-subscribed-to", + }, + "id": "pep-websocket_client", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n---------- REGISTER ------------\n") + print_and_send(ws_req) + + +## TRY ACCESS +def try_access(): + request = Path( + "sifis-xacml/manifest_3pa-lamp-amd64/request_1.xml" + ).read_text() + print("XACML request used:") + print(request) + b = base64.b64encode(bytes(request, "utf-8")) # bytes + request64 = b.decode("utf-8") + + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pep-command", + "value": { + "message": { + "purpose": "TRY", + "message_id": str(uuid.uuid1()), + "request": "Pippo", + "policy": None, + }, + "id": "pep-websocket_client", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n--------- TRY ACCESS -----------\n") + print_and_send(ws_req) + + +## START ACCESS +def start_access(): + global session_id + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pep-command", + "value": { + "message": { + "purpose": "START", + "message_id": str(uuid.uuid1()), + "session_id": session_id, + }, + "id": "pep-websocket_client", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n-------- START ACCESS ----------\n") + check_session_before_sending(ws_req) + + +## END ACCESS +def end_access(): + global session_id + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pep-command", + "value": { + "message": { + "purpose": "END", + "message_id": str(uuid.uuid1()), + "session_id": session_id, + }, + "id": "pep-websocket_client", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n--------- END ACCESS -----------\n") + check_session_before_sending(ws_req) + session_id = "None" + + +def check_session_before_sending(ws_req): + global session_id + + if session_id == "None": + print("WARNING: No active session yet") + answer = "?" + while answer != "y" and answer != "n": + answer = input("Do you want to proceed anyway? (y/n)> ") + if answer == "y": + print_and_send(ws_req) + elif answer == "n": + print("\n--------------------------------\n") + enter_command() + else: + print_and_send(ws_req) + + +## ADD POLICY +def add_policy(): + policy = Path("../../../src/main/resources/example-policy.xml").read_text() + print("XACML policy used:") + print(policy) + b = base64.b64encode(bytes(policy, "utf-8")) # bytes + policy64 = b.decode("utf-8") + + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pap-command", + "value": { + "message": { + "purpose": "ADD_POLICY", + "message_id": str(uuid.uuid1()), + "policy": policy64, + "policy_id": "example-policy", + }, + "id": "pap-web_socket", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n--------- ADD POLICY -----------\n") + print_and_send(ws_req) + + +## LIST POLICIES +def list_policies(): + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pap-command", + "value": { + "message": { + "purpose": "LIST_POLICIES", + "message_id": str(uuid.uuid1()), + }, + "id": "pap-web_socket", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n-------- LIST POLICIES ---------\n") + print_and_send(ws_req) + + +## GET POLICY +def get_policy(): + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pap-command", + "value": { + "message": { + "purpose": "GET_POLICY", + "message_id": str(uuid.uuid1()), + "policy_id": "example-policy", + }, + "id": "pap-web_socket", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n--------- GET POLICY -----------\n") + print_and_send(ws_req) + + +## DELETE POLICY +def delete_policy(): + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pap-command", + "value": { + "message": { + "purpose": "DELETE_POLICY", + "message_id": str(uuid.uuid1()), + "policy": None, + "policy_id": "example-policy", + }, + "id": "pap-web_socket", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n-------- DELETE POLICY ---------\n") + print_and_send(ws_req) + + +def add_pip_time(): + attribute_id = "urn:oasis:names:tc:xacml:1.0:environment:current-time" + category = "urn:oasis:names:tc:xacml:3.0:attribute-category:environment" + data_type = "http://www.w3.org/2001/XMLSchema#time" + refresh_rate = 10000 + + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pip-command", + "value": { + "message": { + "purpose": "ADD_PIP", + "message_id": str(uuid.uuid1()), + "pip_type": "it.cnr.iit.ucs.piptime.PIPTime", + "attribute_id": attribute_id, + "category": category, + "data_type": data_type, + "refresh_rate": refresh_rate, + }, + "id": "pip-time", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n------- ADD PIP TIME --------\n") + print_and_send(ws_req) + + +def add_pip_reader(): + attribute_id = ( + "eu:sifis-home:1.0:environment:all-windows-in-bedroom-closed" + ) + category = "urn:oasis:names:tc:xacml:3.0:attribute-category:environment" + data_type = "http://www.w3.org/2001/XMLSchema#boolean" + attribute_value = "true" + file_name = "windows-in-bedroom.txt" + refresh_rate = 1000 + + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pip-command", + "value": { + "message": { + "purpose": "ADD_PIP", + "message_id": str(uuid.uuid1()), + "pip_type": "it.cnr.iit.ucs.pipreader.PIPReader", + "attribute_id": attribute_id, + "category": category, + "data_type": data_type, + "refresh_rate": refresh_rate, + "additional_properties": { + attribute_id: file_name, + file_name: attribute_value, + }, + }, + "id": "pip-reader-windows", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n------ ADD PIP READER -------\n") + print_and_send(ws_req) + + +def add_pip_websocket(): + attribute_id = "eu:sifis-home:1.0:environment:lamps-status" + category = "urn:oasis:names:tc:xacml:3.0:attribute-category:environment" + data_type = "http://www.w3.org/2001/XMLSchema#boolean" + dhtUri = "ws://sifis-device4.iit.cnr.it:3000/ws" + topicName = "domo_light" + topicUuid = "bd59a9b8-fb3d-452d-b4ca-f3d13cf2d504" + refresh_rate = 10000 + + ws_req = { + "RequestPubMessage": { + "value": { + "timestamp": int(datetime.datetime.now().timestamp() * 1000), + "command": { + "command_type": "pip-command", + "value": { + "message": { + "purpose": "ADD_PIP", + "message_id": str(uuid.uuid1()), + "pip_type": "it.cnr.iit.ucs.pipwebsocket.PIPWebSocketLamps", + "attribute_id": attribute_id, + "category": category, + "data_type": data_type, + "refresh_rate": refresh_rate, + "additional_properties": { + "dhtUri": dhtUri, + "topicName": topicName, + "topicUuid": topicUuid, + }, + }, + "id": "pip-websocket-lamps", + "topic_name": "topic-name", + "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to", + }, + }, + } + } + } + print("\n--- ADD PIP WEBSOCKETLAMPS --\n") + print_and_send(ws_req) + + +## UNRECOGNIZED COMMAND +def unrecognized_command(): + ws_req = { + "RequestPostTopicUUID": { + "value": { + "log": { + "type": "info", + "priority": "low", + "category": "status", + "message": "ACE Group Manager", + } + }, + "topic_name": "SIFIS:Log", + "topic_uuid": "Log", + "deleted": "false", + } + } + print("\n----- UNRECOGNIZED COMMAND -----\n") + print_and_send(ws_req) + + print( + "\nMessage was sent. However, if the UCS does not recognize the command, " + "discards it without responding." + ) + print("\n--------------------------------\n") + enter_command() + + ### GET POLICY (policy_XXX) (not present) + # ws_req = { + # "RequestPubMessage": { + # "value": { + # "timestamp": int(datetime.datetime.now().timestamp()*1000), + # "command": { + # "command_type": "pap-command", + # "value": { + # "message": { + # "purpose": "GET_POLICY", + # "message_id": str(uuid.uuid1()), + # "policy_id": "policy_XXX" + # }, + # "id": "pap-0", + # "topic_name": "topic-name", + # "topic_uuid": "topic-uuid-the-ucs-is-subscribed-to" + # } + # } + # } + # } + # } + + +if __name__ == "__main__": + ws = websocket.WebSocketApp( + websocket_uri, + on_open=on_open, + on_message=on_message, + on_error=on_error, + on_close=on_close, + ) + + ws.run_forever() diff --git a/trials/list_applications.py b/trials/list_applications.py index a4b4e1f..56f1421 100644 --- a/trials/list_applications.py +++ b/trials/list_applications.py @@ -18,7 +18,7 @@ def on_open(ws): def publish(): ws = websocket.WebSocketApp( - "ws://localhost:3000/ws", + "ws://sifis-device4.iit.cnr.it:3000/ws", on_open=on_open, on_error=on_error, on_close=on_close, diff --git a/trials/pull_image.py b/trials/pull_image.py index 4decd2c..a7d09ed 100644 --- a/trials/pull_image.py +++ b/trials/pull_image.py @@ -18,7 +18,7 @@ def on_open(ws): def publish(): ws = websocket.WebSocketApp( - "ws://localhost:3000/ws", + "ws://sifis-device4.iit.cnr.it:3000/ws", on_open=on_open, on_error=on_error, on_close=on_close, @@ -35,7 +35,7 @@ def publish(): "operation": "pull_image", "requestor_id": "1", "request_id": "1", - "image_name": "ghcr.io/sifis-home/application-manager:latest", + "image_name": "3pa-lamp-amd64", # ghcr.io/sifis-home/ }, } } diff --git a/trials/remove_image.py b/trials/remove_image.py index 627affa..7ce2b29 100644 --- a/trials/remove_image.py +++ b/trials/remove_image.py @@ -18,7 +18,7 @@ def on_open(ws): def publish(): ws = websocket.WebSocketApp( - "ws://localhost:3000/ws", + "ws://sifis-device4.iit.cnr.it:3000/ws", on_open=on_open, on_error=on_error, on_close=on_close, @@ -35,7 +35,7 @@ def publish(): "operation": "remove_image", "requestor_id": "1", "request_id": "1", - "image_name": "ghcr.io/sifis-home/application-manager:latest", + "image_name": "ghcr.io/sifis-home/3pa-lamp-amd64", }, } }