tools.py 4.21 KB
Newer Older
Jiahang Chen's avatar
Jiahang Chen committed
1
"""This module provides a collection of convenience functions."""
C. Albrecht's avatar
WIP  
C. Albrecht committed
2
3
4
5
6

import datetime
import time
import uuid
import json
Jiahang Chen's avatar
Jiahang Chen committed
7
import os
C. Albrecht's avatar
WIP  
C. Albrecht committed
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
from functools import partial
from s3i import Directory
from s3i import Broker
from s3i import ServiceRequest
from s3i import TokenType
from s3i import IdentityProvider
from ml.app_logger import APP_LOGGER
from ml.authentication import GRANT_TYPES

IDENTITY_PROVIDER_URL = "https://idp.s3i.vswf.dev/"


class BColors:
    """colors for the console log"""

    HEADER = "\033[95m"
    OKBLUE = "\033[94m"
    OKGREEN = "\033[92m"
    WARNING = "\033[93m"
    FAIL = "\033[91m"
    ENDC = "\033[0m"
    BOLD = "\033[1m"
    UNDERLINE = "\033[4m"


Jiahang Chen's avatar
Jiahang Chen committed
33
def make_sub_thing(name, roles, features=[]):
C. Albrecht's avatar
WIP  
C. Albrecht committed
34
    """
Jiahang Chen's avatar
Jiahang Chen committed
35
36
37
    Creates a dictionary representing this thing in it's current state
    as a subordinate thing. This representation should be used for
    subordinate things in s3i repository entries
C. Albrecht's avatar
WIP  
C. Albrecht committed
38

Jiahang Chen's avatar
Jiahang Chen committed
39
40
41
42
43
44
    :param name: name of the sub thing
    :type name: str
    :param roles: fml40 roles of the sub thing
    :type roles: str
    :param features: fml40 features of the sub thing
    :type features: list
C. Albrecht's avatar
WIP  
C. Albrecht committed
45

Jiahang Chen's avatar
Jiahang Chen committed
46
47
    :returns: JSON of the sub thing
    :rtype: dict
C. Albrecht's avatar
WIP  
C. Albrecht committed
48
49

    """
Jiahang Chen's avatar
Jiahang Chen committed
50
51
52
    sub_thing = {
        "class": "ml40::Thing",
        "name": name,
Jiahang Chen's avatar
Jiahang Chen committed
53
54
        "roles": roles,
        "features": features
Jiahang Chen's avatar
Jiahang Chen committed
55
56
57
58
59
    }

    return sub_thing


Jiahang Chen's avatar
Jiahang Chen committed
60
def make_feature_config(class_name, identifier="", name="", subFeatures=""):
Jiahang Chen's avatar
Jiahang Chen committed
61
    """
Jiahang Chen's avatar
Jiahang Chen committed
62
    Creates a dictionary representing for its ml/fml40 features
Jiahang Chen's avatar
Jiahang Chen committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76

    :param class_name: class name of the fml40 feature
    :type class_name: str
    :param identifier: local id of the fml40 feature
    :type identifier: str
    :param name: name of the fml40 feature
    :type name: str
    :param subFeatures: sub features
    :type subFeatures: dict

    :returns: JSON of the fml40 feature
    :rtype: dict

    """
Jiahang Chen's avatar
Jiahang Chen committed
77
78
79
80
81
82
83
84
85
86
87
88
89
    config_json = {
        "class": class_name
    }
    if identifier:
        config_json["identifier"] = identifier
    if name:
        config_json["name"] = name
    if subFeatures:
        config_json["subFeatures"] = subFeatures

    return config_json


Jiahang Chen's avatar
Jiahang Chen committed
90
def make_thing_config(thing_id, name, roles, features=[], config_path=""):
Jiahang Chen's avatar
Jiahang Chen committed
91
    """
Jiahang Chen's avatar
Jiahang Chen committed
92
    Creates a configuration file (JSON) for a fml40 thing.
Jiahang Chen's avatar
Jiahang Chen committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    :param thing_id: identifier of the thing
    :type thing_id: str
    :param name: name of the thing
    :type name: str
    :param roles: fml40 roles of the thing
    :type roles: list
    :param features: fml40 features of the thing
    :type features: list
    :param config_path: path where the configuration file should be located
    :type config_path: str

    :returns: name of the configuration file of the thing
    :rtype: str


    """
Jiahang Chen's avatar
Jiahang Chen committed
110
111
112
    if not config_path:
        config_path = os.path.join("__file__", "configs")

Jiahang Chen's avatar
Jiahang Chen committed
113
    config_file = {
Jiahang Chen's avatar
Jiahang Chen committed
114
115
        "thingId": thing_id,
        "policyId": thing_id,
Jiahang Chen's avatar
Jiahang Chen committed
116
117
118
        "attributes": {
            "class": "ml40::Thing",
            "name": name,
Jiahang Chen's avatar
Jiahang Chen committed
119
120
            "roles": roles,
            "features": features,
Jiahang Chen's avatar
Jiahang Chen committed
121
122
        }
    }
Jiahang Chen's avatar
Jiahang Chen committed
123
124
    file_path = os.path.join(config_path, "{}.json".format(name))
    with open(file_path, 'wb') as file:
Jiahang Chen's avatar
Jiahang Chen committed
125
        file.write(json.dumps(config_file).encode('utf-8'))
126
    return "{}.json".format(name)
Jiahang Chen's avatar
Jiahang Chen committed
127
128


Jiahang Chen's avatar
Jiahang Chen committed
129
def load_config(config_filepath):
Jiahang Chen's avatar
Jiahang Chen committed
130
    """
Jiahang Chen's avatar
Jiahang Chen committed
131
    Loads a JSON object from a json formatted file found at config_filepath.
C. Albrecht's avatar
WIP  
C. Albrecht committed
132
133

    :param config_filepath: Path to json formatted file.
Jiahang Chen's avatar
Jiahang Chen committed
134
    :type config_filepath: str
C. Albrecht's avatar
WIP  
C. Albrecht committed
135
136

    """
Jiahang Chen's avatar
Jiahang Chen committed
137
    with open(config_filepath) as config_file:
C. Albrecht's avatar
WIP  
C. Albrecht committed
138
139
140
141
        config = json.load(config_file)
        return config


Jiahang Chen's avatar
Jiahang Chen committed
142
def find_broker_endpoint(dir_obj, thing_id):
C. Albrecht's avatar
WIP  
C. Albrecht committed
143
    """
Jiahang Chen's avatar
Jiahang Chen committed
144
    Finds the S3I-B endpoint of a thing
C. Albrecht's avatar
WIP  
C. Albrecht committed
145

Jiahang Chen's avatar
Jiahang Chen committed
146
147
148
    :param dir_obj: S³I Directory Object
    :type dir_obj: object
    :param thing_id: identifier of the searched thing
C. Albrecht's avatar
WIP  
C. Albrecht committed
149
150

    """
Jiahang Chen's avatar
Jiahang Chen committed
151
152
153
154
155
156
    thing_json = dir_obj.queryThingIDBased(thing_id)
    all_endpoints = thing_json["attributes"].get("allEndpoints", None)
    if all_endpoints:
        for ep in all_endpoints:
            if "s3ib" in ep:
                return ep
C. Albrecht's avatar
WIP  
C. Albrecht committed
157
158


Jiahang Chen's avatar
Jiahang Chen committed
159
def remove_namespace(input_str):
C. Albrecht's avatar
WIP  
C. Albrecht committed
160
    """
Jiahang Chen's avatar
Jiahang Chen committed
161
    Removes the specified namespace like ml40 or fml40
Jiahang Chen's avatar
Jiahang Chen committed
162

Jiahang Chen's avatar
Jiahang Chen committed
163
164
165
166
    :param input_str: input with namespace
    :type input_str: str
    :returns: output without namespace
    :rtype: str
Jiahang Chen's avatar
Jiahang Chen committed
167

Jiahang Chen's avatar
Jiahang Chen committed
168
    """
Jiahang Chen's avatar
Jiahang Chen committed
169
    return input_str.replace("fml40::", "").replace("ml40::", "")